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

后端 未结 19 2112
萌比男神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:45

    On Debian 7 I was still unable to correctly set the sender address using answers from this question, (would always be the hostname of the server) but resolved it this way.

    Install heirloom-mailx

    apt-get install heirloom-mailx
    

    ensure it's the default.

    update-alternatives --config mailx
    

    Compose a message.

    mail -s "Testing from & replyto" -r "sender <sender@example.com>" -S replyto="sender@example.com" recipient@example.net < <(echo "Test message")
    
    0 讨论(0)
  • 2020-12-12 14:47

    I derived this from all the above answers. Nothing worked for me when I tried each one of them. I did lot of trail and error by combining all the above answers and concluded on this. I am not sure if this works for you but it worked for me on Ununtu 12.04 and RHEL 5.4.

    echo "This is the body of the mail" | mail -s 'This is the subject' '<receiver-id1@email.com>,<receiver-id2@email.com>' -- -F '<SenderName>' -f '<from-id@email.com>'
    

    One can send the mail to any number of people by adding any number of receiver id's and the mail is sent by SenderName from from-id@email.com

    Hope this helps.

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

    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.

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

    On CentOS this worked for me:

    echo "email body" | mail -s "Subject here" -r from_email_address email_address_to
    
    0 讨论(0)
  • 2020-12-12 14:49

    I don't know if it's the same with other OS, but in OpenBSD, the mail command has this syntax:

    mail to-addr ... -sendmail-options ...
    

    sendmail has -f option where you indicate the email address for the FROM: field. The following command works for me.

    mail recepient@example.com -f from@example.com
    
    0 讨论(0)
  • 2020-12-12 14:50

    What allowed me to have a custom reply-to address on an Ubuntu 16.04 with UTF-8 encoding and a file attachment:

    Install the mail client:

    sudo apt-get install heirloom-mailx
    

    Edit the SMTP configuration:

    sudo vim /etc/ssmtp/ssmtp.conf
    mailhub=smtp.gmail.com:587
    FromLineOverride=YES
    AuthUser=???@gmail.com
    AuthPass=???
    UseSTARTTLS=YES
    

    Send the mail:

    sender='send@domain.com'
    recipient='recipient@domain.com'
    zipfile="results/file.zip"
    today=`date +\%d-\%m-\%Y`
    mailSubject='My subject on the '$today
    read -r -d '' mailBody << EOM
    Find attached the zip file.
    
    Regards,
    EOM
    mail -s "$mailSubject" -r "Name <$sender>" -S replyto="$sender" -a $zipfile $recipient < <(echo $mailBody)
    
    0 讨论(0)
提交回复
热议问题