Skip to content

Instantly share code, notes, and snippets.

@shebin512
Last active February 16, 2021 20:20
Show Gist options
  • Save shebin512/bcdabb6329a92d3e315a6e062603ebad to your computer and use it in GitHub Desktop.
Save shebin512/bcdabb6329a92d3e315a6e062603ebad to your computer and use it in GitHub Desktop.
cUrl Send email using SMTP
source ./sendEmailCurl-v1.1.sh # OR ./sendEmailCurl-v1.0.sh
EMAILTO='user1@example.com,user2@test1.com' \
EMAILSUBJECT="Test EMAIL $(date +%F)" \
EMAILBODY="This is a test email from $(HOSTNAME)\n USER $(USER)" \
sendEmail "$@"
exit 0
#!/usr/bin/env bash
###################################################################
#Script Name : sendEmailCurl.sh
#Description : Script for sending email using curl
#Args : NA use variables
#Author : Shebin Sheikh
#Email : shebin512@gmail.com
#Ref : https://blog.edmdesigner.com/send-email-from-linux-command-line/
###################################################################
set -e
[ "${DEBUG}" == 'true' ] && set -x
sendEmail() {
##################################################
# Usage:
# source ./sendEmail.sh
# EMAILTO='user1@example.com,user2@example2.org' \
# EMAILSUBJECT="Email Subject" \
# EMAILBODY="Email Body" \
# sendEmail "$@"
##################################################
[ -z "${EMAILTO}" ] || [ -z "${EMAILSUBJECT}" ] || [ -z "${EMAILBODY}" ] && $(echo -e "Required variable/s missing!\n";exit 1)
# ===== SMTP CONFIGURATIONS 1 =====
SMTPADDRESS='smtp://smtp.gmail.com:465'
SMTPFROM='user@gmail.com'
SMTPUSER='user'
SMTPPASS='pass'
# Reciepients Array
RCPTS=($(echo $EMAILTO | tr ',' '\n'))
# Generate "--mail-rcpt" for multiple recepients
CURLRCPTS=''
for RCPT in ${RCPTS[@]}
do
CURLRCPTS="--mail-rcpt ${RCPT} "${CURLRCPTS}
done
EMAILCONTENT="From: ${SMTPFROM}\nTo: ${EMAILTO}\nSubject: ${EMAILSUBJECT}\nDate: $(date -R)\n\n${EMAILBODY}"
# exit 0
curl -v --ssl-reqd ${SMTPADDRESS} \
--mail-from ${SMTPFROM}\
--user "${SMTPUSER}:${SMTPPASS}" \
$CURLRCPTS \
-T <(echo -e "${EMAILCONTENT}")
}
EMAILTO='user1@example.com,user2@test1.com' \
EMAILSUBJECT="Test EMAIL $(date +%F)" \
EMAILBODY="This is a test email from $(HOSTNAME)\n USER $(USER)" \
sendEmail "$@"
exit 0
set -e
[ "${DEBUG}" == 'true' ] && set -x
# ===== SMTP CONFIGURATIONS 1 =====
SMTPADDRESS='smtp://smtp.gmail.com:465'
SMTPFROM='user@gmail.com'
SMTPUSER='user'
SMTPPASS='pass'
##################################################
# Usage:
# source ./sendEmail-v1.1.sh
# EMAILTO='user1@example.com,user2@example2.org' \
# EMAILSUBJECT="Email Subject" \
# EMAILBODY="Email Body" \
# sendEmail "$@"
# GLOBAL Vars:
# SMTPADDRESS
# SMTPFROM
# SMTPUSER
# SMTPPASS
##################################################
sendEmail() {
[ -z "${EMAILTO}" ] || [ -z "${EMAILSUBJECT}" ] || [ -z "${EMAILBODY}" ] && { echo -e "Required variable/s missing!\n";exit 1; }
EMAILCONTENT="From: ${SMTPFROM}\nTo: ${EMAILTO}\nSubject: ${EMAILSUBJECT}\nDate: $(date -R)\n\n${EMAILBODY}"
RCPTS=$(echo "${EMAILTO}" | tr ',' ' ')
CMD="curl -v --ssl-reqd ${SMTPADDRESS} --mail-from ${SMTPFROM} --user \"${SMTPUSER}:${SMTPPASS}\""
# Generate "--mail-rcpt" for multiple recepients
for RCPT in ${RCPTS};do
CMD="${CMD} --mail-rcpt ${RCPT}"
done
CMD="${CMD} -T <(echo -e \"${EMAILCONTENT}\")"
eval "${CMD}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment