31

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
therobyouknow
  • 6,604
  • 13
  • 56
  • 73

6 Answers6

41

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

echo "Test" | mail -a "From: Someone <someone@example.com>" other@example.com

You can add any custom headers using -a:

echo "Test" | mail -a "From: Someone <someone@example.com>" \
                   -a "Subject: This is a test" \
                   -a "X-Custom-Header: yes" other@example.com
sam hocevar
  • 11,853
  • 5
  • 49
  • 68
  • 8
    This didn't work. Sorry. – therobyouknow Jul 01 '11 at 08:29
  • 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? – sam hocevar Jul 01 '11 at 09:44
  • Hocevar I'd need to know how to use it with a custom header. Feel free to advise. – therobyouknow Jul 01 '11 at 20:31
  • 1
    @Rob: I edited the message to give an example of a custom header – sam hocevar Jul 01 '11 at 22:17
  • 1
    +1 @Sam Hocevar, thanks for the custom header example. – therobyouknow Jul 02 '11 at 22:08
  • 1
    The error this command returns is: `From: Someone : 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. – Eric Leschinski Nov 13 '12 at 22:18
  • @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? – sam hocevar Nov 15 '12 at 07:07
  • 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. – therobyouknow Feb 22 '13 at 15:55
  • 1
    Doesn't work for Amazon Linux (AMI) either sadly – Milan Velebit Nov 27 '17 at 14:31
10
mail -s "$(echo -e "This is the subject\nFrom: Paula <johny@paula.com>\n
Reply-to: 1232564@yourserver.com\nContent-Type: text/html\n")" 
milas.josh@gmail.com < 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.

MoSs
  • 261
  • 3
  • 3
  • 1
    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 Reply-to: 1232564@yourserver.com\nContent-Type: text/html\n")" josh@gmail.com < htmlFileMessage.txt ` – Eric Leschinski Nov 13 '12 at 22:26
  • No been successful yet with this answer. Will try to provide details as to how I'm calling it. – therobyouknow Feb 22 '13 at 15:56
  • 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! – Eric Dec 10 '14 at 23:51
9

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

mail -r me@example.com -s ...

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

mail -r "me@example.com (My Name)" -s "My Subject" ...
RafaSashi
  • 16,483
  • 8
  • 84
  • 94
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

user1106046
  • 61
  • 1
  • 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. – therobyouknow Feb 22 '13 at 12:41
  • 1
    Thanks man. This works for me. You're the GAY! – Henrique Van Klaveren Dec 30 '19 at 18:23
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. [...]
Gryphius
  • 75,626
  • 6
  • 48
  • 54
  • 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? – therobyouknow Jul 01 '11 at 08:41
  • 1
    afaik most common MTA implementations provide a generic sendmail compatible interface and its common for cgi scripts to call sendmail directly. – Gryphius Jul 01 '11 at 08:54
  • +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. – therobyouknow Jul 01 '11 at 09:20
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 yourusername@yourdomain.com' >> /etc/postfix/generic

3) sudo postmap /etc/postfix/generic

4) sudo service postfix restart

Community
  • 1
  • 1
Andy
  • 119
  • 1
  • 5