100 lines
3.6 KiB
Bash
Executable File
100 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
## author : Dryusdan
|
|
## date : 13/05/2020
|
|
## description : A Ghost newsletter sender
|
|
## usage : ./ghost.sh postid
|
|
|
|
## Bash strict mode ####################################
|
|
set -o errexit # abort on nonzero exitstatus
|
|
set -o nounset # abort on unbound variable
|
|
set -o pipefail # don't hide errors within pipes
|
|
|
|
## Bash color ##########################################
|
|
# Set colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[00;32m'
|
|
YELLOW='\033[00;33m'
|
|
BLUE='\033[00;34m'
|
|
PURPLE='\033[00;35m'
|
|
CYAN='\033[00;36m'
|
|
LIGHTGRAY='\033[00;37m'
|
|
LRED='\033[01;31m'
|
|
LGREEN='\033[01;32m'
|
|
LYELLOW='\033[01;33m'
|
|
LBLUE='\033[01;34m'
|
|
LPURPLE='\033[01;35m'
|
|
LCYAN='\033[01;36m'
|
|
WHITE='\033[01;37m'
|
|
NC='\033[0m' # No Color
|
|
|
|
## Logs ################################################
|
|
readonly SCRIPTNAME="$(basename "$0")"
|
|
debug() { echo -e "${LPURPLE}[DEBUG] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
|
|
info() { echo -e "${LBLUE}[INFO] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
|
|
warning() { echo -e "${YELLOW}[WARNING] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
|
|
error() { echo -e "${LRED}[ERROR] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
|
|
fatal() { echo -e "${RED}[FATAL] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; exit 1 ; }
|
|
########################################################
|
|
|
|
## Starting ###########################################
|
|
info "Sourcing environment variable"
|
|
source .env
|
|
|
|
info "Getting cookie"
|
|
# Create a session, and store the cookie in ghost-cookie.txt
|
|
curl -c ghost-cookie.txt -d username=${GHOST_USER} -d password=${GHOST_PASSWORD} -H "Origin: https://${GHOST_WEBSITE}" https://${GHOST_WEBSITE}/ghost/api/v3/admin/session/
|
|
|
|
info "Getting HTML and subject"
|
|
# Use the session cookie to create a post
|
|
SUBJECT="Nouvel article sur mon blog : $(curl -s -b ghost-cookie.txt -H "Origin: https://${GHOST_WEBSITE}" "https://${GHOST_WEBSITE}/ghost/api/v3/admin/email_preview/posts/${1}/" | jq --raw-output '.email_previews[].subject')"
|
|
HTML=$(curl -s -b ghost-cookie.txt -H "Origin: https://${GHOST_WEBSITE}" "https://${GHOST_WEBSITE}/ghost/api/v3/admin/email_preview/posts/${1}/" | jq --raw-output '.email_previews[].html')
|
|
PLAINTEXT=$(curl -s -b ghost-cookie.txt -H "Origin: https://${GHOST_WEBSITE}" "https://${GHOST_WEBSITE}/ghost/api/v3/admin/email_preview/posts/${1}/" | jq --raw-output '.email_previews[].plaintext')
|
|
|
|
info "Getting email"
|
|
for member in $(curl -s -b ghost-cookie.txt -H "Origin: https://${GHOST_WEBSITE}" "https://${GHOST_WEBSITE}/ghost/api/v3/admin/members/?limit=all" | jq --raw-output ".members[] | @base64")
|
|
do
|
|
email=$(echo ${member} | base64 --decode | jq --raw-output '.email')
|
|
uuid=$(echo ${member} | base64 --decode | jq --raw-output '.uuid')
|
|
HTML=$(echo ${HTML} | sed "s/preview=1/uuid=${uuid}/g")
|
|
if [ $(echo ${member} | base64 --decode | jq --raw-output '.subscribed') == true ]
|
|
then
|
|
info "send email to ${email}"
|
|
|
|
boundary="$(openssl rand -hex 24)"
|
|
content_type="content_type='multipart/alternative; boundary=${boundary}'"
|
|
|
|
##
|
|
cat << END > newmail.eml
|
|
From: ${MAIL_FROM}
|
|
To: ${email}
|
|
#Date: $(date)
|
|
Subject: ${SUBJECT}
|
|
MIME-Version: 1.0
|
|
Content-Type: multipart/alternative;
|
|
boundary="${boundary}"
|
|
--$boundary
|
|
Content-Type: text/plain; charset="UTF-8"
|
|
Content-Transfer-Encoding: quoted-printable
|
|
|
|
${PLAINTEXT}
|
|
|
|
--$boundary
|
|
Content-Type: text/html; charset="UTF-8"
|
|
Content-Transfer-Encoding: quoted-printable
|
|
|
|
${HTML}
|
|
|
|
--$boundary--
|
|
END
|
|
##
|
|
info "Sending mail"
|
|
echo "" | mutt -H newmail.eml
|
|
sleep 30
|
|
rm newmail.eml
|
|
else
|
|
warning "No email send ${email}"
|
|
fi
|
|
done
|
|
rm ghost-cookie.txt
|
|
info "Finish"
|