Ivan Tomica

Sending mail using multiple Mu4e contexts in Emacs

My current email client of choice is Mu4e within Emacs. There’s plethora of reasons why I have settled for it instead of some other client and why in the end, I transitioned to it from mutt, but that’s perhaps a topic for some future article.

Setting up Mu4e in the beginning was pretty simple as I had only one email account and used asynchronous email function to queue emails and then flush them all in one go. Whole asynchronous process relied on using smtpmail-send-it function.

Unfortunately, I had to ditch the whole process of queuing email and sending it asynchronously due to the fact that I had to configure multiple accounts in Mu4e.

Mu4e configuration

Setting up email account with context is pretty straight-forward. For simplicity sake here’s an example with single account context, to add one more, you’d basically only copy/paste ,(make-mu4e-context part N times

(setq mu4e-contexts
      `(,(make-mu4e-context
          :name "Private"
          :match-func (lambda (msg) (when msg
                                      (string-prefix-p "/Private" (mu4e-message-field msg :maildir))))
          :vars '(
                  (user-full-name . "Ivan Tomica")
                  (user-mail-address . "MYEMAIL")
                  (mu4e-compose-signature . (concat
                                             "Ivan Tomica\n"
                                             "https://www.tomica.net\n"))
                  (mu4e-sent-folder . "/Sent")
                  (mu4e-drafts-folder . "/Drafts")
                  (mu4e-trash-folder .  "/Trash")
                  (mu4e-refile-folder . "/Archive")
                  (mu4e-maildir-shortcuts . (("/INBOX" . ?i)
                                             ("/Archive" . ?a)
                                             ("/Sent" . ?s)
                                             ("/Trash" . ?t)
                                             ("/Junk" . ?j)))))))

That’s basically enough for reading, searching and managing messages locally.

The problem starts with sending email.

If you’re familiar with mu4e you know it is just an interface for mu, meaning it has no sending capabilities, instead, you’re using smtpmail-send-it function which is configured with following variables

(setq smtpmail-smtp-server "MAILSERVERADDRESS"
      smtpmail-stream-type 'starttls
      smtpmail-smtp-service 587)

But as you can see, there’s no way to switch those easily depending on which account you use for sending email.

My first instinct was to just configure those within the mu4e context, but unfortunately, for some weird reason, that didn’t work. Setting up variables works just fine, but when you try to send the message, smtpmail-send-it function fails with no clear explanation why. My attempts to debug it were, unsuccessful to say the least…

So, after two days of banging my head against the wall and trying multiple approaches and hacks I finally decided to transition to msmtp. Configuring Emacs was easy as

(setq sendmail-program "/usr/bin/msmtp"
      message-sendmail-f-is-evil t
      message-sendmail-extra-arguments '("--read-envelope-from")
      send-mail-function 'smtpmail-send-it
      message-send-mail-function 'message-send-mail-with-sendmail)

msmtp

And configuring .msmtprc was even simpler

defaults
auth  on
tls   on
tls_trust_file /etc/ssl/certs/ca-bundle.crt
logfile ~/.cache/msmtp.log

# Private
account MYACCOUNT
host    MYSERVER
port    587
from    MYEMAIL
user    MYEMAIL
passwordeval "secret-tool lookup MYEMAIL password"

account default : MYACCOUNT

And there’s also native way to use GNOME Keyring, so instead of using passwordeval command as I do, you could use something similar to what is mentioned in Arch Wiki

secret-tool store --label=msmtp host smtp.your.domain service smtp user yourusername

and omit password and passwordeval options all together.

Tagged in:, , ,
About 
Sysadmin on the everlasting journey of learning.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *