How to send an email in a bash script

bash email

You can send an email in a bash script using mutt command. Here I have explained how you can send an email with an attachment to multiple recipients using a linux shell script.

Here is the format of the script.

echo "<message body>" | mutt -s "<subject>" -a "<optional attachment>" -e "my_hdr From:<from address>" -- <recipients list separated by space>

Below is a sample bash script using the above code format.


subject="Email from bash"
body="This email was sent using a bash script"
from="me@example.com"
to="you@example.com other@example.com"
fileAttachment="/tmp/test.txt"

echo "Sending email..."
echo "$body" | mutt -s "$subject" -a "$fileAttachment" -e "my_hdr From:$from" -- $to
echo "Email sent to: $to"

Additionally you can use following parameters,

  • -c carbon copy (Cc)
  • -b blind carbon copy (Bcc)

You can view the full parameter list in official mutt website.

Image courtesy : JASE Group LLC

Enjoyed this article? Share with friends

Subscribe

Subscribe to our email feed and never miss a post

Join Us