Send emails using msmtp with a Gmail account
Install msmtp
sudo apt update
sudo apt install msmtp msmtp-mta ca-certificates
Generate a Gmail App Password
- Log in to your Google Account.
- Go to Security and enable 2-Step Verification (if not already enabled).
- Search for App passwords in your account search bar.
- Create a new app password (e.g., name it “diskha”).
- Copy the 16-character app password.
Configure msmtp
Create a .msmtprc file in your home directory. An example is shown below. Be sure to use your email address and password in the file.
# Set default values for all accounts
defaults
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
# Gmail account
account gmail
host smtp.gmail.com
port 465
from your-email@gmail.com
user your-email@gmail.com
password your-16-char-app-password
# Set gmail as the default account
account default : gmail
Secure the msmtp configuration file
chmod 600 ~/.msmtprc
Test
Create a simple email message in a text file (e.g. /tmp/test-email.txt):
Subject: Test email from msmtp
Hello! This email was sent via Gmail using msmtp.
Send the test email using msmtp and your new ~/.msmtprc file.
cat /tmp/test-email.txt | msmtp your-email@gmail.com