How to send email from Terminal?

BashEmailTerminal

Bash Problem Overview


I know there are ways to send email from terminal in Linux/MacOS, but I can't seem to find proper documentation on how to do that.

Basically I need it for my bash script that notifies me every time there is a change in a file.

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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" "[email protected]" <<EOF
hello
world
EOF

This will send an email to [email protected] with the subject hello and the message

> Hello > > World

Solution 3 - Bash

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 '[email protected]' \
  --mail-rcpt '[email protected]' \
  --user '[email protected]:YourPassword' \
  -T <(echo -e 'From: [email protected]\nTo: [email protected]\nSubject: Curl Test\n\nHello')

Solution 4 - Bash

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"

Solution 5 - Bash

If you want to attach a file on Linux

echo 'mail content' | mailx -s 'email subject' -a attachment.txt [email protected]

Solution 6 - Bash

in the terminal on your mac os or linux os type this code

mail -s (subject) (receiversEmailAddress)  <<< "how are you?"

for an example try this

mail -s "hi" abc@example.com <<< "how are you?"<br>

Solution 7 - Bash

For SMTP hosts and Gmail I like to use Swaks -> https://easyengine.io/tutorials/mail/swaks-smtp-test-tool/

On a Mac:

  1. brew install swaks
  2. swaks --to [email protected] --server smtp.example.com

Solution 8 - Bash

I think swaks is the best. Here you have more complicated example, using TLS encryption on port 25:

swaks --from john.smith@mydomain.com \
--h-From: '"John Smith" <[email protected]>' \
--h-Subject: 'Subject of message' \
--auth LOGIN --auth-user mylogin --auth-pass mypass \
--to someone@otherdomain.com \
--server smtp.example.com --port 25 -tls \
--add-header 'Content-Type: text/plain; charset="utf-8"'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoobDev4iPhoneView Question on Stackoverflow
Solution 1 - BashearldouglasView Answer on Stackoverflow
Solution 2 - BashgadgetmoView Answer on Stackoverflow
Solution 3 - BashAliaksandr SushkevichView Answer on Stackoverflow
Solution 4 - BashJRFergusonView Answer on Stackoverflow
Solution 5 - BashMiae KimView Answer on Stackoverflow
Solution 6 - BashPramodya AbeysingheView Answer on Stackoverflow
Solution 7 - BashTumView Answer on Stackoverflow
Solution 8 - BashAndrzeyView Answer on Stackoverflow