'How to use an alias while sending mail on linux?

I have a shell script which sends out a mail at the end of its processing about the status. I am using the command -

mail -s "MAIL_SUBJECT" "tom@my_domain.com" < "MAIL_TEXT"

This mail is being sent from an email id like user@<machine_name>.my_domain.local.

Is it possible to use an alias while sending these mails ?

Something like process.name@my_domain.com?

If yes how do we do it ?



Solution 1:[1]

Use the -r option to mention the from address.

mail -r "process.name@my_domain.com" -s "MAIL_SUBJECT" "tom@my_domain.com" < "MAIL_TEXT"

Solution 2:[2]

Some versions of mail have options for this, but they are not universally portable. If your needs are simple, just write your own sendmail wrapper.

cat - MAIL_TEXT << ____HERE | sendmail -oi [email protected]
From: Some Alias <[email protected]>
Subject: MAIL_SUBJECT

____HERE

The empty line after the headers is significant. Sendmail will fill in (what it thinks are) sensible defaults for the headers you don't specify.

It doesn't have to be a proper Sendmail; most MTAs ship a "sendmail" binary which (more or less) supports the traditional Sendmail command-line API.

If you need MIME or other bells and whistles, maybe look at mutt instead.

Solution 3:[3]

-r doesn't do anything IF you have not set :

FromLineOverride=Yes

in /etc/ssmtp/ssmtp.conf (for debian at least) see -> https://linux.die.net/man/5/ssmtp.conf ["FromLineOverride Specifies whether the From header of an email, if any, may override the default domain. The default is ''no''."]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nothrow
Solution 2 tripleee
Solution 3