Msmtp - SMTP Client

Posted on Apr 13, 2023

Home: https://marlam.de/msmtp/

Msmtp is a SMTP client that is very simple to use, basically you use msmtp to send emails. Note that msmtp is a command line application.

Installation

To install pass use your systems package manger. See below for some examples.

## Arch
sudo pacman -S msmtp
## Void
sudo xbps-install -S msmtp
## Debian/Ubuntu
sudo apt install msmtp
## macOS
brew install msmtp

Configuration

To configure msmtp follow the bellow instructions.

# Command to create msmtp config directory.
mkdir ~/.config/msmtp
# Command to create and configure the msmtp config file using Vim. You can use whatever text editor you want if Vim is not your thing.
vim ~/.config/msmtp/config
# Command to make the msmtp config file readable and writable. Required for msmtp to work.
chmod 600 ~/.config/msmtp/config

Add the below to “~/.config/msmtp/config”.

~/.config/msmtp/config

# Give your account a name.
account <account_name>
# Enter your email providers SMTP host address.
host <host_address>
# Enter the port number that your email provider uses for SMTP. Almost always 597.
port 587
# Enter the name you want to appear in the "From" section of your email.
from <your_name>
# Enter your actual email.
user <your_email>
# Enter your password for your email.
password <account_password>
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Enter the path to your msmtp log. Use the below if your not sure, replace "<pc-user>" with your info.
logfile /home/<pc_user>/.config/msmtp/msmtp.log

# Set your default account, should be the account you just created or if you have multiple accounts then pick one.
account default : <account_name>

Configuration - Secure Your Password (Optional)

The following steps are not necessary but strongly recommend. Msmtp allows you to either place your password in the “~/.config/msmtp/config” configuration file or call an external command to handle the password input. If you choose to simply place your password in the “~/.config/msmtp/config” file know that your password will be stored in plaintext, meaning that if anyone gets their hands on your “~/.config/msmtp/config” file then they will also have your password. The better option is to call an external command. I will show two methods for how to do this, see below:

Configuration - Secure Your Password (Optional) - Method 1

Method 1 - GPG

The first method is to use GPG to encrypt a file storing your password. Basically you will create a regular file and paste your password into the file. You will then encrypt the file with a master password so that only those who know the master password can access the email password.

read -s password
echo $password > ~/.msmtpPASS
gpg -c ~/.msmtpPASS
shred -u ~/.msmtpPASS
#Replace "password <account_password>" in your "~/.config/msmtp/config" with the below.
passwordeval "gpg -q --for-your-eyes-only --no-tty -d ~/.msmtpPASS.gpg"

Configuration - Secure Your Password (Optional) - Method 2

Method 2 - Pass

The second method is to use pass. Pass is a password manager for Unix systems and does require some initial setup to get it working, please lookup its manual for how to set it up or read my Walk Through on how to use it. Basically pass works like any other password manager, you add your password to it and secure it using a master password. To access the password you need to know the master password; in reality there’s more to it but that’s the gist of it.

pass add user@example.com
# Replace "password <account_password>" in your "~/.config/msmtp/config" with the below.
passwordeval "pass user@example.com"

Usage

To use msmtp follow the below examples.


# The below is a simple test, replace "somedude@test.com" with your email of choice and see if your email goes out.
echo "Testing things out!" | msmtp -a default somedude@test.com

Other usages of msmtp will really depend on what you want to use it on. The tool is really flexible and can be paired with many MUAs (Mail User Agent) aka email clients.

End

That sums up msmtp in a nut shell, but note that the above is just a basic setup and there are many more options to using it. One email client that pairs well with msmtp is Neomutt. So again if you want a simple way to send emails then check out msmtp.