Linux Send Emails

July 10th 2022

In a Linux environment, you might find the need to send notifications when certain events or situations take place. For instance when software updates are available, an email notification can be a helpful reminder to ensure the system is secure. This guide will focus on how to setup msmtp, a lightweight SMTP client on a Debian based Linux system.

Prepare

Before sending emails you need a mail provider with a valid SMTP configuration, and preferably one that supports authentication. There are many to choose from, you might already have access to one through your personal email. The links below are instructions on how to acquire SMTP credentials for popular personal email services. Note that some of the links lead to articles with general information about connecting to third party email programs, as such they have instructions for setting up IMAP or POP too. These functions are not necessary for send-only configurations, as in you only want to send emails from this system and you do not want to receive emails on this system. Focus only on sections that mention SMTP.

  1. Apple/iCloud
  2. Gmail - If you have 2fa setup, you will need to create an app password.
  3. Outlook
  4. Yahoo
  5. ProtonMail - ProtonMail requires the use of ProtonMail Bridge to encrypt messages. ProtonMail Bridge CLI is available on environments without a GUI
  6. AOL

Free public mail providers like the ones listed above are great options to start but they might not scale well. Many impose strict limits on how much mail can be sent through them, and some might consider activity beyond a certain threshold against their terms of service. In these cases a transactional email software solution might be more desirable. These services are built for receiving and sending emails triggered by interactions or events and many of them have useful free tiers. Mailgun, is such a solution which as of July 2022 allows up to 1000 emails per month on the free tier with a first month limit of 5000 emails, very useful for testing.

Install msmtp

Msmtp by itself can be a powerful, but many programs are built around another popular email agent called sendmail. For those situations we have Msmtp-mta, which will automatically create an alias for sendmail and in most cases will also install msmtp.


sudo apt install msmtp-mta

If you only want msmtp, or the command above didn’t install msmtp use the command below.


sudo apt install msmtp

Configure msmtp

Configure msmtp using the file at /etc/msmtprc
If the file doesn’t exist create it using the template for Mailgun below.

     
        defaults
        tls on
        tls_trust_file /etc/ssl/certs/ca-certificates.crt
        logfile /var/log/msmtp.log

        account mailgun
        host smtp.mailgun.org
        port 587
        auth on
        user @yourhost.tld
        password somestrongpassword
        from [email protected]

        account default : mailgun
    

For other email services replace the details for lines 7 through 12 per the mail service’s requirements.
Some services require the ‘from’ and ‘user’ fields to have the same value.

Test msmtp

Send a test email to a known good email address using the command below


echo "test message" | msmtp -a default [email protected]

Test sendmail

If you have sendmail installed on the system, test it with the command below


echo "test message" | sendmail [email protected]
This post is written by Gouthaman Raveendran, licensed under CC BY-NC 4.0.