When setting up cron jobs with msmtp as your MTA, you might encounter emails showing up as (unknown sender) in your Gmail inbox. The raw headers typically display:
From: root (Cron Daemon)
This occurs because msmtp inherits the system's default sender identity rather than using your configured email address.
First, ensure your ~/.msmtprc
or /etc/msmtprc
contains proper authentication:
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile ~/.msmtp.log
account your_account
host smtp.gmail.com
port 587
from your.email@gmail.com
user your.email@gmail.com
password your_app_specific_password
1. Using msmtp's --from Flag
Modify your cron job to explicitly set the sender:
0 5 * * * /usr/bin/msmtp --from=your.name@domain.com -t recipient@domain.com < /path/to/your/script/output
2. System-wide MAILFROM Configuration
Create or edit /etc/email-addresses
:
root: your.email@domain.com
www-data: your.email@domain.com
Then modify your msmtp configuration to use this file:
aliases /etc/email-addresses
For complete control, create a wrapper script (/usr/local/bin/sendmail-custom
):
#!/bin/sh
/usr/bin/msmtp \
--from="Your Name <your.email@domain.com>" \
--add-missing-from-header \
"$@"
Make it executable and update your symlink:
chmod +x /usr/local/bin/sendmail-custom
ln -sf /usr/local/bin/sendmail-custom /usr/sbin/sendmail
Test your configuration with:
echo "Test message" | mail -s "Test Subject" your.email@gmail.com
Check the resulting email headers to confirm the From address appears correctly.
When cron jobs send emails through msmtp, the default behavior is to use the system username (like 'root') as the sender, which gets flagged as (unknown sender) in email clients like Gmail. This happens because:
- Cron uses the system's mail command format
- msmtp inherits the envelope sender from the MTA
- No proper email address is specified in the headers
1. Direct msmtp Configuration /h2>
Edit your ~/.msmtprc
or /etc/msmtprc
:
defaults auth on tls on tls_trust_file /etc/ssl/certs/ca-certificates.crt account your_account host smtp.yourprovider.com port 587 from your.email@example.com user your_username password your_password account default : your_account
Key points:
- The
from
field sets the default envelope sender - Make sure the file has correct permissions:
chmod 600 ~/.msmtprc
2. Using mail.cf Wrapper /h2>
Create /usr/local/bin/mail.cf
:
#!/bin/sh /usr/bin/msmtp -t --from=your.email@example.com
Then make it executable and set as default:
chmod +x /usr/local/bin/mail.cf sudo ln -sf /usr/local/bin/mail.cf /usr/sbin/sendmail
3. Cron MAILTO Directive /h2>
At the top of your crontab:
MAILTO="your.email@example.com" SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Then your jobs * * * * * /path/to/your/script.sh
Verify with this command:
echo "Test message" | mail -s "Test Subject" your.email@example.com
Check the received email headers for proper 'From' field.
For complete control, use a script wrapper:
#!/bin/bash { echo "From: Your Name <your.email@example.com>" echo "To: Recipient <recipient@example.com>" echo "Subject: Cron Job Notification" echo "" /path/to/your/script.sh } | /usr/bin/msmtp -t
Remember to restart cron after changes: sudo service cron restart