Msmtp Email Server Setup with OAuth2 and TLS Security

Author

Reads 2.4K

Diverse team of customer service representatives working collaboratively in a modern office environment.
Credit: pexels.com, Diverse team of customer service representatives working collaboratively in a modern office environment.

Setting up Msmtp with OAuth2 and TLS security is a great way to secure your email communication. This setup provides an additional layer of security by encrypting your emails and authenticating your identity.

Msmtp supports OAuth2 for authentication, which allows you to use your Google account to send emails. This is done by registering an OAuth2 client ID and secret key.

With TLS security, your emails are encrypted in transit, making it difficult for hackers to intercept and read your emails. This is especially important when sending sensitive information.

To set up Msmtp with OAuth2 and TLS security, you'll need to create a certificate and private key using OpenSSL. This will be used to encrypt your emails.

Installation

To install msmtp, you'll need to install the msmtp program, which routes mail through a standard SMTP server. Install msmtp by running the command `sudo apt-get install msmtp msmtp-mta mailutils`.

You'll also need to link system users with email addresses, which allows system users to receive mail from cronjobs. This involves editing the `/etc/aliases` file using `sudo nano /etc/aliases`.

Install

Cables Connected on Server
Credit: pexels.com, Cables Connected on Server

To get started with the installation process, you'll need to install the msmtp program. This is done with the command sudo apt-get install msmtp msmtp-mta mailutils.

The msmtp program is a great tool for routing server mail through a standard SMTP server. It's a crucial step in setting up your server's mail system.

You'll also need to link system users with email addresses so that system users can receive mail from cronjobs. This is done by editing the /etc/aliases file.

The installation process is straightforward and can be completed with just a few commands. Make sure to install all the necessary packages to ensure a smooth setup.

Prerequisites

To begin with, you should be running a Linux operating system, specifically a Debian-like distro, on your computer. This is a requirement for the installation process.

You'll also want to ensure that you can send emails from your terminal, as this will be a key feature of the installation.

Credit: youtube.com, Installation Prerequisites

Your email account should be a GMail one, but don't worry if it's not - you can still follow along even if your setup is slightly different.

If you're using GNOME as your desktop environment, you're good to go.

However, it's worth noting that you should refrain from making any changes to the /usr directory, as this is the territory of the package manager, apt.

Debian Developer Tools

Debian developer tools are a great example of how to use alternative email sending tools. bts and reportbug are command-line tools that can send emails, but they need to be configured to use a different email client.

The default email client for bts is /usr/sbin/sendmail, but you can change it to use msmtp by adding the --sendmail option. For example, you can use bts --sendmail='/usr/bin/msmtp -t' to send emails with msmtp.

reportbug also uses /usr/sbin/sendmail by default, but you can change it to use msmtp by adding the --mta option. For example, you can use reportbug --mta=/usr/bin/msmtp to send emails with msmtp.

There's a package named msmtp-mta that creates a symlink for you, so you don't need to configure every tool that relies on sendmail. This package creates a symlink /usr/sbin/sendmail → /usr/bin/msmtp, making everything work "out of the box".

Recommended read: Linux Email Reader

Configuration

Credit: youtube.com, HOWTO secure the msmtp configuration using libsecret's password manager with Microsoft SMTP (Ubuntu)

To configure msmtp, you can write a configuration file. There are a few locations to choose from, but we'll use ~/.msmtprc for simplicity.

You can store default values for all accounts under the "defaults" section. These settings will apply to all accounts unless overridden.

The "account" section is where you can specify settings specific to a particular account. This will override any default settings until another "account" section is found.

To define the default account, you can use the last line of the configuration file. However, you don't actually need to use "--account default" because it's the default setting if you don't provide an account argument.

Some common options can be shortened for convenience. For example, "--read-recipients" can be shortened to "-t".

Setup and Usage

You can set up msmtp by editing the configuration file with sudo nano /etc/msmtprc.

For offline use, consider using msmtpqueue, a collection of scripts that allow you to send emails even without an internet connection.

To use msmtpqueue, copy the scripts to a convenient location like /usr/local/bin, and then change your MUA to use msmtp-enqueue.sh instead of msmtp when sending email.

Set Up

Close Up Photo of Cables Plugged into the Server
Credit: pexels.com, Close Up Photo of Cables Plugged into the Server

To set up msmtp, you'll need to create a configuration file. This can be done by running the command `sudo nano /etc/msmtprc`.

The program has many more authentication methods beyond what's shown in this example.

You can't use a user configuration file if you plan on using msmtp as a sendmail replacement with PHP or similar programs. Instead, create `/etc/msmtprc` and remove your user configuration, or use it for something else.

Make sure the configuration file is readable by whatever program is using it. For example, if you're using PHP, make sure the file has user read/write permissions (600).

Broaden your view: PHP

Set up Mailx

To set up mailx, you'll need to install it first. Install mailx using the command `sudo apt-get install bsd-mailx`.

The mail command requires a mail transport agent to function properly. To set the mail transport agent to use msmtp, you'll need to edit the mail.rc file.

Open the mail.rc file in a text editor using the command `sudo nano /etc/mail.rc`. Append the following line to the file to configure msmtp:

If this caught your attention, see: Mailx

Security and Authentication

