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

送分小仙女□ 提交于 2019-12-03 11:54:11

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"
pizza

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: <me@local.domain.name>
RCPT TO: <you@local.domain.name>
DATA
From: me@local.domain.name
To: you@local.domain.name
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

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

Example:

SERVER="smtp.company.com"
FROM="sender@company.com"
TO="recepient@company.com"
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: http://caspian.dotconf.net/menu/Software/SendEmail/.

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.

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='from@test.com'
TO='to@test.com'
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"

You could use SSMTP. Maybe this one helps too:

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

  • 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: AuthUser=user@domain.com

    · 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" user@domain.com

OR

#!/bin/bash echo "Today's DB backup is ok." | mail -s "DB daily backup alert" user@domain.com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!