Specify the user when sending email using the mail command

Does anyone know how to change the user when sending email using the mail command? I looked at the manual page and don’t see how to do it.

We are launching Redhat Linux 5.

+54
linux email redhat
Sep 23 '08 at 6:29
source share
16 answers

http://www.mindspill.org/962 seems to have a solution.

Essentially:

echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -- -f from_user@example.com

+28
Sep 23 '08 at 6:44
source share

You can specify any additional header that you may need with -a

 $mail -s "Some random subject" -a "From: some@mail.tld" to@mail.tld 
+67
Sep 23 '08 at 7:03
source share

mail -r from@from.from -R from@from.com

-r = from-addr -R = reply-to addr

The author indicated that his version of the mail does not support this flag. But if you have a version that does this well.

+21
Sep 23 '08 at 6:31
source share

When sending via SMTP on the mail help page, it is recommended to set the from variable as follows (tested on CentOS 6):

 mail -s Subject -S from=sender@example.com recipient@example.com 

You can also attach the file using the -a option:

 mail -s Subject -S from=sender@example.com -a path_to_attachement recipient@example.com 
+18
May 05 '14 at 12:11
source share

None of them worked for me (Ubuntu 12.04), but finally with the trial and error that I received:

 echo 'my message blabla\nSecond line (optional of course)' | mail -s "Your message title" -r 'Your full name<yourSenderAdress@yourDomain.abc>' -Sreplyto="yourReplyAdressIfDifferent@domain.abc" destinatorEmail@destDomain.abc[,otherDestinator@otherDomain.abc] 

(all on one line, no place in "-Sreplyto")

I received this mail command:

 apt-get install mailutils 
+5
04 Oct '13 at
source share

You can add sendmail options to the end of the mail command by adding - first. -f is the sendmail command to set the from address. So you can do this:

mail recipient@foo.com - -f sender@bar.com

+3
Sep 23 '08 at 6:49
source share

Here .

The second simplest solution after -r (to specify the From header and separate it from the body using a new line such as

  $mail -s "Subject" destination@example.com From: Joel <joel@example.com> Hi! . 

only works in several versions of mail, I don’t know which version of redhat is).

PS: Most versions of mail suck!

+2
Sep 23 '08 at 6:34
source share

None of this helped me. And it took me a long time to figure this out, hopefully this helps the next guy.

I am using Ubuntu 12.04 LTS with mailutils v2.1.

I found this solution somewhere on the network, I don’t know where, I can’t find it again:

 -aFrom:Servername-Server@mydomain.com 

The full command is used:

 cat /root/Reports/ServerName-Report-$DATE.txt | mail -s "Server-Name-Report-$DATE" myemailadress@mydomain.com -aFrom:Servername-Server@mydomain.com 
+2
Jan 14 '14 at 13:13
source share
 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" 

or

 echo "This is the main body of the mail" | mail -s "Subject of the Email" recipent_address@example.com -aFrom:"Elvis Presley<from_user@example.com>" 
+1
Apr 15 '14 at 21:42
source share

Most people need to change two values, trying to fake the address from the email address correctly. The first is the address, and the second is the source address. Many of the online solutions offered change only one of these values.

If as a root user I try a simple email command to send me an email, it might look like this. echo "test" | mail -s "a test" me@noone.com

And related logs: Feb 6 09:02:51 myserver postfix/qmgr[28875]: B10322269D: from=<root@myserver.com>, size=437, nrcpt=1 (queue active) Feb 6 09:02:52 myserver postfix/smtp[19848]: B10322269D: to=<me@noone.com>, relay=myMTA[xxxx]: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)

Attempting to change the address with - echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com echo "test" | mail -s "a test" me@noone.com -- dude@thisguy.com

This changes the orig-to value, but not the value from: Feb 6 09:09:09 myserver postfix/qmgr[28875]: 6BD362269D: from=<root@myserver.com>, size=474, nrcpt=2 (queue active) Feb 6 09:09:09 myserver postfix/smtp[20505]: 6BD362269D: to=<me@noone>, orig_to=<dude@thisguy.com>, relay=myMTA[xxxx]: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)

Then try using -r and a to adjust the from and orig-to options. echo "test" | mail -s "a test" -r dude@comeguy.com me@noone.com -- dude@someguy.com

And logs: Feb 6 09:17:11 myserver postfix/qmgr[28875]: E3B972264C: from=<dude@someguy.com>, size=459, nrcpt=2 (queue active) Feb 6 09:17:11 myserver postfix/smtp[21559]: E3B972264C: to=<me@noone.com>, orig_to=<dude@someguy.com>, relay=myMTA[xxxx]: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)

Here's how it works for me. Hope this helps someone.

+1
Feb 06 '15 at 15:21
source share

It works on Centos7

echo "This is the main body of the mail" | mail -s "Subject of the Email" -r seneder_address@whatever.com recipent_address@example.com

+1
May 13 '16 at a.m.
source share

Here's the answer from 2018, on Debian 9 pulled.

Note -e for echo to allow newlines, and -r for mailx to show the name along with the outgoing email address:

 $ echo -e "testing email via yourisp.com from command line\n\nsent on: $(date)" | mailx -r "Foghorn Leghorn <sender@yourisp.com>" -s "test cli email $(date)" -- recipient@somedomain.com 

Hope this helps!

+1
Apr 21 '18 at 15:02
source share

on CentOs5: -r from@me.omg

0
Feb 03 '10 at 11:01
source share

Thanks to all the sample providers, some worked for some, no. Below is another simple sample format that worked for me.

 echo "Sample body" | mail -s "Test email" from=sender-addrs@example.com recepient-addres@example.com 
0
Sep 21 '15 at 18:36
source share

For CentOS, here is the working command:

 mail -s Subject -S from=sender@example.com recipient@example.com 
0
Aug 30 '19 at 11:18
source share

I ran into the same problem. But I decided that the problem was setting the geko field in the / ect / passwd field. Postfix by default sends mail from user login. Suppose you want to switch from a field to Alert. You just need to edit the / ect / passwd file in any editor you like.

  • vim / ect / passwd

root: x: 0: 0: alerts : / root: / bin / bash

  1. Restart postfix.

Now check the result.

  1. echo "This is the bulk of the mail" | mail -s "Email subject" user@domain.com

Now at the recipient. From will be shown as Alerts , as you indicated in the geko field. Hope this solution will work for you.

-one
Aug 22 '16 at 23:08
source share



All Articles