Credit: youtube.com, Step-by-Step Guide to Authenticate SMTP, IMAP & POP Connections Using OAuth 2.0 | Entra ID Setup

OAuth2 can be used to securely authenticate msmtp when basic username/password authentication is unsupported or undesirable.

To set up OAuth2, add the following lines to your /etc/msmtprc file: Add to /etc/msmtprc:aliases /etc/aliasesCreate /etc/aliases:your_username: [email protected]

msmtp can also find passwords automatically by storing them in encrypted files or a keyring, which can be set up using GnuPG.

To do this, create a secure directory on a tmpfs with 700 permissions, then create a plain text file with the mail account password, and encrypt it with your private key.

You can also store passwords in ~/.netrc, a file that can act as a common pool for msmtp, OfflineIMAP, and associated tools.

If you're using GSSAPI and encounter issues, try changing your auth setting to plain, instead of gssapi in your .msmtprc file.

Password Management

To manage passwords securely, you can store them in a keyring using GnuPG. This way, you won't have to enter your password every time.

Focus on password security with white keyboard tiles spelling 'PASSWORD' on a coral background.
Credit: pexels.com, Focus on password security with white keyboard tiles spelling 'PASSWORD' on a coral background.

Set up GnuPG, including gpg-agent, to avoid the hassle of entering your password repeatedly. Create a secure directory on a tmpfs with 700 permissions to keep your unencrypted password safe.

Create a plain text file with your mail account password in that directory, and then encrypt it with your private key. Remove the plain text file and move the encrypted file to the final location, such as ~/.mail/.msmtp-credentials.gpg.

In your ~/.msmtprc file, add the path to the encrypted password file. Normally, this is enough for a GUI password prompt to appear when sending a message from Mutt.

If you can't get the gpg prompt to work, start the gpg-agent before, or use a simple hack to start it from your muttrc file. For example, you can add a command to execute gpg-agent when Mutt starts.

Alternatively, you can place your passwords in ~/.netrc, a file that can be shared among msmtp, OfflineIMAP, and other tools.

For another approach, see: Mutt (email Client)

Issues with TLS

Credit: youtube.com, Let's FIX a BROKEN TLS Handshake // with Wireshark

If you're experiencing issues with TLS, it's likely due to a misconfigured tls_trust_file parameter in your configuration file. This can be fixed by following the fine manual to find the server certificate issuer and checking if the certificate is in the /usr/share/ca-certificates/ directory.

Sometimes, you may need to obtain the certificate on your own, or if you're using your own certificate, you can add it to your ~/.msmtprc file. You can also use openssl to find the fingerprint of the certificate stored in a PEM file.

If you're trying to send mail through Gmail and encountering errors, check out the thread mentioned in the manual or try using the second Gmail example. Alternatively, you can temporarily disable the cert check if you're 100% sure you're communicating with the right server.

You may be affected by a bug if msmtp is compiled with GnuTLS by default, so you can recompile it with --with-ssl=openssl to fix the issue.

Explore further: Mobile Web Server

Issues with GSSAPI

Credit: youtube.com, DevOps & SysAdmins: How to debug ssh authentication failures with gssapi-with-mic?

If you're experiencing issues with GSSAPI, it might be due to an error in your authentication settings.

Try changing your auth setting to plain, instead of gssapi in your .msmtprc file. This can resolve the issue.

Enabling authentication with GSSAPI can also be problematic. You may need to try a different authentication method to get it working.

If you get the following error, it's likely related to GSSAPI: Try changing your auth setting to plain, instead of gssapi in your .msmtprc file.

Alternative to GPG: OpenSSL

If you're looking for an alternative to GPG for encryption, you might want to consider using OpenSSL.

To use OpenSSL, you'll need to disable msmtp's apparmor profile, which disallows executing openssl.

This can be done by creating an encrypted password in a file with the command to type your password and finish with Enter, then Ctrl-D.

To test OpenSSL, you'll need to create an encrypted password in a file.

Integration and Use Cases

Credit: youtube.com, send mail through msmtp (2 Solutions!!)

You can use msmtp to send emails from the command-line in various situations.

Sending emails from the command-line is useful when you need to automate tasks or notifications.

To make it work with msmtp, you might need to configure the software.

The use-cases for msmtp include sending emails from scripts or cron jobs.

You'll need to have msmtp installed and configured to send emails from the command-line.

msmtp requires a configuration file to work properly.

This configuration file can be used to specify the email account settings.

The configuration file can also be used to specify the mail server settings.

You can use msmtp to send emails from the command-line using the msmtp command.

msmtp supports multiple mail accounts and can be configured to use different accounts for different emails.

Troubleshooting

If you're experiencing issues with msmtp, don't worry, we've got you covered.

If you get a "server sent empty reply" error, this probably means the mail server doesn't support STARTTLS over port 587, but requires TLS over port 465.

To resolve this, you can try adding the following line to your msmtp configuration file: add the following line to ~/.msmtprc. This will allow msmtp to use TLS over port 465.

Jennie Bechtelar

Senior Writer

Jennie Bechtelar is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for distilling complex concepts into accessible language, Jennie has established herself as a go-to expert in the fields of important and industry-specific topics. Her writing portfolio showcases a depth of knowledge and expertise in standards and best practices, with a focus on helping readers navigate the intricacies of their chosen fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.