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

Written by James McDonald

March 13, 2008

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

( cat bodyofmail.txt; uuencode filetoattach attachmentnameinemail ) |\ 
mail -s "The subject" [email protected] 
# 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" [email protected] 
# 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" [email protected]

0 Comments

Submit a Comment

Your email address will not be published.

You May Also Like…

PDF Poster

PDF Poster

Start with a wide long image. This is just a screen shot of my desktop. widescreen.png Convert it to postscript using...