Send mail from bash with attachment

Install mutt.

apt-get install mutt


Run mutt with the following syntax:

echo "This is the message body" | mutt -a /somewhere/file -s "subject of message" -- [email protected]

Note that when you use variables, there is a slight modification necessary:

echo $BODY | mutt -a /pathto/file -s "$SUB" -- [email protected]

Note that the subject coming after -s needs to be enclosed in quotes. In fact whenever you need to use variables as parts of the mutt command, you need to enclose the respective variable in double quotes. Single quotes of course escape the variables and would use the variable name as literals.

The sender unless specified is taken as your [email protected], where username is your root shell username, which is usually root.

You can use a file as the BODY field of the email. Can you guess the syntax?

cat /pathto/bodyfile | mutt -a /somewhere/attachmentfile -s "subject of message" -- [email protected]

cat /pathto/bodyfile | mutt -a /somewhere/attachmentfile -s “subject of message” — [email protected]

If you need to specify the sender:

[[email protected]] ~ #cat ~/.muttrc
set from="BackupServer <[email protected]>"

You can change the sender header at runtime, without relying on .muttrc, by building up a variable for the header using quotes for your literals, and concatenate (join) them with variables.


Say you need your email to be coming from [email protected], with the name John B. Doe, you’d have to do something like this:

[email protected]
fname="John B. Doe"
hdrst='my_hdr From:'$fname' <'$email'>'

which will build the string:

my_hdr From:John B. Doe<[email protected]>

and send it to the mutt command as a custom header for your email.

echo $BODY | mutt -e "$hdrst" -a fullbackup -s "$SUB" -- [email protected]

Once again, note that the command will fail unless the variables $hdrst and $SUB are enclosed in double quotes.


  1. Official mutt FAQ
  2. Mutt manual
  3. Cybergav blog post

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.