Skip to content

Instantly share code, notes, and snippets.

@SBejga
Last active December 15, 2021 05:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SBejga/ab009201a16370289250 to your computer and use it in GitHub Desktop.
Save SBejga/ab009201a16370289250 to your computer and use it in GitHub Desktop.
Bash Script as alias of "mail" using "ssmtp" in ubuntu. Main purpose is sending mails via munin by ssmtp instead of postfix.
#!/bin/bash
# mail syntax: mail -s "any mail subject string" any@mail.com
# or: mail "any mail subject string" any@mail.com
if [ $1 == "-s" ]
then
subject=$2
recip=$3
else
subject=$1
recip=$2
fi
# using temporary txt file in /tmp with filename by current date
name=/tmp/$(date '+%s')_ssmtp_mail.txt
# create payload for ssmtp mail
echo "To: $recip" > $name
echo "From: stranged.de@gmail.com" >> $name
echo "Subject: $subject" >> $name
echo "" >> $name
# receive mail payload by stdin line by line
while read line; do
echo "${line}" >> $name
done
# exec ssmtp command with tmp txt file we have created
ssmtp $recip < $name
# remove tmp file again
rm $name
# may need to symlink: ssmtp
# ln -s /usr/sbin/ssmtp /usr/bin/ssmtp
@boscho87
Copy link

👍 im a little thief :-) and copied this

@fwolfst
Copy link

fwolfst commented Apr 15, 2020

Nice, thanks. Might use it. I think it might be simplified a bit using named pipes or a kind of IO Buffer in memory instead of writing the stuff to disk. And $name is an odd variable name here, but hey this is a script and it does what it should. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment