Today I learned very elegant trick to send e-mail asynchronously and reliably from Emacs. The example code is for gmail, but I’m sure it can be adapted to any SMTP server.

My previous setup was using just straight out smtpmail like this:

(setq message-send-mail-function 'smtpmail-send-it
      starttls-use-gnutls t
      smtpmail-starttls-credentials
      '(("smtp.gmail.com" 587 nil nil))
      smtpmail-auth-credentials
      (expand-file-name "~/.authinfo.gpg")
      smtpmail-default-smtp-server "smtp.gmail.com"
      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587
      smtpmail-debug-info t)

It works out nicely, but when the message is large, the whole Emacs just blocks. There are solutions, like using smtpmail-async, but it has its own problems.

Local SMTP-server to rescue! Yes, I installed postfix and configured it to use gmail as relay. Emacs configuration also became much simpler:

(setq send-mail-function 'sendmail-send-it)
(setq message-send-mail-function 'message-send-mail-with-sendmail)

Now the message sending is immediate, and the postfix relay can handle things like network outages etc. very efficiently.

These are the relevant portions of postfix configuration in /etc/postfix/main.cf:

myhostname = smtp.gmail.com
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_password
smtp_use_tls = yes
smtp_sasl_security_options = noanonymous

Contents of /etc/postfix/sasl_password:

[smtp.gmail.com]:587 email_address@gmail.com:password

Compile sasl database:

$ sudo postmap sasl_password

On Fedora, you can debug postfix with journalctl:

$ sudo journalctl --unit=postfix