Sunday 5 May 2013

Sending emails from the Terminal in linux | Send mail using terminal


In this post I’m going to explain how to set up sSMTP in order to send emails through your Gmail right from the Terminal. Once you have sSMTP set up you can also use cron jobs to get you computer/server to automatically email you when there is a problem. Please note that this only works on Linux (and Mac OS X).

First we install sSMTP:

    sudo apt-get install ssmtp

Configure sSMTP by editing ssmtp.conf:

    vi /etc/ssmtp/ssmtp.conf

You can delete everything in that file and just replace it with this:

    root=your_username@gmail.com
    UseSTARTTLS=YES
    UseTLS=YES
    mailhub=smtp.gmail.com:465
    rewriteDomain=gmail.com
    FromLineOverride=YES
    AuthUser=username
    AuthPass=********

(If the gmail is a Google Apps account then use the full email address for the AuthUser.)
Now we need to edit revaliases to add each user account we want to send mail from:

    vi /etc/ssmtp/revaliases

Add:

    root:username@gmail.com:smtp.gmail.com:587
    user:username@gmail.com:smtp.gmail.com:587

And we’re done!

To send emails from the Terminal either type:

    echo “email content” | mail -s “email subject” recipient_email@domain.com

or:

    ssmtp recipient_email@domain.com

Then enter the following:

    To:Â recipient_email@domain.com
    From: your_email@gmail.com
    Subject: this is your email subject
    And here you write the content of the email

And to send the email, press: CTRL+D
(N.B. The empty line between the Subject and the content of the email must not be omitted)

You can also send emails using a text file:

    ssmtp recipient_email@domain.com < message.txt

(The contents of message.txt must be in the same format as above)

Depending on your machine’s set up, it may be necessary to remove sendmail and install mailutils:

    sudo service sendmail stop
    sudo apt-get remove sendmail
    sudo apt-get install mailutils

If you have any questions please leave a comment!

No comments:

Post a Comment