Title : Sending mail with mu4e
Author: Solène
Date  : 22 May 2018
Tags  : unix emacs

In my article about mu4e I said that I would write about sending mails
with it. This will be the topic covered in this article.

There are a lot of ways to send mails with a lot of differents use
cases. I will only cover a few of them, the documentation of mu4e and
emacs are both very good, I will only give hints about some
interestings setups.

I would thank Raphael who made me curious about differents ways of
sending mails from mu4e and who pointed out some mu4e features I
wasn't aware of.


## Send mails through your local server

The easiest way is to send mails through your local mail server (which
should be OpenSMTPD by default if you are running OpenBSD). This only
requires the following line to works in your *~/.emacs* file:

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

Basically, it would be only relayed to the recipient if your local
mail is well configured, which is not the case for most servers. This
requires a reverse DNS address correctly configured (assuming a static
IP address), a SPF record in your DNS and a DKIM signing for outgoing
mail. This is the minimum to be accepted to others SMTP
servers. Usually people send mails from their personal computer and
not from the mail server.


### Configure OpenSMTPD to relay to another smtp server

We can bypass this problem by configuring our local SMTP server to
relay our mails sent locally to another SMTP server using credentials
for authentication.

This is pretty easy to set-up, by using the following
*/etc/mail/smtpd.conf* configuration, just replace remoteserver by
your server.
  
    table aliases file:/etc/mail/aliases
    table secrets file:/etc/mail/secrets
  
    listen on lo0
  
    accept for local alias <aliases> deliver to mbox
    accept for any relay via secure+auth://label@remoteserver:465 auth <secrets>

You will have to create the file */etc/mail/secrets* and add your
credentials for authentication on the SMTP server.

From smtpd.conf(5) man page, as root:

    # touch /etc/mail/secrets
    # chmod 640 /etc/mail/secrets
    # chown root:_smtpd /etc/mail/secrets
    # echo "label username:password" > /etc/mail/secrets

Then, all mail sent from your computer will be relayed through your
mail server. With 'sendmail-send-it, emacs will delivered the mail to
your local server which will relay it to the outgoing SMTP server.


## SMTP through SSH

One setup I like and I use is to relay the mails directly to the
outgoing SMTP server, this requires no authentication except a SSH
access to the remote server.

It requires the following emacs configuration in *~/.emacs*:

    (setq
      message-send-mail-function 'smtpmail-send-it
      smtpmail-smtp-server "localhost"
      smtpmail-smtp-service 2525)

The configuration tells emacs to connect to the SMTP server on
localhost port 2525 to send the mails. Of course, no mail daemon runs
on this port on the local machine, it requires the following ssh
command to be able to send mails.

    $ ssh -N -L 127.0.0.1:2525:127.0.0.1:25 remoteserver

This will bind the port 127.0.0.1:25 from the remote server point of
view on your address 127.0.0.1:2525 from your computer point of view.

Your mail server should accept deliveries from local users of course.


## SMTP authentication from emacs

It's also possible to send mails from emacs using a regular smtp
authentication directly from emacs. It is boring to setup, it requires
putting credentials into a file named *~/.authinfo* that it's possible
to encrypt using GPG but then it requires a wrapper to load it. It
also requires to setup correctly the SMTP authentication. There are
plenty of examples for this on the Internet, I don't want to cover it.


## Queuing mails for sending it later

Mu4e supports a very nice feature which is mail queueing from smtpmail
emacs client. To enable it, it requires two easy steps:

In *~/.emacs*:

    (setq
      smtpmail-queue-mail t
      smtpmail-queue-dir "~/Mail/queue/cur")

In your shell:

    $ mu mkdir ~/Mail/queue
    $ touch ~/Mail/queue/.noindex

Then, mu4e will be aware of the queueing, in the home screen of mu4e,
you will be able to switch from queuing to direct sending by pressing
`m` and flushing the queue by pressing `f`.

Note: there is a bug (not sure it's really a bug). When sending a mail
into the queue, if your mail contains special characters, you will be
asked to send it raw or to add a header containing the encoding.