Skip to navigation

Mutt with msmtp and a mail queue

If you read and send a lot of e-mail, you want to use the mutt mail client. If you travel a lot, you want to be able to compose e-mails offline and then send them off when you’re back online. Mutt has always insisted on being a pure mail user agent (MUA), not a mail transport agent (MTA), so it doesn’t handle a mail queue. Fine.

On a Linux system, you usually have a fully functional MTA like exim or postfix, and those can handle mail queues. However, without additional tweaking, sending mail using these MTAs will always carry the stigma of originating from a non-fixed IP without a valid domain name if you use them on your laptop. Besides, full-fledged MTAs should really be maintained carefully if you allow sending mail to outside IP addresses. So most likely, you’ll want to go with something more low-key, like a lightweight SMTP client that just authenticates with a “real” SMTP server (like that of your ISP) and lets that outside server send the mail. One such nice client is msmtp. Msmtp is really easy to configure and has the benefit of handling multiple accounts. A starting ~/.msmptrc file could look as simple as:

account default 
auth plain	 
user your_username 
password your_passwd
tls_trust_file  /etc/ssl/certs/Thawte_Premium_Server_CA.pem 
logfile ~/.msmtprc-log

And correspondingly, in your ~/muttrc, you would put:

set sendmail="/usr/bin/msmtp -a default"

The problem is, msmtp doesn’t actually handle mail queues. While you’re offline, you can still compose messages in mutt and keep adding them to the “postponed” folder, but when you’re back online, you’ll have to manually send each of these messages individually, which is not good.

When I googled for a solution I found that Chris Gianniotis had actually written two shell scripts to handle this situation, however, links to files of the scripts that I found via Google were defunct. The only instance I could actually find of these scripts is in this archived message of the msmtp-users mailing list. (The link is a post by the autor of the scripts, and is recommended reading for more information). (Addendum: See comment #1 for official location of scripts).) However, some of the long comment lines in the scripts are interrupted by linebreaks on that URL, so I had to remove them manually. To spare you the pain of doing that yourself, I have uploaded the two scripts here in a slightly modified version:

My modifications are:

  • Cleaned up comments from version found online, i.e. removed accidental line breaks
  • Modified location of queue directory and log file to suit my own needs: you are supposed to adjust them to your own needs anyway.
  • Changed the invoked shell from #!/bin/sh to #!/bin/bash, since msmtpq otherwise kept hiccuping on the declare -i CNT bit (then I changed it for both scripts)

The way this works is:

  1. Put the two scripts somewhere you like, say into ~/bin and make them executable, e.g:
    cd ~/bin
    chmod 744 msmtpQ msmtpq
  2. If needed, modify the mail queue and log file locations in each script, near the top.
  3. Create the mail queue directory (the script will not create it). If you haven’t changed what it is:
    mkdir ~/.msmtp.queue
  4. In your ~/.muttrc,
    set sendmail="~/bin/msmtpQ"

    (adjust path if you put the scripts somewhere else).

All set. You should now be able to send mails when you’re online. When you’re offline, sending the mails actually moves them to the queue. When you’re back online, you say:

msmptq -r

to send the mail out. (Type msmtpq to see more options, like deleting messages from the queue).

Optionally, the author of the scripts also recommends setting

set sendmail_wait= -1

in your ~/.muttrc. I didn’t know about this, and it’s great in general — mutt backgrounds sending the mail, which means you don’t wait forever for large attachments to go out, or on a slow connection. You can of course use this with regular .msmtp as well, or with any other MTA. However, be warned: Even though the the mutt manual claims that “if there is some error, you will be informed as to where to find the output,” this didn’t happen when I tested it (using just regular msmtp). I changed my password in the ~/.msmptrc to an incorrect one, but mutt happily backgrounded the msmtp process and never complained. So I’d recommend setting a logfile in ~/.msmtprc and checking that for errors from time to time. (Addendum: See the next post for a script that checks the error logs for you.


To make it even easier to send queued mail, I’ve put together a small script that incorporates the different msmtpq options: You get a quick overview over all queued messages, can either send them all at once, delete individual or all messages.

6 Responses to “Mutt with msmtp and a mail queue”

  1. Prasinos Says:

    The “official” scripts can be found in the cvs repo of msmtp.
    See this post for the links:

  2. Marianne Says:

    Prasinos, thanks for the info.

  3. K. Callis Says:

    Is your sendq script a replacement for any of the msmtpQ and/or msmtpq?

  4. Marianne Says:

    #3: No, the sendq is not a replacement for either msmtpq or msmtpQ. It runs msmtpq. It is just a crutch because I could never remember the msmtpq options. So it simply displays help and offers several options of what to do with queued mail. (Note I haven’t been using this mail queue setup for a while. My current second-hand laptop sometimes drops the connection; mail would silently go to the queue, and I would think it had gone out).

  5. jm Says:

    In the script of the addendum I think you should replace:
    if [ -z “$(/usr/bin/find /home/mpromber/.msmtp.queue/ -iname ‘*mail’ -print0)” ]

    if [ -z “$(/usr/bin/find ~/.msmtp.queue/ -iname ‘*mail’ -print0)” ]

    Thank you very much for your wor!

  6. Marianne Says:

    Thanks. You’re right, yours is the version that works for other usernames, too. I have no time to change it right now though.