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
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")
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.
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.
On CentOS this worked for me:
echo "email body" | mail -s "Subject here" -r from_email_address email_address_to
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
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)