Skip to content

Instantly share code, notes, and snippets.

@kgaughan
Created June 15, 2015 10:52
Show Gist options
  • Save kgaughan/d1498b42156ecb89980f to your computer and use it in GitHub Desktop.
Save kgaughan/d1498b42156ecb89980f to your computer and use it in GitHub Desktop.
Sending an email from the shell
# To send a email, use the 'mailx' command. This takes the email to send on
# standard input. The -s flag is used to specify your subject, and the
# recipient address is specified without any flags. Everything after '--' is
# passed on to the Mail Transport Agent (MTA, the local mailserver), and the
# '-f' flag is used to specify the sender address, while '-F' is used to
# specify the sender name.
#
# See 'man 1 mailx' for details. The Debian packages you can use for this are
# 'bsd-mailx' or 'heirloom-mailx', though both present slightly different
# command line interfaces. I'm using 'bsd-mailx' here.
mailx -s "My subject" recipient@example.com -- -F "Jane Doe" -f sender@example.com <<END
This is my email, and it's in a heredoc!
END
# To add an attachment, the simplest method is to uuencode the attachment.
# UU-encoded attachments can be put anywhere in the email. 'uuencode' itself
# takes two arguments: the full path of the attachment and the filename to use
# for the attachment in the email. Here's an example.
my_attachment=/path/to/my/attachment
attachment_name=$(basename $my_attachment)
mailx -s "My subject" recipient@example.com -- -F "Jane Doe" -f sender@example.com <<END
I'm sending you something very useful.
$(uuencode $my_attachment $attachment_name)
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment