Menu

Using postfix instead of smtpmail to send email in mu4e

The standard setup for mu4e uses smtpmail to actually send the messages. This works fine for the most part, but by default it does not run asynchronously, so emacs pauses when a mail is sent. Usually this is very brief, but for messages with large attachments it can be very noticeable. This was particularly annoying to me when I started developing a package to add a two minute delay to all outgoing mail to give an “undo send” functionality (I’ll post more about this package soon). When the two minute timer finished and the mail was sent, I would be working on something else, and emacs would hang briefly, which was a bit annoying.

It is possible to configure mu4e to send the mail asynchronously, but apparently this can be unreliable. A better solution is to run your own mail server to handle outgoing mail. This is supported in mu4e, and mu4e hands off the mail to the server to send, so it happens instantly from the user’s point of view, regardless of the size of the mail. Another benefit is that mail sent while you are offline is handled automatically; the server queues the mail and then sends it when you reconnect.

I am using OS X Yosemite, and setting up a mail server was quite easy. The postfix mail server is installed by default on OS X, so it was just a matter of configuring it to work with my gmail account. I followed these helpful instructions, noting the advice in the comments that for Yosemite, the extra line

smtp_sasl_mechanism_filter = login

is needed.

Now we tell mu4e to use postfix to handle sending mail by adding the following to our emacs config file:

(Note the references to “sendmail” are because this was the name of one of the original mail servers on unix/linux. Postfix is a more modern mail server that is compatible with sendmail.)

This should be all you need to switch to using postfix to send your mail. Some useful commands (to run at the command line, not using M-x in emacs) are

sudo postfix start
sudo postfix stop

to start and stop the server, and

mailq

to list the current queue of unsent mail. This will generally be empty, but if you have been offline you might see some mails in there. They should send automatically once the server notices you are back online, but if not, use

mailq -q

to send all mail in the queue. See the man pages for more functionality.

So there you are. This might not be for everyone, but if you don’t mind fiddling with some config files, I highly recommend moving over to postfix for sending your mail.

One comment: “mailq -q” doesn’t seem to do anything for me, but “postqueue -f” seems to have the desired effect.

Titus von der Malsburg

Great post, thank you! One benefit of synchronous sending is that I can be absolutely certain that an email was actually sent. With the postfix approach, I have to open a shell and check, which means that I don’t save that much time. I would be great to have a more convenient way to monitor whether a message has been sent. Perhaps a temporary buffer (like Mu4e’s update buffer) which shows the mail queue until it’s empty?

Edit: Or even better an indicator showing the number of unsent messages in the mode line.

Hi, here is the script I use to monitor my mail counts. I reports number of unread in inbox, inbox total, drafts, and number in mailq (added following your suggestion – thanks). The number in drafts is useful if you use mu4e-delay. I run in using shellwrangler to keep a count in my menubar in OS X.

For what it’s worth on OS X this script didn’t work for the number in mailq, and I wasn’t quite sure how it was supposed to (bearing in mind that I know nothing about regexps, grep or perl).

However, replacing the use of grep with:

grep /[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/

seems to work fine (i.e. counting the timestamps).
Thanks for the script, and the idea of using shellwrangler (which I hadn’t heard of – unfortunately it seems to be a little hard to get hold of now, as their website has been down for a while).

Ah, I see – for whatever reason on my system (running Yosemite), there are 11 hex characters at the start of mailq lines, it seems. Thanks for clarifying (and for the bitbar link, and for everything else on this blog, which I’m finding very helpful!).

svsmailus

How does postfix work if you have multiple email addresses? I’ve set up postfix, but the smtp is gmail. What needs to be done to get this to work with more than one sending smtp?