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 https://www.verisign.com/support/thawte-roots.zip --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)
unzip thawte-roots.zip
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: http://msmtp.sourceforge.net/doc/msmtp.html#A-user-configuration-file
defaults
logfile /var/log/msmtp.log

# isp account
account isp
auth login
host SMTP.YOURISP.COM
port 25
user YOURNAME@ISP.COM
from YOURNAME@ISP.COM
password *****

# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
user YOURNAME@gmail.com
password *****
from YOURNAME@gmail.com
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: http://ubuntuforums.org/showpost.php?p=4531994&postcount=6
# docs: http://msmtp.sourceforge.net/doc/msmtp.html#Configuration-files
# isp account (default)
# $ nail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
set from="YOURNAME@ISP.COM"
set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a isp"

# gmail account
# $ nail -A gmail -s "subject line" -a /path/file recipient@email.com < /path/body.txt
account gmail {
set from="YOURNAME@gmail.com (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" YOURNAME@gmail.com < /tmp/test_email nail -A gmail -s "gmail test" YOURNAME@gmail.com < /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 http://phosphorusandlime.blogspot.com/2008/05/ubuntu-command-line-email.html

2 comments:

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

    echo "abc" | nail -r "me@gmail.com (My Name)" -s "test1" -S smtp=smtps://smtp.gmail.com:465 -S smtp-auth=login -S smtp-auth-user=me@gmail.com -S smtp-auth-password="password1" you@gmail.com

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

    ReplyDelete