Specify the from user when sending email using the mail command
Does anyone know how to change the from user when sending email using the mail command? I have looked through the man page and can not see how to do this. We are running Redhat Linux 5.
You would probably have got the answer to this question a lot faster by just typing «man mail», which will show you the manual for the command.
15 Answers 15
You can specify any extra header you may need with -a
$mail -s "Some random subject" -a "From: some@mail.tld" to@mail.tld
yes with quotes like your example and it displays the error I wrote. But the mail -r option is working stackoverflow.com/questions/119390/… (btw don’t forget the @username mention if you you want people to get notifications about your replies)
also working on ubuntu 14.04 ( /etc/alternatives/mail -> /usr/bin/bsd-mailx ), but not on 16.04 ( /etc/alternatives/mail -> /usr/bin/s-nail )
echo «This is the main body of the mail» | mail -s «Subject of the Email» recipent_address@example.com — -f from_user@example.com
Doesn’t seem to pass the sniff-test. — marks end of options to getopt(3) , so how would -f possibly work as an option after — ? Also, reading the man page for (bsd) mail on Ubuntu doesn’t mention any such -f argument support. Lastly, testing this on Ubuntu 14.04 (with some disbelief and slight hope) shows that indeed it doesn’t work. FWIW (hard to believe again) the original mindspill.org URL says it works on Red Hat without specifying a version.
Use the -v option for Verbose output mail -v -s . . If the mail fails to deliver due to an improperly configured mail server for example, the smtp command log will show what has gone wrong.
mail -r from@from.from -R from@from.com
-r = from-addr -R = reply-to addr
The author has indicated his version of mail doesn’t support this flag. But if you have a version that does this works fine.
On Debian Wheezy, -r is working on mail command for editing From address but -R is not working for editing ReplyTo address.
-r worked for me and went to my gmail acct and not delayed spam (after configuring spf/dkim) BUT not the -R flag which wasn’t a part of the original question anyway.
When sending over SMTP, the mail man page advises to set the from variable, in this way (Tested on CentOS 6):
mail -s Subject -S from=sender@example.com recipient@example.com
You could also attach a file using the -a option:
mail -s Subject -S from=sender@example.com -a path_to_attachement recipient@example.com
None of these worked for me (Ubuntu 12.04) but finally with trial & error I got:
echo 'my message blabla\nSecond line (optional of course)' | mail -s "Your message title" -r 'Your full name' -Sreplyto="yourReplyAdressIfDifferent@domain.abc" destinatorEmail@destDomain.abc[,otherDestinator@otherDomain.abc]
(all in one line, there is no space in «-Sreplyto»)
I got this mail command from:
You can append sendmail options to the end of the mail command by first adding —. -f is the command on sendmail to set the from address. So you can do this:
mail recipient@foo.com — -f sender@bar.com
None of the above worked for me. And it took me long to figure it out, hopefully this helps the next guy.
I’m using Ubuntu 12.04 LTS with mailutils v2.1.
I found this solutions somewhere on the net, don’t know where, can’t find it again:
-aFrom:Servername-Server@mydomain.com
cat /root/Reports/ServerName-Report-$DATE.txt | mail -s "Server-Name-Report-$DATE" myemailadress@mydomain.com -aFrom:Servername-Server@mydomain.com
The second easiest solution after -r (which is to specify a From: header and separate it from the body by a newline like this
$mail -s "Subject" destination@example.com From: Joel Hi! .
works in only a few mail versions, don’t know what version redhat carries).
PS: Most versions of mail suck!
echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com -F "Elvis Presley"
echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -aFrom:"Elvis Presley"
Most people need to change two values when trying to correctly forge the from address on an email. First is the from address and the second is the orig-to address. Many of the solutions offered online only change one of these values.
If as root, I try a simple mail command to send myself an email it might look like this. echo «test» | mail -s «a test» me@noone.com
And the associated logs: Feb 6 09:02:51 myserver postfix/qmgr[28875]: B10322269D: from=, size=437, nrcpt=1 (queue active) Feb 6 09:02:52 myserver postfix/smtp[19848]: B10322269D: to=, relay=myMTA[x.x.x.x]:25, delay=0.34, delays=0.1/0/0.11/0.13, dsn=2.0.0, status=sent (250 Ok 0000014b5f678593-a0e399ef-a801-4655-ad6b-19864a220f38-000000)
Trying to change the from address with — echo «test» | mail -s «a test» me@noone.com — dude@thisguy.com
This changes the orig-to value but not the from value: Feb 6 09:09:09 myserver postfix/qmgr[28875]: 6BD362269D: from=, size=474, nrcpt=2 (queue active) Feb 6 09:09:09 myserver postfix/smtp[20505]: 6BD362269D: to=, orig_to=, relay=myMTA[x.x.x.x]:25, delay=0.31, delays=0.06/0/0.09/0.15, dsn=2.0.0, status=sent (250 Ok 0000014b5f6d48e2-a98b70be-fb02-44e0-8eb3-e4f5b1820265-000000)
Next trying it with a -r and a — to adjust the from and orig-to. echo «test» | mail -s «a test» -r dude@comeguy.com me@noone.com — dude@someguy.com
And the logs: Feb 6 09:17:11 myserver postfix/qmgr[28875]: E3B972264C: from=, size=459, nrcpt=2 (queue active) Feb 6 09:17:11 myserver postfix/smtp[21559]: E3B972264C: to=, orig_to=, relay=myMTA[x.x.x.x]:25, delay=1.1, delays=0.56/0.24/0.11/0.17, dsn=2.0.0, status=sent (250 Ok 0000014b5f74a2c0-c06709f0-4e8d-4d7e-9abf-dbcea2bee2ea-000000)
This is how it’s working for me. Hope this helps someone.
Set from address text in bash mail
I am able to send mail from bash, but cannot change the from text, it currently comes up as ‘root’. I would like email clients to show the following text in the from field script on myserver.com (root@myserver.com) . How can I do this please?
emailsubject="$scriptname ($scriptver) log at $startdate" if [ $errorcount > 0 ]; then emailsubject="ERRORS($errorcount) - $" fi mail -s "$emailsubject" tobealerted@hotmail.com < $logfile
2 Answers 2
If Your "mail" supports it, You may add custom headers using the -a switch like this:
mail -s "subject" -a "From: president@whitehouse.gov" .
This isn't a BASH issue. This is an issue with the mail program itself.
Run the command man mail and see the options for the command. It varies from system to system. Check for a parameter that allows you to set who the mail is from or the sender. If mail doesn't have such a parameter, try mailx which has more options. (It looks like your system has mailx called mail .
On my system, I use mailx instead of mail and I can use the -r parameter to set a sender address:
mailx -r president@whitehouse.gov -s "Secret Message. For your eyes only" < $file
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43530
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to change "From:" field for emails from Cron?
I use remote SMTP via nullmailer and it requires set From field to the specific name, but cron set it as root@my.sweet.server.com. How could I change it to something like me@ya.ru?
13 Answers 13
I don't think you can change the FROM address, (someone should add a MAILFROM option).
You can do something like this though to achieve a similar result:
* * * * * /path/to/script 2>&1 | mail -s "Output of /path/to/script" toaddress@example.com -- -r "fromaddress@example.com" -F"Full Name of sender"
All output is piped to the mail command so the MAILTO variable isn't used at all.
The to address would need to be set but you may be able to use $MAILTO variable. The -- sets the rest of the options to be sendmail options so you can use the -r and and -F options.
-F is the Full name of the sender (makes it look nice in email clients)
Modern versions of cron do accept "MAILFROM=. " in the crontab format. I suggest that you try "man 5 crontab". If it mentions MAILFROM, your version should support it. The phrase to look for is towards the end of the paragraph discussing MAILTO, and should be something like this:
If MAILFROM is defined (and non-empty), it will be used as the envelope sender address, otherwise, ''root'' will be used.
Where "modern" means what? This is not supported by the most recent version of cron in Debian unstable (3.0pl1-124) from what I can see in the changelog or the documentation.
Unfortunately, Ubuntu's cron (at least as of 14.04 LTS) has no mention of MAILFROM in man 5 crontab .
/etc/mailname contains the domain name part of the FROM address. If /etc/mailname contains 'somecompany.com' then cron running for root would have sender as root@somecompany.com
Some info here: wiki.debian.org/EtcMailName - does not say if nullmailer use it. I use sendmail and it did not seem to work for it.
For me, the easiest way to change the from address on a system, is to create a ~/.mailrc file with contents like this:
set name="My Full Name" set from="myrealemail@example.com"
Any of the mail commands that run as my user, now use these settings.
You can set the nullmailer from address via environment variables or command line. The command line arguments are -f and -F for sender address and full name respectively.
Usually you can set environment variables in the crontab.
NULLMAILER_USER=webmaster NULLMAILER_HOST=host.example.com NULLMAILER_NAME="Mr Cron" 5 0 * * * /usr/local/bin/daily.sh
Here are few things you can apply to change your sender domain:
Edit this file: /etc/mailname and change to: example.org sudo postconf -e 'myhostname= example.org' sudo systemctl restart postfix
This is an old question, but it remains valid. There is no easy solution for Debian (and possibly others) because MAILFROM remains unsupported. This has bugged me for some time, as I receive cron generated mail from a number of different servers. But most of them are on subdomains of the same domain, and most of the subdomains do not support mail. Hence I'm forced to configure postfix to use the domain as the origin, and all the mail seems to have come from the same place.
I've finally got round to creating a solution, by adapting cronic to create mycronic. This does much the same as cronic, except it sends its output directly to mail and suppresses all output to cron. It assumes that the server is configured so as to give correct answers to different requests to hostname. For example:
hostname -f => webserver.example.com hostname -d => example.com hostname => webserver
This is achieved by having just webserver in /etc/hostname and having a line in /etc/hosts:
127.0.0.1 webserver.example.com webserver
Postfix is configured to have myorigin set to mydomain. The script also relies on the fact that I have postfix configured to redirect root@example.com to my own mail box. The actual script is:
#!/bin/bash # MyCronic v1 - cron job report wrapper to send results directly to mail # Copyright 2020 Martin Brampton. No rights reserved, whatsoever. # Based on Cronic v3 - cron job report wrapper # Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever. # Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/ set -eu DOMAIN=`hostname -d` FQDN=`hostname -f` RECIPIENT="root@$DOMAIN" MAILER=`which mail` TMP=$(mktemp -d) OUT=$TMP/cronic.out ERR=$TMP/cronic.err TRACE=$TMP/cronic.trace set +e "$@" >$OUT 2>$TRACE RESULT=$? set -e PATTERN="^$\\+$" if grep -aq "$PATTERN" $TRACE; then ! grep -av "$PATTERN" $TRACE > $ERR else ERR=$TRACE fi if [ $RESULT -ne 0 -o -s "$ERR" ]; then mailtext=" My cronic detected failure or error output for the command: $@ RESULT CODE: $RESULT ERROR OUTPUT: $( < $ERR) STANDARD OUTPUT: $(< $OUT) " if [ $TRACE != $ERR ]; then $mailtext=" $mailtext TRACE-ERROR OUTPUT: $(< $TRACE) " fi $MAILER -s "My Cronic detected a failure on $FQDN" -aFrom:MyCronic\$RECIPIENT
You may need to adapt it a bit to suit your own purposes, but it should work for most situations with minimal alteration.