bash - How to send email from Terminal?

ID : 20254

viewed : 14

Tags : bashemailterminalbash

Top 5 Answer for bash - How to send email from Terminal?

vote vote

96

echo "this is the body" | mail -s "this is the subject" "to@address" 
vote vote

80

Go into Terminal and type man mail for help.

You will need to set SMTP up:

http://hints.macworld.com/article.php?story=20081217161612647

See also:

http://www.mactricksandtips.com/2008/09/send-mail-over-your-network.html

Eg:

mail -s "hello" "example@example.com" <<EOF hello world EOF 

This will send an email to example@example.com with the subject hello and the message

Hello

World

vote vote

79

Probably the simplest way is to use curl for this, there is no need to install any additional packages and it can be configured directly in a request.

Here is an example using gmail smtp server:

curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \   --mail-from 'from-email@gmail.com' \   --mail-rcpt 'to-email@gmail.com' \   --user 'from-email@gmail.com:YourPassword' \   -T <(echo -e 'From: from-email@gmail.com\nTo: to-email@gmail.com\nSubject: Curl Test\n\nHello') 
vote vote

63

If all you need is a subject line (as in an alert message) simply do:

mailx -s "This is all she wrote" < /dev/null "myself@myaddress" 
vote vote

59

If you want to attach a file on Linux

echo 'mail content' | mailx -s 'email subject' -a attachment.txt username@stackoverflow.com 

Top 3 video Explaining bash - How to send email from Terminal?

Related QUESTION?