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:

(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


And we’re done!

To send emails from the Terminal either type:

    echo “email content” | mail -s “email subject”



Then enter the following:

    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 < 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