Send email from command-line using a remote SMTP server
This tutorial shows you how to configure your machine to send emails using a remote SMTP server, in the case of this tutorial we will use gmail’s SMTP server.
We will start by installing and configuring sSMTP:
cd /tmp wget http://ftp.us.debian.org/debian/pool/main/s/ssmtp/ssmtp_2.64-4_amd64.deb dpkg -i ssmtp_*
We will also require the “mailutils” package (available by default on the majority of distro’s) but it wasn’t on mine, so we do:
apt-get install mailutils
Now we will configure the remote mail server settings:
nano /etc/ssmtp/ssmtp.conf
Example Configuration:
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=mail # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=myhostname AuthUser=myusername@gmail.com AuthPass=mypassword FromLineOverride=YES mailhub=smtp.gmail.com:587 UseSTARTTLS=YES
It is also important to make sure that no other MTA’s are running, for example postifx, sendmail etc., you can do a quick check as follows:
netstat -tuna | grep 25
We can now send a test email in the following format:
echo "<subject>" | mail -s "<message>" <email-address>
e.g.
echo "My Subject" | mail -s "Hellow World!" myname@gmail.com
If you encounter any problems or are not receiving emails, re-check your configuration (I think Gmail requires you to enable SMTP access externally – I maybe wrong though.) and you can check the mail queue by using:
mailq
and debug problems by checking /var/log/mail.error or /var/log/mail.log