Monday, February 7, 2011

Change the "From:" address in Unix "mail"

Sending a message from the Unix command line using mail TO_ADDR results in an email from $USER@$HOSTNAME. Is there a way to change the "From:" address inserted by mail?

For the record, I'm using GNU Mailutils 1.1/1.2 on Ubuntu (but I've seen the same behavior with Fedora and RHEL).

[EDIT]

$ mail -s Testing chris@example.org                                                                  
Cc: 
From: foo@bar.org

Testing
.

yields

Subject: Testing
To: <chris@example.org>
X-Mailer: mail (GNU Mailutils 1.1)
Message-Id: <E1KdTJj-00025z-RK@localhost>
From: <chris@localhost>
Date: Wed, 10 Sep 2008 13:17:23 -0400

From: foo@bar.org

Testing

The "From: foo@bar.org" line is part of the message body, not part of the header.

  • Here are some options:

    • If you have privelige enough, configure sendmail to do rewrites with the generics table

    • Write the entire header yourself (or mail it to yourself, save the entire message with all headers, and re-edit, and send it with rmail from the command line

    • Send directly with sendmail, use the "-f" command line flag and don't include your "From:" line in your message

    These aren't all exactly the same, but I'll leave it to you look into it further.

    On my portable, I have sendmail authenticating as a client to an outgoing mail server and I use generics to make returning mail come to another account. It works like a charm. I aggregate incoming mail with fetchmail.

  • In my version of mail ( Debian linux 4.0 ) the following options work for controlling the source / reply addresses

    • the -a switch, for additional headers to apply, supplying a From: header on the command line that will be appended to the outgoing mail header
    • the $REPLYTO environment variable specifies a Reply-To: header

    so the following sequence

    export REPLYTO=cms-replies@example.com
    mail -aFrom:cms-sends@example.com -s 'Testing'
    

    The result, in my mail clients, is a mail from cms-sends@example.com, which any replies to will default to cms-replies@example.com

    Chris Conway : -a works like a charm! But REPLYTO isn't working at all...
    cms : I just tested it again here to make sure, and it works fine for me. Not all mail clients work well with Reply-To, but I'd have thought that was a solved problem by now. The REPLYTO env variable is mentioned in the man page, Other UNIX mailers honour it, emacs etc. Still, I guess you have a fix.
    Chris Conway : I don't think it's the mail client... I don't see the Reply-To header in the raw message text. But, yeah, -a is sufficient.
    cms : Odd. does 'man mail' suggest that it ought to work ?
    Chris Conway : No, it doesn't. But: "The complete GNU mailutils manual is not available in Debian systems due to licensing reasons." -aReply-To:... works.
    Chris Conway : I also don't see any mention of REPLYTO at http://www.gnu.org/software/mailutils/manual/mailutils.html
    cms : Thank you for replying. My /usr/bin/mail comes from the mailx package, which I think is derived from BSD-mail. And most of my unix machines are BSD.
    From cms
  • for me -a is not working ..any other alternative...pls let me know

  • this worked for me

    echo "hi root"|mail -rsawrub@testingdomain.org -s'testinggg' root

  • On Centos 5.3 I'm able to do:

    mail -s "Subject" user@address.com -- -f from@address.com < body
    

    The double dash stops mail from parsing the -f argument and passes it along to sendmail itself.

    From Beau

0 comments:

Post a Comment