是否可以通过 smtp 通过 bash 脚本发送邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9998710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:54:34  来源:igfitidea点击:

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

bashsmtppostfix-mta

提问by Jason

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

我有 postfix+dovecot。我想制作可以为此使用 SMTP 的 bash 脚本。我不想使用sendmail。

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

是否可以?可能有人有一些代码示例?

回答by dldnh

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

男孩,当那个手套被抛出时,它总是在bash我的头上!:-)

#!/bin/sh

function checkStatus {
  expect=250
  if [ $# -eq 3 ] ; then
    expect=""
  fi
  if [  -ne $expect ] ; then
    echo "Error: "
    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"

回答by pizza

Tested with gmail and it currently works.

用 gmail 测试过,目前有效。

#!/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

回答by n01d

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

刚刚发现了这个微小但很棒的实用程序sendemail(不是sendmail)。语法太简单了,无法解释。

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: http://caspian.dotconf.net/menu/Software/SendEmail/.

更多信息可通过帮助或作者的站点获得:http: //caspian.dotconf.net/menu/Software/SendEmail/

回答by achasinh

  • 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

  • 安装 sSMTP,例如:

    apt-get install ssmtp

  • 配置ssmtp:

    sudo nano /etc/ssmtp/ssmtp.conf

    · 服务器: mailhub=smtp.1und1.de:587

    · 主机名: hostname=subdomain.domain.com

    · 用户: [email protected]

    · 经过: AuthPass=your_password

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

然后在您的 sh 文件中,执行您需要的操作并将其通过管道发送到邮件,例如:

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

#!/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]

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

回答by larsks

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

您想bash直接与 SMTP 服务器通话吗?这不会真的发生。使用 bash 中可用的网络通信支持在技术上可能是可能的,但实际上您不想走这条路。

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:

这意味着您真正需要的是调用一个外部程序来为您处理 SMTP。通常情况下,这将是sendmail,但如果您想避免这种情况,还有很多其他选择,包括:

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

这两者都可以在不涉及 sendmail 的情况下处理与远程 SMTP 服务器的通信。

回答by dAm2K

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.

我不清楚你说你不想使用sendmail。可能是您不想使用 sendmail 进程。

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

Postfix 有一个名为“sendmail”的可执行文件,您可能想要使用它,因为我想不出为什么您不应该使用它。

#/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"

回答by Hafenkranich

You could use SSMTP. Maybe this one helps too:

您可以使用 SSMTP。也许这也有帮助:

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

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

回答by Frobozz

I love dldnh's answer!

我喜欢 dldnh 的回答!

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?

通过您的专用 smtp 中继发送管理警报的完美解决方案(我知道您有一个)。将 smtp/Postfix/etc 放在多个服务器上是没有意义的,对吧?

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