0

Problem: when I use the mail command under linux (Ubuntu Server 16.04) as root to send an email (several scripts on my server do so), the From: field in the mail header looks like From: root@mydomain.org. I want it to look like From: admin@mydomain.org.

Attempt: I already found the option -a "From: admin@mydomain.org" to add the field to the mail header.

My whole command looks like this:

echo "content" | mail -s "subject" "recipient@wherever.org" -a "From: admin@mydmain.org"

Second Problem: However, I do not want to write the -a option at every point I use the mail command in a script because this is some kind of hard coding.

Second Attempt: My best attempt yet is to write a wrapper, though I think there should be a cleaner method to always add that header field to mails sent with the mail command.

Question: Does anyone know a better way which does not include hard coding? Still I want to use such a simple command line as above to not make things unnecessary complicated.

Best

Fabian

Ilka
  • 50
  • 8

1 Answers1

0

the mail command reads ~/.mailrc or a different startup file given by the environment variable MAILRC. (see manpage, http://manpages.ubuntu.com/manpages/xenial/man1/bsd-mailx.1.html#contenttoc4)

this file can contain the line set from=you@example.org.

conf="$(mktemp)"
trap 'rm -f "$conf"' EXIT
echo set from=you@example.org > "$conf"
export MAILRC="$conf"

# now mail will use you@example.org as From:
thejonny
  • 488
  • 2
  • 9