Thursday, March 8, 2012

Command line email with attachment

The problem: How to send an email with attachment using CLI?

Solution: Use mutt.
I am assuming that you have a SMTP server from your ISP that you can use. If you don't know what is a SMTP server then this is definitely the case. Typically the smtp server's host name is smtp.youisp.net, so if you are a comcast customer your smtp server is smtp.comcast.net

To use this smtp server you have to add an entry in ~/.muttrc file. Edit this file so that it contains something like:

set smtp_url="smtp://smtp.comcast.net:587/"
set smtp_pass="yourpassword"

Then execute mutt like this:
echo "All the text that I want in Body of the message" | mutt -a -s "Picture $name" -- youemail@yourdomain.com



For example the line below send all the pictures as attachment to my email:

for name in `ls -1 *.JPG` ; 
do 
echo "Here is the picture $name" | mutt -a $name -s "Picture $name" -- to_email@domain.com; 
echo send $name; 
done


Thats it! 

Note: 
If you need to change the from address in the email then set EMAIL environment variable to desired email address


Many ISPs are not supporting port 25 anymore and also requiring user name and password. Please see for doing all that:
http://nixtricks.wordpress.com/2010/05/05/mutt-configure-mutt-to-receive-email-via-imap-and-send-via-smtp/

No comments: