How to Send Email from Gmail SMTP with Linux Command Line

Channel: Linux
Abstract: 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 conte

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.

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 SMTPExample 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 "[email protected]"   \
    -u "Email Subject 1"     \
    -t "[email protected]" \
    -s "smtp.gmail.com:587"  \
    -o tls=yes \
    -xu "[email protected]" \
    -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 "[email protected]"  \
    -u "Email Subject 2"  \
    -t "[email protected]"  \
    -cc "[email protected]"  \
    -bcc "[email protected]"  \
    -s "smtp.gmail.com:587"  \
    -o tls=yes  \
    -xu "[email protected]"  \
    -xp "Email Password" 

Ref From: tecadmin

Related articles