As stated earlier this site is now running on a DigitalOcean droplet. A droplet is basically the same as having a “real server”, and when running a bare bones machine, it isn’t born with the ability to handle email – receiving nor sending. As a number of web apps require the ability to handle mail, I had to setup facilities on the server (or droplet) to handle mail.
The “default” way to do this would probably be to install sendmail or postfix, as they are full-featured mail server, but configuring a mail-server, keeping it secure and updated is a nightmare I’d like to avoid. Therefore it was time to look for another option.
msmtp is an open-source, light-weight solution, which allows you to get your server to send email, or as the project itself describes it:
In the default mode, it transmits a mail to an SMTP server (for example at a free mail provider) which takes care of further delivery.
– msmtp project homepage
There are several ways msmtp can be setup, but in this post I’ll just cover the two basic scenarios.
msmtp can handle mail delivery different ways. I’ll just cover two basic scenarios here.
If you have a smtp-server available. Your hosting provider or someone else may provide you with access to a full-featured SMTP-server. If this is the case, you can configure msmtp to pass all mail on to that server like this:
# smtp server configuration
# Default account to use
account default : smtp
As you’re talking to a “real” SMTP server all options and features should (potentially) be available to you.
If you have a Google account - either a regular Gmail account or Google Apps account will do just fine. To configure msmtp to use the Gmail SMTP server use this configuration:
# Gmail/Google Apps
# Default account to use
account default : gmail
In the above example you need to change “email@example.com” to an actual GMail account, and you need to change “enter-password-here!” to the password belonging to the specified Gmail addresss.
Using Gmail, all mail passed on from msmtp, will be sent from the account credentials used in the configuration, and there doesn’t seem to be a way to override this. You may therefore opt to create a specific mail-account for this use. You can set a custom Reply-To header in the mails passed through Gmail SMTP, which in many cases may help secure the replies get to a proper recipient.
If your site has adopted DMARC, this may not be a suitable option (at least not on the free tier), as they don’t support signing and do not offer dedicated IP-addresses for you SPF-records.
Testing 1, 2, 3…
Once you’ve set up the mstmp configuration file, it’s time to do some testing. Create at text file called “testmail.txt” with this content:
Subject: Subject for test mail
This is the body content for the test mail.
Change firstname.lastname@example.org to your own actual email address. Then enter from the command line:
cat testmail.txt | msmtp email@example.com
You should recieve your test mail shortly.
Setting up an alias
Many unix/linux tools and apps seems to assume, that you have sendmail installed and that it is available at /usr/bin/sendmail or a few other locations in the file system. To handle these cases easily, you can create an alias pointing the sendmail name to the msmtp binary like this (the examples should cover most cases):
ln -s /usr/bin/msmtp /usr/sbin/sendmail
ln -s /usr/bin/msmtp /usr/bin/sendmail
ln -s /usr/bin/msmtp /usr/lib/sendmail
Depending on which package manager your installation use, it may automatically setup these aliases, so do check if they exist before trying to create them.
Setting up with PHP
if you made the aliases as suggested above, it may already work, but you should make the following changes, just keep things clean and transparent.
Find all php.ini files applicable (you probably have one for the web-server and another for the Command Line):
Add or change the line:
sendmail_path = "/usr/bin/msmtp -t"
Now for some testing. Add a file with the following content (change the example-address to your own):
<!--?php mail("firstname.lastname@example.org","test","test","-email@example.com"); ?-->
Now, call the file from the command line using the php cli, and then call the file through the webserver. In both cases you should receive an email shortly.
Apart from running sendmail or postfix, there also seems to an application similar to mstmp called ssmtp, which offers many of the same features as msmtp.