I setup ssmtp to send email messages from my Ubuntu server to an external email address.
Using the mail command of the mailutils package, I am able to receive emails from my server sent via :
$echo test | mail -s test [email protected]
What I want to do now, is to send any message addressed to user root (like alerts and so on) to [email protected].
$echo test | mail -s test root
One of the most promissing solutions I found so far is :
sSMTP revaliases, aliases and mail.rc
As ssmtp is not able to forward and alias, the article tells one to use mail to alias addresses. But this also doesn't work. Indeed, I do not even have an /etc/mail.rc file in my filesystem.
It seems like the email has left the host, but never reaches the remote mail address.
Apr 19 19:46:16 hostname sSMTP[1160]: Creating SSL connection to host
Apr 19 19:46:16 hostname sSMTP[1160]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
Apr 19 19:46:17 hostname sSMTP[1160]: Sent mail for [email protected] (221 2.0.0 Bye) uid=0 username=root outbytes=459
So far the summary. Now to the Question :
How to setup ssmtp and mail so that
$echo test | mail -s test root
will successfully send an email sent to root to an alias of root ?
This was fixed for me with the answer at How to get ssmtp to map local user to email address for the To: field
In summary, the problem was that I was using the
mailutils
package. Switching tobsd-mailx
(which also provides/usr/bin/mail
) allowed for me to customize/etc/mail.rc
to map theTo
address withalias root root<[email protected]>
Some further context:
My goal was to do the above using Fastmail as my SMTP server, sending to my own Fastmail account. When running
echo 'test' | mail -s "Test" root
, I would see the following in the ssmtp logs:I couldn't find anything useful for that specific error Fastmail was returning. To troubleshoot, I tried switching to a gmail account. In that case, Google accepted my email, and it was visible in my user's "sent" messages, but I quickly got a bounce message:
And, looking at the message source:
So, clearly Google was accepting the email, then trying to send to
[email protected]
which didn't exist. In contrast, Fastmail does the smarter thing of checking the To address before accepting the email, avoiding the need to generate a bounce. I just wish their error message was clearer and documented somewhere!With the above in place, I now get the following headers (triggered by running a
zfs scrub
on a pool where zfs sends emails on completion):