Jay Taylor's notes
back to listing indexmsmtp - ArchWiki
[web search]msmtp
Related articles
msmtp is a very simple and easy to use SMTP client with fairly complete sendmail compatibility.
1Installation
Install the msmtp package. Additionally, install msmtp-mta, which creates a sendmail alias to msmtp.
2Basic setup
Since msmtp version 1.8.6 you can place your user configuration either at ~/.msmtprc
or $XDG_CONFIG_HOME/msmtp/config
. The following is an example of a msmtp configuration (the file is based on the per-user example file located at /usr/share/doc/msmtp/msmtprc-user.example
; the system configuration file belongs at /etc/msmtprc
and its corresponding example file is located at /usr/share/doc/msmtp/msmtprc-system.example
).
- Use OAuth, via #OAuth2 Setup.
- If you use two factor authentication: create an app password.
The user configuration file must be explicitly readable/writeable by its owner or msmtp will fail:
To avoid saving the password in plain text in the configuration file, use passwordeval to launch an external program, or see the #Password management section below. This example using Gnu PG is commonly used to perform decryption of a password:
gpg --encrypt -o .msmtp-gmail.gpg -r <email> -
. The ending dash is not a typo, rather it causes gpg to use stdin. After running that snippet of code, type in your password, press enter, and press Control-d so gpg can encrypt your password.1 "." counter(h3-section)OAuth2 Setup
OAuth2 can be used to securely authenticate msmtp when basic username/password authentication is unsupported by the site configuration or otherwise undesirable.
1 "." counter(h3-section) "." counter(h4-section)oama
msmtp alone lacks the ability to renew or authorize OAuth2 credentials. A comprehensive solution is using the oama utility which provides IMAP/SMTP clients with renewal capabilities and authorization of OAuth2 credentials.
To use oama, install oama-binAUR and configure msmtp to use it:
Access token renewal happens automatically in the background transparent to the user.
3Using the mail command
To send mails using the mail
command you must install the package s-nail, which also provides the mailx
command. You will also need to provide a sendmail
-compatible MTA, either by installing msmtp-mta (which symlinks sendmail
to msmtp
) or by editing /etc/mail.rc
to set the sendmail path:
A .msmtprc
file will need to be in the home of every user who wants to send mail or alternatively the system wide /etc/msmtprc
can be used.
msmtp also understands aliases. Add the following line to the defaults section of msmtprc or your local configuration file:
and create an aliases file in /etc
4Test functionality
The account option (--account=,-a
) tells which account to use as sender:
Or, send both a subject and a body:
Or, with the addresses in a file:
--read-envelope-from
instead of -a default
to automatically choose account by the From: field in a message you are going to send. You can also use --read-recipients,-t
to read recipient addresses from the To, Cc, and Bcc headers of the mail in addition to the recipients given on the command line.5Cronie default email client
To make Cronie use msmtp rather than sendmail, make sure msmtp-mta is installed, or edit the cronie.service
systemd unit:
Then you must tell cronie or msmtp what your email address is. One way to accomplish this is by adding it to the msmtp configuration:
- Add to
/etc/msmtprc
: - Create
/etc/aliases
:
Alternatively, you can add it directly to the crontab:
- Add a
MAILTO
line to the crontab: - Add a
MAILFROM
line to the crontab:
- The last part is necessary to prevent this error:
6Password management
Passwords for msmtp can be stored in plaintext, encrypted files, or a keyring.
2 "." counter(h3-section)GNOME Keyring
Storing passwords in GNOME Keyring is supported natively in msmtp. Setup the keyring as described on the linked wiki page and install libsecret. Then, store a password by running:
msmtp should now find the password automatically.
3 "." counter(h3-section)GnuPG
The password
directive may be omitted. In that case, if the account in question has auth
set to a legitimate value other than off
, invoking msmtp from an interactive shell will ask for the password before sending mail. msmtp will not prompt if it has been called by another type of application, such as Mutt. For such cases, the --passwordeval
parameter
can be used to call an external keyring tool like GnuPG.
To do this, set up GnuPG, including gpg-agent to avoid having to enter the password every time. Then, create an encrypted password file for msmtp, as follows. Create a secure directory with 700
permissions located on a tmpfs to avoid writing the unencrypted password to the disk. In that directory create a plain text file with the mail account password. Then, encrypt the file with your private key:
Remove the plain text file and move the encrypted file to the final location, e.g. ~/.mail/.msmtp-credentials.gpg
. In ~/.msmtprc
add:
Normally this is sufficient for a GUI password prompt to appear when, for example, sending a message from Mutt. If gpg prompt for the passphrase cannot be issued, then start the gpg-agent before. A simple hack to start the agent is to execute a external command in your muttrc using the backtick `command`
syntax. For example, you can put something like the following in your muttrc:
Mutt will execute this when it starts, gpg-agent will cache your password, msmtp will be happy and you can send mail.
An alternative is to place passwords in ~/.netrc
, a file that can act as a common pool for msmtp, OfflineIMAP, and associated tools.
4 "." counter(h3-section)pass
You may store your credentials inside of the pass password manager.
If you are using your main password (which is customarily stored in the first line of your pass file) to login into your SMTP server, you can add the following to your .msmptrc
:
If you are using Gmail, and have set up an app password, the following configuration will suit you better.
Save your app password inside your pass
password file, but with a msmtp:
prefix:
Then add the following to your .msmptrc
:
In either case, trying to send an email with msmtp will trigger pass
, which may ask you for your pass
master password if you have not entered it recently.
7Miscellaneous
5 "." counter(h3-section)Using msmtp offline
Although msmtp is great, it requires that you be online to use it. This is not ideal for people on laptops with intermittent connections to the Internet or dialup users. Several scripts have been written to remedy this fact, collectively called msmtpqueue.
The scripts are installed under /usr/share/doc/msmtp/msmtpqueue
. You might want to copy the scripts to a convenient location on your computer, (/usr/local/bin
is a good choice).
Finally, change your MUA to use msmtp-enqueue.sh instead of msmtp when sending e-mail. By default, queued messages will be stored in ~/.msmtpqueue
. To change this location, change the QUEUEDIR=$HOME/.msmtpqueue
line in the scripts (or delete the line, and export the QUEUEDIR variable in .bash_profile
like so: export QUEUEDIR="$XDG_DATA_HOME/msmtpqueue"
).
When you want to send any mail that you have created and queued up run:
Adding /usr/local/bin
to your PATH can save you some keystrokes if you are doing it manually. The README file that comes with the scripts has some handy information, reading it is recommended.
6 "." counter(h3-section)Vim syntax highlighting
The msmtp source distribution includes an msmtprc
syntax-highlighting script for Vim, which is available at /usr/share/vim/vimfiles/syntax/msmtp.vim
. The filetype is not detected automatically. The easiest way to enable it is by adding a modeline at the top or bottom of the file(s), i.e.:
7 "." counter(h3-section)Send mail with PHP using msmtp
Look for sendmail_path
option in your php.ini
and edit like this:
Note that you can not use a user configuration file (ie: one under ~/) if you plan on using msmtp as a sendmail replacement with php or something similar. In that case just create /etc/msmtprc, and remove your user configuration (or not if you plan on using it for something else). Also make sure it is readable by whatever you are using it with (php, django, etc...).
From the msmtp manual: Accounts defined in the user configuration file override accounts from the system configuration file. The user configuration file must have no more permissions than user read/write
So it is impossible to have a conf file under ~/ and have it still be readable by the php user.
To test it place this file in your php enabled server or using php-cli.
php-fpm
will fail to send mails and logs the warning: PHP Warning: mail(mail.log): failed to open stream
unless you set the permissions of your /etc/msmtprc to user read/write (600).
8Troubleshooting
8 "." counter(h3-section)Issues with TLS
If you see the following message:
It probably means your tls_trust_file is not right.
Just follow the fine manual. It explains you how to find out the server certificate issuer of a given smtp server. Then you can explore the /usr/share/ca-certificates/
directory to find out if by any chance, the certificate you need is there. If not, you will have to get the certificate on your own. If you are using your own certificate, you can make msmtp trust it by adding the following to your ~/.msmtprc
:
If you are trying to send mail through Gmail and are receiving this error, have a look at this thread or just use the second Gmail example above.
If you are completely desperate, but are 100% sure you are communicating with the right server, you can always temporarily disable the cert check:
If you see the following message:
You may be affected by this bug. Recompile with --with-ssl=openssl
(msmtp is compiled with GnuTLS by default).
9 "." counter(h3-section)Server sent empty reply
If you get a "server sent empty reply" error, this probably means the mail server does not support STARTTLS over port 587, but requires TLS over port 465.
To let msmtp use TLS over port 465, add the following line to ~/.msmtprc
:
2 "." counter(h3-section) "." counter(h4-section)Zoho SMTP server
It can also happen on Zoho SMTP servers when the mail has no blank line between mail headers and mail body (see Debian bug #917260). The solution to this is to add an extra space in between:
10 "." counter(h3-section)Issues with GSSAPI
If you get the following error
Try changing your auth setting to plain, instead of gssapi in your .msmtprc file [1]:
11 "." counter(h3-section)Envelope not accepted
In the case of
Try enabling authentication with
or any other method.
- This page was last edited on 5 October 2024, at 08:21.
- Content is available under GNU Free Documentation License 1.3 or later unless otherwise noted.