Use Gmail account to send mail from a Shell prompt

Sometimes we want our desktops/servers to send mails automatically, but we dont want to setup a full mail server. sSMTP (not usual SMTP) can be perfect for this. It is extremely simple and resource conserving. In this article we will configure sSMTP and then use it to send outgoing email through gmail account.

Install sSMTP by running the following command in your terminal

[chia]$ sudo apt-get install ssmtp (Ubuntu users)

[chia]$ yum install ssmtp (Fedora users)

Configuring sSMTP is very easy, open /etc/ssmpt/ssmpt.conf is a text editor

[chia]$ vi /etc/ssmpt/ssmlt.conf 

Update the following Settings:

root=emailaddress@gmail.com

AuthUser=Gmail-Username

AuthPass=Gmail-Password

mailhub=smtp.gmail.com:587

UseSTARTTLS=YES

FromLineOverride=YES

Other mail service can also be configured by changing mailhub to their SMTP server.

Stop Sendmail and replace it with sSMTP. 

[chia]$ sudo service sendmail stop

[chia]$ sudo chkconfig --levels 2345 sendmail off

To completely replace Sendmail with sSMTP, copy sendmail to another file and then create a symbolic link from sSMTP to sendmail.

[chia]$ mv /usr/sbin/sendmail /root/.sendmail.backup

[chia]$ sudo ln -s /usr/local/ssmpt/sbin/ssmpt /usr/sbin/sendmail

Once you are done configuring these settings you can try sending a mail.

Open  a terminal and run the command

[chia]$ ssmpt reciever-email@example.com

Follow the following format

To: reciever-email@example.com

From: your-email-address@gmail.com

Subject: Test mail

 

This is a test mail.

There is a blank line between the subject and the body of the mail. After you are done with this you can send the mail by pressing Ctrl-D.

Further you can set a cron job and schedule a mail. Store the mail is some text file. Add this command to your cron.

ssmtp myemailaddress@gmail.com < message.txt

You can also use mail or mailx command to send email. 

[chia]$ echo "This is a test mail" | mail -s "Test mail" reciever-email@example.com

 

1 Comment

American Job Advocate (not verified)
April 5th, 2010 09:04 am
Nice. I was doing some PHP and wanted a cheap easy way to send mail using my gmail. this will work nicely as I'll just shell exec :) thanks

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Use syntax $$latex$$ to insert LaTeX formulas.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.