This SMTP application works the same way on Raspbian 10 (Buster).

sudo apt-get install msmtp msmtp-mta -y
sudo nano /etc/msmtprc

Copy and paste this content to the file:

defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
account gmail
host smtp.gmail.com
port 587
from username@gmail.com
user username@gmail.com
password password
account default : gmail

Using your real password is not ideal for two reasons. First, it will be stored as plain text and other users of your server can read it. Second, you have to go into your Google account settings and allow “less secure apps” [Link].

Instead, you can create an App Password (a password exclusive to this application) in your Google account, then copy and paste it into the MSMTP configuration file.

Steps (the links are shortcuts; if they do not work, follow the steps manually):

  • Enter your Google account settings [Link].
  • Select Security [Link].
  • Under Signing in to Google, enable 2-Step Verification [Link].
  • Select App Passwords [Link].
  • Choose the app and device you want to generate the password for.
  • Click Generate.
  • Copy the password, which will look something like this: mssl uemq sabi fjbc.
  • Paste it into the MSMTP configuration file.

This App Password procedure also applies if you want to use Sendmail or Postfix as a Google relay.

Note that all emails sent through the Google server will use your Google account address as the sender.

Test your configuration:

(echo "Subject: Test"; echo 'Body of the email') | msmtp username@gmail.com
cat file.txt | msmtp -a default username@gmail.com

Note: -a followed by a configuration name will select which configuration (server, credentials, etc.) to use.

You can also install the mail tools and test them:

sudo apt-get install mailutils -y
echo 'Message' | mail -s "Subject" username@gmail.com
echo 'Message' | mail -s "Subject" -A attachment.zip username@gmail.com
echo 'Message' | sendmail username@gmail.com

BONUS

Sending test emails with CURL:

curl --url 'smtp://smtp.example.com:25' --mail-from '[email protected]' --mail-rcpt '[email protected]' -F '='

Sending test emails with Telnet/Netcat:

telnet smtp.example.com 25
EHLO example.com
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
DATA
Subject: Test Email Subject
From: [email protected]
To: [email protected]

Body of the email
.
QUIT