GuidesUsing msmtp as a Lightweight SMTP Client

Using msmtp as a Lightweight SMTP Client

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Last week, I covered OfflineIMAP to sync IMAP for use with Mutt or other applications. Continuing with that theme, I want to talk about another tool I use with Mutt to handle the opposite problem — actually pushing mail to an SMTP server. Let’s take a look at msmtp.

Need a quick and dirty replacement for Sendmail or Postfix? Running Mutt and don’t want to set up a full-blown MTA? Consider msmtp, a lightweight SMTP client with a Sendmail-compatible interface.

Mutt is a fantastic mailer for admins, but it doesn’t do SMTP. It expects that to be done by an Mail Transport Agent (MTA). Usually, this is something like Sendmail or Postfix, and that makes sense when they’re living on the same system. But it comes to running Mutt on a laptop or desktop, many admins have better things to do with their time than to set up a full-blown MTA. You may not even be running your own mail services in-house, so why dust off the Sendmail or Postfix documentation just to set up a personal MTA?

Actually, you can configure msmtp on the user level or the system level — so if you want to set up a very lightweight Sendmail replacement for a system, msmtp should do the job.

Setting Up msmtp

Most Linux distributions should have packages for msmtp. If not, it’s available under the GPLv3 (or later) and available on Sourceforge.

Once you’ve installed it, you must set up a ~/.msmtprc file. The project has an example configuration to start from. I’ll share mine, which I use to transfer mail to Gmail’s SMTP servers.

defaults 
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/Dropbox/.msmtp.log

account name
host smtp.gmail.com
port 587
protocol smtp
auth on
from email@domain.tld
user email@domain.tld
password XXXXXX

The defaults section contains program defaults for msmtp that can be used across accounts. That’s another nifty feature you get from msmtp as opposed to Postfix, Exim, or Sendmail — msmtp is easy to configure with multiple accounts.

Each account section has a name, and you’ll want to add the host and port information. In the case of Gmail (and Google Apps for your Domain), you’ll use smtp.gmail.com and 587, respectively. The protocol is, of course, SMTP, and you want to turn authentication on. From and user are the same in my case, but they may vary for you.

And, finally, the password. You can include a plain text password — but I don’t recommend it on multiuser systems. You can also use the passwordeval directive instead of a password, and store a password in an encrypted file.

If you’re using msmtp with Mutt, you must tell Mutt about it as well. Also make sure that your ~/.mstmprc is set with read/write permissions for your user only. (chmod 600 ~/.mstmprc) If it’s set incorrectly, it will error out when trying to send mail from Mutt.

To tell Mutt about it, you’ll need to add (or adjust) a few lines in your ~/.muttrc:

set realname='Your Name'
set from=email@domain.tld
set sendmail="/usr/bin/msmtp"
set envelope_from=yes

You might have msmtp set to a different path, depending on how it’s installed on your system. Otherwise, Mutt should now be ready to send mail using msmtp.

Why use msmtp instead of Postfix? My friend Paul Frields recommends it for use with Mutt, and it provides a step-by-step on how to use it. It is, however, a heck of a lot of steps. For my needs, and a lot of others’, Postfix is overkill. It does have some advantages, though. In particular, sending to Postfix is fairly quick, in contrast to the very short lag I experience with msmtp, while it’s sending to my SMTP server. The Postfix method is also good if you want to process a lot of mail on a flight, as Frields’ method will just queue mail and send it next time there’s a network connection. I spend a lot less time on the road these days, though, so I can tolerate a few hours without sending mail — assuming the flight I’m on doesn’t have Wi-Fi anyway.

The disadvantages to this approach? As noted, Postfix requires a much more involved setup. This also means that it’s difficult to share a configuration between machines. Once I set up msmtp for my laptop, it was easy to copy the configuration over and use it on my desktop machine as well.

I’ve been using mstmp for a while now, and I haven’t really thought about it much since I set it up initially. Writing this tutorial was the first time I needed to actually peek at the configuration again. Next time you need a quick and dirty replacement for Sendmail or Postfix, give it a shot. The documentation is quite good and it’s much faster to configure than a full-blown SMTP server.

Joe ‘Zonker’ Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at jzb@zonker.net and follow him on Twitter.

Follow ServerWatch on Twitter

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories