Skip to content

Instantly share code, notes, and snippets.

@nestorwheelock
Forked from fbatschi/bash-smtp-auth-email
Last active January 2, 2020 06:01
Show Gist options
  • Save nestorwheelock/df9223e164ee58d39010f697da4f17b7 to your computer and use it in GitHub Desktop.
Save nestorwheelock/df9223e164ee58d39010f697da4f17b7 to your computer and use it in GitHub Desktop.
How to send an email from bash via SMTP Auth
$!/bin/sh
# Send an email from bash with SMTP This is an attempt to create a pseudo class to have an email routine/template to use
# in scripts that send emails. I'm using FreeBSD so the original gist had been modified to make it work there.
RECIPIENT=""
SENDERNAME=""
SENDEREMAIL=""
SMTPUSERNAME=""
SMTPPASSWD=""
SMTPSERVER=""
SUBJECT=""
BODY=`cat <<'EOF'
Insert Body Here
EOF`
# Use quotes around "$BODY" to preserve newlines
# This seems to be working on FreeBSD
$SENDMAIL=`echo "$BODY" | mailx -s $SUBJECT $RECIPIENT -f "From: $SENDERNAME <$SENDEREMAIL>"
$SENDMAIL
# not working on FreeBSD -S not found so might make a switch to mutt
#SENDEMAIL=`echo "$BODY" | mailx -s $SUBJECT -S smtp=smtp://$SMTPSERVER -S smtp-auth=login -S smtp-auth-user=$SMTPUSERNAME -S smtp-auth-password=$SMTPPASSWD -S from="$SENDERNAME <$SENDEREMAIL>" $RECIPIENT)`
$SENDEMAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment