Skip to content


How to attach a file to an email using the Linux mail command

How to attach a binary file to an email using the Linux mail command:

( cat bodyofmail.txt; uuencode filetoattach attachmentnameinemail ) |\
 mail -s "The subject" me@mydomain.com
 
# for example
# if I had the text body of my email in "body.txt"
# and I had a PDF file named "mypdf.pdf" which 
# I wanted it sent as an attachment with the same name
# I would use this command:
( cat body.txt; uuencode mypdf.pdf mypdf.pdf ) |\
 mail -s "Attached is your Invoice" emailaddress@emaildomain.com.au
 
# you can also stuff you email BODY into a variable
BODY="This is the body\n\nThere are a lot of things to do with mail\n"
 
# use the -e option to get echo to translate the embedded new lines (e.g. \n)
( echo -e $BODY; uuencode mypdf.pdf mypdf.pdf ) |\
 mail -s "Attached is your Invoice" emailaddress@emaildomain.com.au
Bookmark and Share

Posted in Linux Tools.

Tagged with , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.