Email sending from Ubuntu Server via Google Apps SMTP with MSMTP

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.

Get prepared

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 --tls=on --tls-certcheck=off --port=587 
SMTP server at ( []), port 587: ESMTP y12sm259113106eeb.11
TLS certificate information:
Common Name:
Organization: Google Inc
Locality: Mountain View
State or Province: California
Country: US
Common Name: Google Internet Authority
Organization: Google Inc
Country: US
Activation time: St 16. únor 2011, 05:38:09 CET
Expiration time: Čt 16. únor 2012, 05:48:09 CET
SHA1: DB:A0:2A:07:00:F9:E3:23:7D:07:E7:52:3C:95:9D:E6:7E:12:54:3F
MD5: 02:4C:12:F3:37:1F:0C:C1:EB:10:4B:92:F7:F1:E0:DF
SIZE 35882577:
Maximum message size is 35882577 bytes = 34,22 MiB
Support for TLS encryption via the STARTTLS command
Supported authentication methods:

MSMTP Configuration

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 --version command.

$ 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 
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# Logging - uncomment either syslog or logfile, having both uncommented disables logging at all.
#syslog on
# Or to log to log own file
#logfile /var/log/msmtp.log

keepbcc on

# Gmail/Google Apps (configure as may as you want)
account gmail
port 587
from your@gmail
user your@gmail
password secretPassword

# 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).

In /etc/msmtprc or your user configuration file add/change key

  • logfile /var/log/msmtp.log or whatever file you wish to log into dedicated file OR
  • syslog on to enable syslog logging. Default facility is LOG_USER or specify with syslog .

MSMTP logs to syslog but we want to tweak it to log on startup to /var/log/msmtp.log instead.
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

Open file /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 '' to '': 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

Tryout MSMTP

Always use -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:

To: <username>
Subject: A test

Yadda, yadda, yadda.

And execute cat testmail | msmtp

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. tail /var/log/msmtp.log

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

Mail command

Many scripts assume existence of mail command. For Ubuntu it could be installed via heirloom-mailxpackage.

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