send email from terminal with attachment

علی ذوالفقار
1399/06/04 07:45:55 (894)
to install : yum install mailx
or
sudo apt-get install mailutils

to send mail using terminal :
send message only : 
echo "Message-Body" | mail -s "Message-Subject" receiver@address.com

send message with file attachment  : 
echo "Message-Body" | mail -s "Message-Subject" -r sender@my-server.com -a ~/my-file-to-send.txt Reciver@address.com

a bash script can help : 
this is my openvpn server send-mail script : sm.sh
#!/bin/sh
if [[ $1 == "" ]] || [[ $2 == "" ]]
then 
    echo 
    echo "./sm.sh [FILE_NAME] [RECIVER_ADDRESS]" # USAGE MESSAGE 
    echo 
else
    echo 	
    # send mail command :  echo "MESSAGE_BODY" | mail -s "MESSAGE_SUBJECT" -r SENDER_ADDRESS@SERVER.COM -a FILE_NAME RECIVER_ADDRESS
    echo "OPENVPN OVP FILE IS ATTACHED" | mail -s "OPENVPN OVP FILE" -r azolfaghar@gmail.com -a $1 $2
    # print message 
    echo "MAIL SEND TO ( $2 ) , FILE ( $1  ) IS ATTACHED TO THAT MESSAGE , THE MESSAGE MUST BE IN CERVICE SPAM FOLDER DUE TO INVALID EMAIL SENDER"
    echo 
fi 
Back