2009-04-04

bash - send email with attachment or empty body with mutt

The mutt is not just an email client, it is also very useful in bash script. mutt's CLI (command line interface) parameter can be called and execute without starting the email client interactively, ideal to run it within bash script to send email. mutt is also able to control from config file, ~/.muttrc, with a rich feature set waiting to harness. Below is one of the possible way to send email with attachment, one or multiple, in a bash script :

brief mutt parameters :
-s = subject
-a = attachment. multiple attachment with need multiple -a


common usage :
echo "the email body text" | mutt -s "the email subject" -a attachment01.txt recipient-email-address
e.g.
echo "this is the email body" | mutt -s "Re: subject" -a somepicture.jpg someone@some-domain.com


send an email with empty body :
echo | mutt -s "testing with empty body" recipient-email-address
e.g.
echo | mutt -s "Re: subject" someone@some-domain.com


send multiple attachment :
echo "the email body text" | mutt -s "test sending multiple attachment" -a attachment01.txt -a attachment02.txt recipient-email-address
e.g.
echo "this is the email body" | mutt -s "Re: the email comes with multiple attachment" -a picture01.jpg -a picture02.png someone@some-domain.com


alternative way to invoke mutt to include body text :
mutt -s "the email subject" -a attachment01.txt recipient-email-address < the-email-body-text-file
e.g.
mutt -s "this is the email body" -a picture99.gif someone@some-domain.com < email-body.txt

Have fun !!!

No comments: