How to include a display name when sending mail with mailx
I’m sending email from a script using mailx. The script is run daily by systemd, using .service and .timer files. For testing purposes I’m sending from the command line.
The mail is sent with the command
echo "Test message - you know the drill" | mailx -r "localweb@example.uk" -s "Test Message" a.user@example.com
(personal data replaced for privacy)
This works. It’s relayed by postfix to my gateway server (also postfix) which signs the outgoing message. This is then delivered to GMail which verifies the SPF data and DKIM signature, and the message is delivered to my inbox.
This is good, except it’s displayed as Ubuntu <localweb@example.uk>
I have other servers sending mail by the same method, and they all display similarly.
I’d like to change the display name to something more useful, per server. For example, Web Server <localweb@example.uk>
but something I’m doing is messing up the From
address as it travels to the gateway server.
I’ve tried
echo "Test message - you know the drill" | mailx -r "Web Server localweb@example.uk" -s "Test Message" a.user@example.com
and
echo "Test message - you know the drill" | mailx -r "Web Server <localweb@example.uk>" -s "Test Message" a.user@example.com
But in both cases the From address is somehow lost by the local PostFix, and the message sent to the gateway server has a from
address of localweb@smtp.private.example.uk
where smtp.private.example.uk
is the FQDN of the gateway server. This is sent to GMail, but fails the SPF and DKIM tests and is unceremoniously dumped in spam as a result. (I don’t know how the local postfix is getting the name of the gateway server)
So, how can I add the display name to the parameters I pass to mailx such that it survives the journey from server to server and is displayed correctly by GMail?
Mailx version 3.14 (GNU Mailutils)
Ubuntu 22.04
I’ve made only one change to main.cf
for PostFix: added a relay-host address for the gateway server.
Since you want to use pre-installed programs and because you have postfix installed, we can use a postfix mode to give you greater control of what you send and how it looks.
This is done via sendmail -t
. In this mode we use postfix as a mail submission agent (MSA), which is what mailx
does anyway, and you can specify the whole header and body, and postfix will send it "as is"
A simple method is to use echo
.
e.g.
echo "From: My name <me@myaddress>
To: You <you@youraddress>
Subject: You'll never guess what...
This is a test!" | /usr/sbin/sendmail -t
This lets you set any header to any value you like.
Now this doesn’t change the envelope sender so the original name is visible in other lines, so it’s not perfect (but you’d get that with mailx
if the From address doesn’t match the account’s address), but it avoids any complications mailx
or other mail user agents (MUAs) may bring in.