Skip to content

Instantly share code, notes, and snippets.

@movd
Created June 6, 2019 11:02
Show Gist options
  • Save movd/7a9e3db63d076f85d16c7dcde62fe401 to your computer and use it in GitHub Desktop.
Save movd/7a9e3db63d076f85d16c7dcde62fe401 to your computer and use it in GitHub Desktop.
Set up msmtp to send emails emails from Ubuntu/Debian Servers

Setting up email with SMTP on Ubuntu/Debian Servers

I used to sift trough my shell history and bookmarks every time I set up a new testing server in order to be able to send mails. So this should help...

Be aware don't use ssmtp anymore. It's unmaintained and has been removed from Debian and Ubuntu will most definitely follow suit.

Install msmtp

First we need the awesome program called msmtp to route all the server's mail through a standard SMTP server.

sudo apt-get install msmtp msmtp-mta mailutils

Set up msmtp

sudo nano /etc/msmtprc

# Set default values for all following accounts.
defaults

# Use the mail submission port 587 instead of the SMTP port 25.
port 587

# Always use TLS.
tls on

# Set a list of trusted CAs for TLS. The default is to use system settings, but
# you can select your own file.
tls_trust_file /etc/ssl/certs/ca-certificates.crt

# The SMTP server of your ISP
account isp
host mail.isp.example
from [email protected]
auth on
user 12345

# Set default account to isp
account default: isp

# Map local users to mail addresses
aliases /etc/aliases

The above is based on the example. The program has many more authentication methods.

Install and set up mailx

In order to be able to use the mail command wee need to install mailx

sudo apt-get install bsd-mailx

Set mail transport agent to use msmtp

sudo nano /etc/mail.rc

append the following:

set mta=/usr/bin/msmtp

Set up aliases

We need to link system users with email addresses in order for system users to receive mails from cronjobs.

sudo nano /etc/aliases

# Send root to Jane
root: [email protected]
   
# Send everything else to admin
default: [email protected]

sudo nano /etc/mail.rc

append:

alias root root<[email protected]>

Emails are now sent to this address if e.g. a cronjob fails. Also a general fallback address is used if messages don't belong to root. Of course more users can be set.

Test it!

echo "Hello World" | msmtp -d [email protected]

Test sending a mail to root

echo "Testing msmtp from ${HOSTNAME} with mail command" | mail -s "hi root" root

Test sending a mail to another email adress

echo "Testing msmtp from ${HOSTNAME} with mail command" | mail -s "hi there" [email protected]

Links

https://marlam.de/msmtp/

https://wiki.archlinux.org/index.php/Msmtp

@huwan
Copy link

huwan commented Jun 9, 2023

Thank you, it works for me on Ubuntu 22.04.

@aqxa1
Copy link

aqxa1 commented Jul 18, 2023

Thanks for the guide, which got me on the right track. But you shouldn't need to install bsd-mailx since mailutils (which you install in first step) also works as a mail client. And that also means you don't need the duplicate config in /etc/mail.rc, since mailutils doesn't seem to need any further configuration to work as long as you have msmtp-mta installed.

EDIT: I did still need bsd-mailx on proxmox due to apparmor issues with mailutils, but didn't actually need the additional /etc/mail.rc config for that either interestingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment