Is it possible to send mails by bash script via smtp?
Asked Answered
O

9

21

I have postfix+dovecot. I want to make bash script which can use SMTP for this. I don't want use sendmail.

Is it possible? May be someone has some examples of code?

Objurgate answered 3/4, 2012 at 17:56 Comment(3)
Why don't you want to use a SMTP client?Asteroid
What's wrong with postfix' sendmail wrapper?Goles
unix.stackexchange.com/questions/36982/… might answer your question . Ssmtp.Bensen
B
27

Boy, when that gauntlet is thrown, it always bashes me right upside the head! :-)

#!/bin/sh

function checkStatus {
  expect=250
  if [ $# -eq 3 ] ; then
    expect="${3}"
  fi
  if [ $1 -ne $expect ] ; then
    echo "Error: ${2}"
    exit
  fi
}

MyHost=`hostname`

read -p "Enter your mail host: " MailHost
MailPort=25

read -p "From: " FromAddr

read -p "To: " ToAddr

read -p "Subject: " Subject

read -p "Message: " Message

exec 3<>/dev/tcp/${MailHost}/${MailPort}

read -u 3 sts line
checkStatus "${sts}" "${line}" 220

echo "HELO ${MyHost}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "MAIL FROM: ${FromAddr}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "RCPT TO: ${ToAddr}" >&3

read -u 3 sts line
checkStatus "$sts" "$line"

echo "DATA" >&3

read -u 3 sts line
checkStatus "$sts" "$line" 354

echo "Subject: ${Subject}" >&3
echo "${Message}" >&3
echo "." >&3

read -u 3 sts line
checkStatus "$sts" "$line"
Babbage answered 3/4, 2012 at 21:3 Comment(3)
This seems that I look for. Thanks.Objurgate
glad I could demonstrate this for you!Babbage
the to in the mail client is empty with thisOrmiston
R
18

Have just found this tiny but wonderful utility sendemail (not sendmail). The syntax is too simple to explain.

Example:

SERVER="smtp.company.com"
FROM="[email protected]"
TO="[email protected]"
SUBJ="Some subject"
MESSAGE="Some message"
CHARSET="utf-8"

sendemail -f $FROM -t $TO -u $SUBJ -s $SERVER -m $MESSAGE -v -o message-charset=$CHARSET

More info available through help or at the author's site: https://github.com/mogaal/sendemail.

Ryley answered 19/10, 2016 at 15:0 Comment(0)
J
9

Tested with gmail and it currently works.

#!/bin/bash
# Use "host -t mx yourispdomain" to find out yourispmailserver
exec 1<>/dev/tcp/yourispmailserver/25
a=$(cat <<"MAILEND"
HELO local.domain.name
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
From: [email protected]
To: [email protected]
Subject: test
send your orders for pizza to the administrator.
.
QUIT
.
MAILEND
)
IFS='
'
declare -a b=($a)
for x in "${b[@]}"
 do
   echo $x
   sleep 1
 done
Josh answered 3/4, 2012 at 21:55 Comment(0)
A
3
  • Install sSMTP, for instance:

    apt-get install ssmtp

  • Configure ssmtp:

    sudo nano /etc/ssmtp/ssmtp.conf

    · Server: mailhub=smtp.1und1.de:587

    · Hostname: hostname=subdomain.domain.com

    · User: [email protected]

    · Pass: AuthPass=your_password

Then in your sh file, do what you need and pipe it to mail, for instance:

#!/bin/bash du -sh | mail -s "Disk usage report" [email protected]

OR

#!/bin/bash echo "Today's DB backup is ok." | mail -s "DB daily backup alert" [email protected]

Abstergent answered 9/10, 2017 at 8:36 Comment(1)
Getting error: sSMTP[19315]: 554 Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1Semifinalist
M
1

It's not clear to me when you say that you don't want to use sendmail. May be you don't want to use the sendmail process.

Postfix has an executable called "sendmail", and may be you could want to use it because I cannot think why you should not.

#/bin/bash

FROM='[email protected]'
TO='[email protected]'
SUBJECT='This is a test message'

BODY="This is a test mail message body.
Hi there.
"

printf "From: <%s>\nTo: <%s>\nSubject: %s\n\n%s" "$FROM" "$TO" "$SUBJECT" "$BODY" | sendmail -f "$FROM"
Maghutte answered 3/4, 2012 at 21:58 Comment(1)
I agree, may be I'm not say clear. I mean, that I want to know about possibility of bash script to use smtp auth to connect to remote smtp server and send mail.Objurgate
M
1

You could use SSMTP. Maybe this one helps too:

http://tecadmin.net/send-email-smtp-server-linux-command-line-ssmtp/

Mia answered 22/8, 2016 at 11:2 Comment(1)
SSMTP allowed to send emails via SMTP server as well. There are anohter option to use sendEmail command. tecadmin.net/send-email-from-gmail-smtp-via-linux-command or tecadmin.net/ways-to-send-email-from-linux-command-lineUncounted
F
1

Also wanted to throw in another solution here, useful if you don't have access to sendmail etc. Most linux servers will have telnet, so we can use that.

function send_email{

echo "EHLO my_mail_server.example.com"   # replace this with your mail server domain
sleep 1
echo "MAIL FROM: [email protected]
sleep 1
echo "RCPT TO: [email protected]
sleep 1
echo "DATA
sleep 1
echo "Subject: My Subject Line"
sleep 1
echo "To: [email protected]"
sleep 1
echo "From: [email protected]"
sleep 1
echo "This is the body of my email. Put whatever in here."
sleep 1
echo "." # signifies the end of data entry, mail will be queued to send.
}

send_email | telnet 1.2.3.4 25 # call the function and pass it into telnet command.
Femmine answered 2/6, 2020 at 8:23 Comment(1)
I am going to use this solution. ThanksDeprave
F
0

You want bash to talk directly to an SMTP server? That's not really going to happen. It might technically be possible using the support for network communication available in bash, but realistically you don't want to go down that path.

That means that what you really need is to call an external program that will take of SMTP for you. Typically, that's going to be sendmail, but if you're trying to avoid that there are lots of other alternatives, including:

Both of these can handle communication with a remote SMTP server without involving sendmail.

Fairground answered 3/4, 2012 at 18:5 Comment(1)
people often do because sendmail, msmtp. postfix etc may not be installed and configuredAudiophile
N
0

I love dldnh's answer!

Perfect solution for sending administrative alerts via your dedicated smtp relay (I know you've got one). No sense putting smtp/Postfix/etc on more than one server, right?

I do have one suggestion: Send one blank line before your message text to let the relay know that no more headers are coming. Without the blank line, the text of your message may not be sent; for example if there is a colon in that first line of the message.

Here is a small rewrite I took liberties with:

#!/bin/bash
MyHost=$(hostname)
MailHost="mail"
MailPort=25

function checkStatus {
  read -u 3 sts line
  expect=250
  if [ $# -eq 1 ] ; then
    expect="${1}"
  fi
  if [ $sts -ne $expect ] ; then
    echo "Error: ${line}"
    exit
  fi
}

FromAddr="$(whoami)@${MyHost}"
ToAddr="[email protected]"
Subject="Status Alert"
Message='Msg: hello bozo!!'

# Brilliant!!
exec 3<>/dev/tcp/${MailHost}/${MailPort} ; checkStatus 220

echo "HELO ${MyHost}" >&3 ; checkStatus
echo "MAIL FROM: ${FromAddr}" >&3 ; checkStatus
echo "RCPT TO: ${ToAddr}" >&3 ; checkStatus
echo "DATA" >&3 ; checkStatus 354
echo "Subject: ${Subject}" >&3

# Insert one blank line here to let the relay know you are done sending headers
# Otherwise a colon ":" in the message text will result in no text being sent.
echo "" >&3

# Send the message text and close
echo "${Message}" >&3
echo "." >&3 ; checkStatus
Newton answered 21/1, 2020 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.