1

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 remote@mydomain.com

What I want to do now, is to send any message addressed to user root (like alerts and so on) to remote@mydomain.com.

$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 remote@mydomain.com (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 ?

  • Are you trying to send mail to the root user of the system that it is on? If so, it is just echo test | mail -s test root@localhost – Terrance Apr 19 '20 at 18:20
  • echo test | mail -s test root@localhost results in Apr 19 20:22:58 hostname sSMTP[1270]: RCPT TO:<root@localhost> (504 5.5.2 <root@localhost>: Recipient address rejected: need fully-qualified address) – marcas756 Apr 19 '20 at 18:24
  • https://serverfault.com/a/407876/289128 Chances are your myorigin of /etc/postfix/main.cf is not configured properly – Terrance Apr 19 '20 at 18:27
  • Change to msmtp... – andrew.46 Apr 19 '20 at 21:43

1 Answers1

0

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 to bsd-mailx (which also provides /usr/bin/mail) allowed for me to customize /etc/mail.rc to map the To address with alias root root<me@example.com>

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:

$ sudo journalctl -t sSMTP -f
Dec 26 21:20:55 server.lan sSMTP[3898845]: Creating SSL connection to host
Dec 26 21:20:55 server.lan sSMTP[3898845]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
Dec 26 21:20:56 server.lan sSMTP[3898845]: 551 5.7.1 Not authorised to send from this header address

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:

DNS Error: 13562819 DNS type 'mx' lookup of server.lan responded with code NXDOMAIN Domain name not found: server.lan 

And, looking at the message source:

Return-Path: <example@gmail.com>
Received: from gmail.com ([MY-IP-ADDRESS-REDACTED])
        by smtp.gmail.com with ESMTPSA id az14sm13499775qkb.97.2021.12.27.06.12.13
        for <root@server.lan>
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Mon, 27 Dec 2021 06:12:14 -0800 (PST)
Message-ID: <61c9c9be.1c69fb81.e5f1.caae@mx.google.com>
From: root <example@gmail.com>
X-Google-Original-From: "root" <root@gmail.com>
Received: by gmail.com (sSMTP sendmail emulation); Mon, 27 Dec 2021 09:12:12 -0500
Date: Mon, 27 Dec 2021 09:12:12 -0500
To: <root@server.lan>
X-Mailer: mail (GNU Mailutils 3.7)

test

So, clearly Google was accepting the email, then trying to send to root@server.lan 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):

From: "root" <root@example.com>
Date: Mon, 27 Dec 2021 09:24:06 -0500
To: root<andrew@example.com>
Subject: ZFS scrub_finish event for bpool on fileserver.lan
MIME-Version: 1.0
Content-Type: text/plain; charset="ANSI_X3.4-1968"
Content-Transfer-Encoding: 8bit
Message-ID: <cmu-lmtpd-1817460-1640615049-2@sloti47n08>