The mail command can be used under Linux or UNIX bash / ksh / csh shell to send an email. To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent. You are then expected to type in your message, followed by an ‘control-D’ at the beginning of a line. However, using the following syntax one can send email easily:
mail -s 'Subject' you@example.com
Sample Shell Script
Here is what you need to put in a shell script:
#!/bin/bash # script to send simple email # email subject SUBJECT="SET-EMAIL-SUBJECT" # Email To ? EMAIL="admin@somewhere.com" # Email text/message EMAILMESSAGE="/tmp/emailmessage.txt" echo "This is an email message test"> $EMAILMESSAGE echo "This is email text" >>$EMAILMESSAGE # send an email using /bin/mail /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
See also: Sending mail with attachment from command line/shell
How about putting this at the end of a bash file so it emails an attachment? My script would run and dump the output into > /file.txt How can I paste this script to attach the file and email it or grab the console output and email that instead of an attachment?
How can we get the output of a query in html table so that it can be sent through mail in shell script
Works fine! Thanks
Hello admin.script not working,What can I do?
Thanks
sir, This script runs successfully ! but i am not recieving any mails in my account ! plz help ! :D
sir, This script runs successfully ! but i am not recieving any mails in my account ! plz help ! :D
its not working
This breaks as soon as the input contains a char not in ASCII. I am currently looking for sending correct emails from command line, but this command seems to process the text byte by byte which is not correct if it is not ASCII.
One easy way around the issue with unicode is to specify the correct encoding. If the current locale uses UTF, adding
to mail seems to do the trick.-a "Content-Type: text/plain; charset=UTF-8"