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
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
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 firstname.lastname@example.org password PASSWORD tls on tls_starttls on from email@example.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 firstname.lastname@example.org password PASSWORD tls on tls_starttls on from email@example.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 firstname.lastname@example.org 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>
domain1 with the correct domain obviously. This should correspond to the account names specified in the
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.