Is it possible to send mails by bash script via sm

2019-03-27 04:26发布

问题:

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?

回答1:

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"


回答2:

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


回答3:

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/.



回答4:

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:

  • msmtp
  • Heirloom mailx

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



回答5:

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"


回答6:

You could use SSMTP. Maybe this one helps too:

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



回答7:

  • 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