How to change sender name (not email address) when using the linux mail command for autosending mail? [closed]
Asked Answered
H

6

32

Mailbox shows the sender name as "Apache", because the mail I am autosending is being sent from a Perl CGI program. How do I change it to something else?

Halvaard answered 30/6, 2011 at 15:32 Comment(0)
T
41

You just need to add a From: header. By default there is none.

echo "Test" | mail -a "From: Someone <[email protected]>" [email protected]

You can add any custom headers using -a:

echo "Test" | mail -a "From: Someone <[email protected]>" \
                   -a "Subject: This is a test" \
                   -a "X-Custom-Header: yes" [email protected]
Trott answered 30/6, 2011 at 15:44 Comment(9)
This didn't work. Sorry.Halvaard
Well it should... Maybe the mail server is rewriting the e-mail to enforce enveloppe/content consistency, in which case you won't be able to use mail. Can you check whether it works with a custom header instead?Trott
Hocevar I'd need to know how to use it with a custom header. Feel free to advise.Halvaard
@Rob: I edited the message to give an example of a custom headerTrott
+1 @Sam Hocevar, thanks for the custom header example.Halvaard
The error this command returns is: From: Someone <[email protected]>: No such file or directory It's as if it's treating the email address as a directory. Both commands fail on Fedora 17. You passed a string which is not a file into the '-a' option, this cannot work.Gilliangilliard
@EricLeschinski the Sendmail and Postfix versions of mail expect -a to be passed a string, not a file. What is the email software you are using on Fedora?Trott
Trying to close this question and accept an answer. Had a try of your suggestion @SamHocevar but I was unsuccessful - no email sent. I'll try to provide more details later.Halvaard
Doesn't work for Amazon Linux (AMI) either sadlyLilly
P
10
mail -s "$(echo -e "This is the subject\nFrom: Paula <[email protected]>\n
Reply-to: [email protected]\nContent-Type: text/html\n")" 
[email protected] < htmlFileMessage.txt

the above is my solution..just replace the "Paula" with any name you want e.g Johny Bravo..any extra headers can be added just after the from and before the reply to...just make sure you know your headers syntax before adding them....this worked perfectly for me.

Picco answered 25/7, 2012 at 18:44 Comment(3)
This worked, you put the command on 3 lines, the 3 lines has to be one line, like this: mail -s "$(echo -e "This is the subject\nFrom: Paula <[email protected]> Reply-to: [email protected]\nContent-Type: text/html\n")" [email protected] < htmlFileMessage.txt Gilliangilliard
No been successful yet with this answer. Will try to provide details as to how I'm calling it.Halvaard
THANK YOU. I've been awake for 7 days straight now trying to get this to work. This was the only solution that worked for me!Casabonne
C
9

You can use the "-r" option to set the sender address:

mail -r [email protected] -s ...

In case you also want to include your real name in the from-field, you can use the following format

mail -r "[email protected] (My Name)" -s "My Subject" ...
Compost answered 14/5, 2015 at 6:8 Comment(1)
Note: -r is a GNU-ism. Not available on BSD.Hiroshima
F
6

If no From: header is specified in the e-mail headers, the MTA uses the full name of the current user, in this case "Apache". You can edit full user names in /etc/passwd

Failure answered 1/4, 2012 at 8:33 Comment(2)
This is a solution but not going to be the accepted one as I would rather not change Apache to another same for the sake of just the email sender being changed. A case of Tail wagging the dog and all that. It should be possible to change the server name, after all, developers of many auto-reponder sites manage this perfectly well.Halvaard
Thanks man. This works for me. You're the GAY!Craiova
P
2

It depends on what sender address you are talking about. The sender address visble in the recipients mailprogramm is extracted from the "From:" Header. which can probably easily be set from your program.

If you are talking about the SMTP envelope sender address, you can pass the -f argument to the sendmail binary. Depending on the server configuration you may not be allowed to do that with the apache user.

from the sendmail manpage :

   -f <address>
                 This  option  sets  the  address  of the envelope sender of a
                 locally-generated message (also known as  the  return  path).
                 The  option  can normally be used only by a trusted user, but
                 untrusted_set_sender can be set to allow untrusted  users  to
                 use it. [...]
Panek answered 30/6, 2011 at 16:11 Comment(3)
I'm using the mail command, not sendmail. I was advised that mail is more generic than sendmail and so more likely to work on various distributions and platforms, is this true?Halvaard
afaik most common MTA implementations provide a generic sendmail compatible interface and its common for cgi scripts to call sendmail directly.Panek
+1 thanks @Gryphius. I'll look into sendmail then and try out your answer. First, I will need to map the arguments that I called mail with, to the equivalents in sendmail. I'll come back later.Halvaard
T
1

On Ubuntu 14.04 none of these suggestions worked. Postfix would override with the logged in system user as the sender. What worked was the following solution listed at this link --> Change outgoing mail address from root@servername - rackspace sendgrid postfix

STEPS:

1) Make sure this is set in /etc/postfix/main.cf:

   smtp_generic_maps = hash:/etc/postfix/generic

2) echo 'www-data [email protected]' >> /etc/postfix/generic

3) sudo postmap /etc/postfix/generic

4) sudo service postfix restart

Tizzy answered 6/5, 2015 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.