It’s common request to allow daemons like cron or applications to send outgoing email to server’s administrator. Instead of configuring full-blown email server like Postfix you can install lightweight forwarder to real external SMTP server, while keeping binary compatibility with traditional Unix sendmail MTA.
Sendmail compatibility is crucial as it is expected by many many applications. Apart from cron, you may need it for PHP
mail() function, Bugzilla mailing feature and many others stuff originating from Linux/Unix universe.
For this tutorial I chose MSMTP delegating to secured Google Apps (or Gmail) SMTP. However instructions apply to any SMTP
Google Apps / Gmail SMTP ports
On Google Support article you find up-to-date SMTP server address and configuration. Google SMTP can be accessed only over secured TLS which complicates MSTMP setup a little.
First off, install MSTMP on your box:
$ sudo apt-get install msmtp
I always start with discovering remote server certificate with
--serverinfo option. Also, for Gmail I changed default port 465 which never worked for me to alternative port 587. Your output would be similar to the following:
$ msmtp --serverinfo --host=smtp.gmail.com --tls=on --tls-certcheck=off --port=587
SMTP server at smtp.gmail.com (ee-in-f109.1e100.net [188.8.131.52]), port 587:
mx.google.com ESMTP y12sm259113106eeb.11
TLS certificate information:
Common Name: smtp.gmail.com
Organization: Google Inc
Locality: Mountain View
State or Province: California
Common Name: Google Internet Authority
Organization: Google Inc
Activation time: St 16. únor 2011, 05:38:09 CET
Expiration time: Čt 16. únor 2012, 05:48:09 CET
Maximum message size is 35882577 bytes = 34,22 MiB
Support for TLS encryption via the STARTTLS command
Supported authentication methods:
MSMTP can be driven from command line for one-time use, or from user or system-wide configuration file. Configuration file location is listed on
$ msmtp --version
System configuration file name: /etc/msmtprc
User configuration file name: /home/libor/.msmtprc
If system configuration file
/etc/msmtprc doesn’t exist, create it
$ sudo touch /etc/msmtprc
Copy’n’paste & customize the following configuration for your Gmail/Google Apps account. Below I configured one account and again used alternative port 587. Consult MSMTP man page for more details.
# Default settings that all others account inherit
# Logging - uncomment either syslog or logfile, having both uncommented disables logging at all.
# Or to log to log own file
# Gmail/Google Apps (configure as may as you want)
# Default account to use
account default : gmail
Because this file contains password in plain text you should cut down its permissions. Let’s make use of group called mail coming from default Ubuntu installation. Any process which is run under mail group member can read this file and therefore actually send email.
$ sudo chgrp mail /etc/msmtprc
$ sudo chmod 660 /etc/msmtprc
Add any users who needs to be member of mail group (like your webserver user etc.)
$ sudo adduser www-data mail
$ sudo adduser ... mail
If not configured differently, MSMTP has logging disabled at all. Hopefully MSMTP can be set to log to separate log file or syslog, but not both (it will stop logging).
/etc/msmtprc or your user configuration file add/change key
/var/log/msmtp.logor whatever file you wish to log into dedicated file OR
syslog onto enable syslog logging. Default facility is
LOG_USERor specify with syslog .
MSMTP logs to syslog but we want to tweak it to log on startup to
If you log to file, it has to exists prior MSMTP can log into it. Therefore create & set permissions appropriately:
$ sudo touch /var/log/msmtp.log
$ sudo chgrp mail /var/log/msmtp.log
$ sudo chmod 660 /var/log/msmtp.log
Setting PHP to MSMTP
/etc/php5/apache2/php.ini and change
sendmail_path = "/usr/bin/msmtp -t"
Restart Apache to gain permissions to read MSMTP configuration file and send emails
sudo service apache2 restart
Symlink to sendmail
However MSMTP is binary compatible with sendmail, it doesn’t create sendmail executable in your path. Applications like Bugzilla has hard-wired link to sendmail executable and in current setup you will see error the following error whey you try to send email:
There was an error sending mail from 'email@example.com' to 'firstname.lastname@example.org': Couldn't find 'sendmail' executable in your PATH and $Email::Send::Sendmail::SENDMAIL is not set.
To fix, symlink MSMTP to common places:
$ ln -s /usr/bin/msmtp /usr/sbin/sendmail
$ ln -s /usr/bin/msmtp /usr/bin/sendmail
$ ln -s /usr/bin/msmtp /usr/lib/sendmail
-v option to see all SMTP communication. MSMTP reads message body from stdin (as sendmail do).
Create file called e.g.
testmail having the following contents:
Subject: A test
Yadda, yadda, yadda.
cat testmail | msmtp email@example.com
Do not merely use “echo ‘Yadda, yadda, yadda.’” instead of “cat test.mail”. This causes at least Gmail and Yahoo to deliver the mail incorrectly. Recipient specified as commandline argument is required however To recipient from file takes precedence over commandline.
You may also review msmtp.log with e.g.
Error “msmtp: account default not found: no configuration file available”
Can you see this error in console or in logs and mail is not actually sent? Check the permissions of
/etc/msmtprc for user running msmtp. This error indicates that a process doesn’t have permission to read configuration file.
***Update by me***
However. I found out if you’re going to use an bash/znc/whatever user you can copy/paste the content of /etc/msmtprc into ~/.msmtprc for the user you’re going to send mail from
Many scripts assume existence of
Mail offers commandline interface more suitable for scripting then msmtp. Send email with mail program is easy as
echo 'Hi from mail body' | mail -s 'Some subject' 'some@email'
SSMTP: alternative for MSMTP
There is also SSMTP but it seems to be abandoned, though it’s still in Ubuntu repositories. For completeness, here are some links for SSMTP:
Original post by Libor Jelinek http://blazerjenzo.tk/1b5pxuD