215

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.

NoobDev4iPhone
  • 5,531
  • 10
  • 33
  • 33
  • 4
    It should probably be noted here that some hosts and ISPs "turn off" the ability for you to send email. I presume it's an attempt to stop people spamming and phishing through them – Jim Jeffries Nov 24 '11 at 18:25
  • @JimJeffries Which ones? I am using an Amazon EC2 web server. – ckjbgames Feb 24 '17 at 16:58

9 Answers9

153
echo "this is the body" | mail -s "this is the subject" "to@address"
earldouglas
  • 13,265
  • 5
  • 41
  • 50
140

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

Community
  • 1
  • 1
gadgetmo
  • 3,058
  • 8
  • 27
  • 41
  • I just tried, and it worked for me, but I have two questions out of my curiosity.1. What does << means, online I've seen some examples with '<' or '<<. ' What is it used for? And why you have given End Of File there and at the end of the message? – Deep Dec 12 '19 at 16:31
82

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')
Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
  • 1
    This is a really good option. You can generate an app specific password for your Google account and this just delivers the email from your own account. Very cool! – dakdad Sep 12 '19 at 07:58
  • @aliaksandr should add this last point to his answer. By default this option (give access to less secure apps) is disabled and it's not evident. At least if you're using gmail account as sender email. – EAmez Nov 26 '19 at 09:25
  • Very nice. Curl is another awesome swiss army knife like netcat :) – EdiD Aug 16 '20 at 08:41
  • How would you attach a file with this method? – Masoud Ghaderi Feb 11 '21 at 00:02
  • Didn't work for me, it printed this `curl: (67) Login denied` – H.B. Aug 11 '22 at 01:03
  • Note that this will only work for Gmail accounts when using app passwords (less secure apps is no longer a thing). https://support.google.com/accounts/answer/6010255?hl=en – Gary Aug 18 '22 at 20:07
42

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"
JRFerguson
  • 7,426
  • 2
  • 32
  • 36
23

If you want to attach a file on Linux

echo 'mail content' | mailx -s 'email subject' -a attachment.txt username@stackoverflow.com
Miae Kim
  • 1,713
  • 19
  • 21
  • 4
    It gives an `invalid header` message for my attached file. The correct option was an uppercase `-A`. – Stephane Jul 01 '17 at 06:59
  • 3
    @Stephane According to patrick-haugh, -a is the attachment switch. -A is for the account command. See the man page: https://linux.die.net/man/1/mailx – Miae Kim Jan 23 '18 at 21:34
8

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>
Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
Pramodya Abeysinghe
  • 1,098
  • 17
  • 13
  • Probably an unimportant nit, but it is not sufficient to do this "in the terminal". Many times when you are typing "in the terminal" you are not typing commands that will be executed by a shell, and this solution will only work if you type the above as input to a shell. – William Pursell Mar 27 '23 at 16:29
7

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 user@example.com --server smtp.example.com
Tum
  • 6,937
  • 2
  • 25
  • 23
1

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" <john.smith@mydomain.com>' \
--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"'
Andrzey
  • 21
  • 2
  • Got `*** TLS not available: requires Net::SSLeay. Exiting`. Any ides how to fix? UPD: [found](https://stackoverflow.com/a/55960237/1778275). – pmor Apr 19 '23 at 07:17
0

I was able to send a multiline message using mailx with the following shell script:

#!/bin/sh

MSG_FROM=zac@server.it
MSG_TO="zac@gmail.com"
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)

Zac
  • 4,510
  • 3
  • 36
  • 44