Ariel Costas

Mu4e: set the correct date when sending email

By Ariel Costas - October 31, 2021

If you use mu4e as your emacs-based email client, you might have come across a problem when sending email: the date is wrong. Let me give you an example: I start composing a message to someone at 10:28:12, and send it by 10:36:42. When that person opens their inbox, the message appears to be sent at 10:28, when I sent it at 10:36.

This is not the desired behavior: if I send an email at a certain time, the date should say that time, and not when I started composing it. If I start writing an email on Monday night, and send it on Tuesday, I’d like the recipients to see it was sent on Tuesday (thus one of the most recent), and not the previous day.

Some backstory

This wasn’t always like this. In 2014, a user on mu’s GitHub repository opened an issue explaining this problem, and it was fixed on this commit. However, if we visit that file nowadays, we find that variable mu4e-compose-auto-include-date as deprecated since version 1.3.5, which I could find no release noted for.

What I did then is clone the repository and run a git blame, to see when it was changed. And found out it was on this commit from 2019, with no apparent reason and saying it got updated when saving or sending (which wasn’t the case for me, it showed the previous date).

So, the only way of making this correct was non-existent, so I continued looking for a solution. At first, that was adding a hook to mu4e-compose, so when I started composing a message it would run flush-lines "^Date", which would delete the Date header. This worked only when starting to write a message, but not when forwarding or replying to one.

Gnus message-mode

After this disaster, I decided to try and debug it (with my poor Elisp knowledge), so I went to an email “headers” list in mu4e and checked which function was called when replying, which I found out to be:

mu4e-compose-reply is an interactive compiled Lisp function in
‘mu4e-compose.el’.

It is bound to R, <menu-bar> <headers> <reply>.

(mu4e-compose-reply)

Compose a reply for the message at point in the headers buffer.

It is a compiled function, so I can’t read it directly from emacs, and had to check it on the Git repository. So I visited that file and found that function. I couldn’t find nothing that was of use for me, so I was about to close the file when I looked a hundred lines below and found this lines

(define-mail-user-agent 'mu4e-user-agent
  'mu4e~compose-mail
  'message-send-and-exit
  'message-kill-buffer
  'message-send-hook)

This means, mu4e reuses Gnus' message-mode for many things, like the message-send-mail-hook which runs before sending a message. That was what I was looking for: a way of replacing the header before sending my message. So I went ahead and wrote the first idea I had, which was:

(add-hook 'message-send-mail-hook (lambda () (flush-lines "^Date")))

What this does is, before sending an email, it deletes the lines starting with “Date”, that will be then added again with now’s date.

This isn’t even by far the best solution, as it depends in whether the date will be added later, which does for now, but maybe not in the future. Also, if you have a line starting with “Date” on your text (in the middle of a sentence, for example), that whole line would get wiped too.

So, instead of “^Date” it should run a search for lines starting with “Date: " and a date formatted in RFC 2822 Section 3.31, which can be also found running date -R (this is not POSIX-standard). And replace that line with “Date: " + the result of message-make-date, which is used by Mu4e to generate RFC-compliant dates2.

Conclusion

I found a workaround, but found some problems with it. It works for me, but could potentially give problems in the future. Does anyone else have a workaround for this problem? If so, please email me your solution to ariel at costas dot dev.

Happy hacking!


  1. https://datatracker.ietf.org/doc/html/rfc2822#section-3.3 ↩︎

  2. https://github.com/djcb/mu/blob/master/mu4e/mu4e-draft.el#L607 ↩︎

#emacs #email #software