Scott Boms

MSMTP and Virtual Hosts

Last year when I originally moved the Wishingline site and a handful of others over a shiny new slice at Slicehst one of the issues I ran into was handling outgoing mail from contact forms, Movable Type, etc. I’m no server admin and despite knowing enough to be dangerous, setting up a secure mail server that can handle multiple domains was definitely outside my comfort zone.

Thanks to Ethan, I discovered a gem of an open source project called MSMTP which was just what I needed; the exception being that I couldn’t figure out how to use it with multiple domains. Until last week that is.

Of course it’s really easy.

Installing and Configuring for Multiple Domains

MSMTP provides two ways you can configure the software using a simple and well-documented configuration file format. It’s all plain text so it’s easy to create, edit and back up.

Installing the Software

Installing MSMSTP requires the following packages which can be installed using the aptitude tool on Ubuntu. Installation on other *nixes may vary.


$ aptitude install msmtp msmtp-mta ca-certificates
Configuration

Once you have everything installed, you need to create a configuration file either in /etc/msmtprc or by creating a user-specific one in your user’s home directory. If you need mail services for more than one domain, I suggest using the global configuration option.

I’m going to assume you’re reasonably comfortable working in a Unix environment from here on out though if you know what you’re doing you can do all of this just as easily using ExpanDrive and TextMate without having to touch the Terminal.


$ sudo nano /etc/msmtprc

Once the nano editor has opened a new blank file for you, enter the following and replace the example configuration as needed. I’m including examples for two domains so you get the idea.


# Account: domain1.com
account         domain1
host            smtp.gmail.com
port            587
auto_from       off
auth            on
user            hello@domain1.com
password        PASSWORD
tls             on
tls_starttls    on
from            robot@domain1.com
maildomain      domain1.com
tls_trust_file  /etc/ssl/certs/ca-certificates.crt
logfile
syslog          LOG_MAIL

# Set a default account to use
account default : domain1

# Account: domain2.com
account         domain2
host            smtp.gmail.com
port            587
auto_from       off
auth            on
user            hello@domain2.com
password        PASSWORD
tls             on
tls_starttls    on
from            robot@domain2.com
maildomain      domain2.com
tls_trust_file  /etc/ssl/certs/ca-certificates.crt
logfile
syslog          LOG_MAIL

Repeat as necessary to add more domains. Save your changes by typing Control-O and pressing Enter. Then type Control-X to exit the editor.

Virtual Host Configuration

Assuming you’re using PHP with Apache as your web server, you can add the last two lines in the example below to each virtual host to specify which configuration account you’d like to use to send mail.


<VirtualHost *:80>
  ServerAdmin webmaster@domain1.com
  ServerName domain1.com
  DocumentRoot /home/user/sites/domain1/
  DirectoryIndex index.html index.php

  # MSMTP configuration for this domain
  php_admin_value sendmail_path "/usr/bin/msmtp -a domain1 -t"
</VirtualHost>

Replace domain1 with the correct domain obviously. This should correspond to the account names specified in the /etc/msmtprc file.

Alternatively you need to instruct your middleware or framework to use MSMTP instead of Sendmail/Postfix to send mail and pass the same account parameter whenever called. Most have some form of configuration option to allow this.

That’s it.