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.
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.
echo "this is the body" | mail -s "this is the subject" "to@address"
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
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')
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"
If you want to attach a file on Linux
echo 'mail content' | mailx -s 'email subject' -a attachment.txt [email protected]
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" [email protected] <<< "how are you?"<br>
For SMTP hosts and Gmail I like to use Swaks -> https://easyengine.io/tutorials/mail/swaks-smtp-test-tool/
On a Mac:
brew install swaks
swaks --to [email protected] --server smtp.example.com
I think swaks is the best. Here you have more complicated example, using TLS encryption on port 25:
swaks --from [email protected] \
--h-From: '"John Smith" <[email protected]>' \
--h-Subject: 'Subject of message' \
--auth LOGIN --auth-user mylogin --auth-pass mypass \
--to [email protected] \
--server smtp.example.com --port 25 -tls \
--add-header 'Content-Type: text/plain; charset="utf-8"'
I was able to send a multiline message using mailx
with the following shell script:
#!/bin/sh
[email protected]
MSG_TO="[email protected]"
MSG_SUBJ="Test"
SMTP_HOST="relay.zac.it"
MSG_BODY=(
"Line one"
""
"Line two")
printf '%s\n' "${MSG_BODY[@]}" | mailx -v -s "$MSG_SUBJ" -S smtp="smtp://$SMTP_HOST" -S from=$MSG_FROM $MSG_TO
I am under Red Hat Enterprise Linux Server release 6.4 (Santiago)