Change the “From:” address in Unix “mail”

后端 未结 19 2111
萌比男神i
萌比男神i 2020-12-12 14:32

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:\" ad

相关标签:
19条回答
  • 2020-12-12 14:51

    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

    NB: Mac OS users: you don't have -a , but you do have $REPLYTO

    NB(2): CentOS users, many commenters have added that you need to use -r not -a

    NB(3): This answer is at least ten years old(1), please bear that in mind when you're coming in from Google.

    0 讨论(0)
  • 2020-12-12 14:52

    Plus it's good to use -F option to specify Name of sender.

    Something like this:

    mail -s "$SUBJECT" $MAILTO -- -F $MAILFROM -f ${MAILFROM}@somedomain.com
    

    Or just look at available options: http://www.courier-mta.org/sendmail.html

    0 讨论(0)
  • 2020-12-12 14:54

    The answers provided before didn't work for me on CentOS5. I installed mutt. It has a lot of options. With mutt you do this this way:

    export EMAIL=myfrom@example.com
    export REPLYTO=myreplyto@example.com
    mutt -s Testing chris@example.org
    
    0 讨论(0)
  • 2020-12-12 14:55

    this worked for me

    echo "hi root"|mail -rsawrub@testingdomain.org -s'testinggg' root
    
    0 讨论(0)
  • 2020-12-12 14:55

    None of the above solutions are working for me...

    #!/bin/bash
    
    # Message
    echo "My message" > message.txt
    
    # Mail
    subject="Test"
    mail_header="From: John Smith <john.smith@example.com>"
    recipients="recipient@example.com"
    
    #######################################################################
    cat message.txt | mail -s "$subject" -a "$mail_header" -t "$recipients"
    
    0 讨论(0)
  • 2020-12-12 14:56

    It's also possible to set both the From name and from address using something like:

     echo test | mail -s "test" example@example.com -- -F'Some Name<example2@example.com>' -t
    

    For some reason passing -F'Some Name' and -fexample2@example.com doesn't work, but passing in the -t to sendmail works and is "easy".

    0 讨论(0)
提交回复
热议问题