Saturday, January 10, 2009

Sending email using either ISP or Gmail account

If you want to be able to send e-mails without having to set up a full-fledged MTA like sendmail or exim by just using an isp email account or gmail account follow this tutorial. By the way I only need this to receive log messages to my e-mail regularly, it is not for checking your mails form the slug... I found this tutorial on the net and it was mainly built for ubuntu. I changed and simplified some of the steps to make it easier to use.

Install the needed programs
apt-get install msmtp
apt-get install nail

Install Thawte certificate for Gmail.(This is necessary for Gmail. I tried and without this it is not working ;) )
mkdir -p ~/etc/.certs
chmod 0700 ~/etc/.certs
cd ~/etc/.certs
wget --no-check-certificate

Option 1)
Ooops at this step at the original tutorial it says to use unzip. I do not have unzip in my slug and I do not want to install it for one time use. So I copied the zip file to my laptop, unzipped and send to the server again. You could follow either way I gue

Option 2)
cp Thawte\ Server\ Roots/ThawtePremiumServerCA_b64.txt ThawtePremiumServerCA.crt
Then configure msmtp.Replace UPPERCASE text with your personal settings
nano ~/.msmtprc

Paste the config file given below to the .msmtprc.

# config options:
logfile /var/log/msmtp.log

# isp account
account isp
auth login
port 25
password *****

# gmail account
account gmail
auth on
port 587
password *****
tls on
tls_trust_file /home/USER/etc/.certs/ThawtePremiumServerCA.crt

# set default account to use (from above)
account default : gmail

Change permission of the msmtprc file:
chmod 600 ~/.msmtprc

Configure nail
nano ~/.mailrc

Copy/paste the config file below into .mailrc and make the necessary changes for your congiguration.

# set smtp for nail
# ref:
# docs:
# isp account (default)
# $ nail -s "subject line" -a /path/file < /path/body.txt
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"

# gmail account
# $ nail -A gmail -s "subject line" -a /path/file < /path/body.txt
account gmail {
set from=" (YOURNAME)"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

You are all done. Now you can send test messages…

echo -e "testing email from the command line" > /tmp/test_email
nail -s "isp test" < /tmp/test_email nail -A gmail -s "gmail test" < /tmp/test_email

Check your email accounts for the new messages. You can chech the log file with the command below.
nano /var/log/msmtp.log

This is the procedure I followed to use my gmail account and it worked great. But some said that it is sufficient to use either msmtp or nail for this purpose. So if you know how to do that tell us. I tried that way for a while ago and I could not make it work maybe it is because I did not use certificate. Anyway, hope this helps someone.
This is the link to the original tutorial if you want to check


  1. I was able to use nail with gmail WITHOUT an apt-get install msmtp. You just need nail.

    echo "abc" | nail -r " (My Name)" -s "test1" -S smtp=smtps:// -S smtp-auth=login -S -S smtp-auth-password="password1"

  2. P.S. You don't need any certificate either.