In our earlier tutorial we had discussed about SSMTP for sending email through SMTP server via command line. This tutorial makes this process much easier using the sendemail command-line utility. SendEmail is a Lightweight command line SMTP email client for sending emails through the SMTP server.

Advertisement

In this article, we have described how to install sendemail package and a few examples for sending email through remote SMTP servers like Gmail.

Install SendEmail Package

SendEmail SMTP client is available under default Ubuntu repositories, Use the following commands to install it.

sudo apt-get update
sudo apt-get install sendemail

Send Email through SMTP

Example 1:

In this example, the email body is passed as an argument (-o message-file=/tmp/mailbody.txt) to the command. Commands read the file content and use at email body content.

sendemail -l email.log     \
    -f "sender@domain.com"   \
    -u "Email Subject 1"     \
    -t "receiver@domain.com" \
    -s "smtp.gmail.com:587"  \
    -o tls=yes \
    -xu "youremail@gmail.com" \
    -xp "Email Password" \
    -o message-file="/tmp/mailbody.txt"

Example 2:

In this example, email body text is passed as piped input from other command output. You can also include cc and bcc email addresses as well.

cat mailbody.txt | sendemail -l email.log \
    -f "sender@domain.com"  \
    -u "Email Subject 2"  \
    -t "receiver@domain.com"  \
    -cc "receiver2@domain.com"  \
    -bcc "receiver3@domain.com"  \
    -s "smtp.gmail.com:587"  \
    -o tls=yes  \
    -xu "youremail@gmail.com"  \
    -xp "Email Password" 
Share.

7 Comments

  1. I dont have 2 factor auth enabled and I am still getting
    ERROR => ERROR => SMTP-AUTH: Authentication to smtp.gmail.com:587 failed.

    Any ideas ?

  2. Works like a charm. Yet I have one question: is there away to pass the body as html? I tried with the -o option referencing to a .html file but it just sends it in clear.

  3. Doesn’t work. Gmail changed the certification routine.
    Authentication to smtp.gmail.com:587 failed.

    • If you have 2 factor auth enabled, you need to create an “application password” in google security, then use that instead of your normal password. it works fine then, as posted here.

  4. Thanks. It worked. You saved my sunday!
    Can you tell me where can I configure gmail SMTP? so that i dont have to type every time gmail id and password!

Exit mobile version