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

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

    echo "body" | mail -S from=address@foo.com "Hello"

    -S lets you specify lots of string options, by far the easiest way to modify headers and such.

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

    GNU mailutils's 'mail' command doesn't let you do this (easily at least). But If you install 'heirloom-mailx', its mail command (mailx) has the '-r' option to override the default '$USER@$HOSTNAME' from field.

    echo "Hello there" | mail -s "testing" -r sender@company.com recipient@company.com
    

    Works for 'mailx' but not 'mail'.

    $ ls -l /usr/bin/mail
    lrwxrwxrwx 1 root root 22 2010-12-23 08:33 /usr/bin/mail -> /etc/alternatives/mail
    $ ls -l /etc/alternatives/mail
    lrwxrwxrwx 1 root root 23 2010-12-23 08:33 /etc/alternatives/mail -> /usr/bin/heirloom-mailx
    
    0 讨论(0)
  • 2020-12-12 14:35

    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.

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

    On CentOS 5.5, the easiest way I've found to set the default from domain is to modify the hosts file. If your hosts file contains your WAN/public IP address, simply modify the first hostname listed for it. For example, your hosts file may look like:

    ...
    11.22.33.44 localhost default-domain whatever-else.com
    ...

    To make it send from whatever-else.com, simply modify it so that whatever-else.com is listed first, for example:

    ...
    11.22.33.44 whatever-else.com localhost default-domain
    ...

    I can't speak for any other distro (or even version of CentOS) but in my particular case, the above works perfectly.

    0 讨论(0)
  • 2020-12-12 14:42
    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....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.

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

    Thanks BEAU

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

    I just found this and it works for me. The man pages for mail 8.1 on CentOS 5 doesn't mention this. For -f option, the man page says:

    -f Read messages from the file named by the file operand instead of the system mailbox. (See also folder.) If no file operand is specified, read messages from mbox instead of the system mailbox.

    So anyway this is great to find, thanks.

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