Send Emails from Linux with Gmail using msmtp
A clean, lightweight way to send emails from a Linux workstation using Gmail SMTP—perfect for cron jobs, scripts, and system alerts.
Install msmtp
sudo apt update
sudo apt install msmtp mailutils
Generate a Gmail App Password
To use Gmail’s SMTP server, you need an App Password (not your Gmail account password):
Go to: https://myaccount.google.com/apppasswords
Configure msmtp
Create the ~/.msmtprc file:
Add the following configuration (replace with your Gmail address and App Password):
account default
host smtp.gmail.com
port 587
auth on
user yourname@gmail.com
password your_app_password
from yourname@gmail.com
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
Set Secure File Permissions
chmod 600 ~/.msmtprc
Backup Existing sendmail (Optional)
sudo mv /usr/sbin/sendmail /usr/sbin/sendmail.bak 2>/dev/null
Use msmtp as sendmail
sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail
Send a Test Email
echo "This is a test email from msmtp." | mail -s "Test Email" recipient@example.com
Check the log if needed
cat ~/.msmtp.log