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

时间:2012-04-03 17:56:11

标签: bash smtp postfix-mta

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

有可能吗?可能有人有一些代码示例吗?

9 个答案:

答案 0 :(得分:15)

男孩,当那个手套被抛出时,它总是bash让我站起来! : - )

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

答案 1 :(得分:7)

使用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: <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

答案 2 :(得分:4)

刚刚发现了这个微不足道的实用工具sendemail(不是sendmail)。语法太简单了,无法解释。

示例:

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

通过帮助或在作者网站上提供更多信息:http://caspian.dotconf.net/menu/Software/SendEmail/

答案 3 :(得分:2)

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

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

这两种方法都可以处理与远程SMTP服务器的通信,而不涉及sendmail。

答案 4 :(得分:1)

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

Postfix有一个名为“sendmail”的可执行文件,你可能想要使用它,因为我不明白为什么你不应该这样做。

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

答案 5 :(得分:1)

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

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

答案 6 :(得分:0)

  • 安装sSMTP,例如:

    apt-get install ssmtp

  • 配置ssmtp:

    sudo nano /etc/ssmtp/ssmtp.conf

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

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

    ·用户:AuthUser=user@domain.com

    ·通过:AuthPass=your_password

然后在您的sh文件中,执行您需要的操作并将其传递给邮件,例如:

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

答案 7 :(得分:0)

我喜欢dldnh的答案!

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

我确实有一个建议:在您的消息文本之前发送一个空白行,以使中继知道不再有头文件了。如果没有空白行,则可能不会发送您的消息文本。例如,如果消息的第一行中有一个冒号。

这是我随心所欲的小改写:

#!/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="admin@SomeDomain.com"
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

答案 8 :(得分:0)

还想在这里添加其他解决方案,如果您无权访问sendmail等,则很有用。大多数Linux服务器都具有telnet,因此我们可以使用它。

function send_email{

echo "EHLO my_mail_server.example.com"   # replace this with your mail server domain
sleep 1
echo "MAIL FROM: my_from_address@example.com
sleep 1
echo "RCPT TO: my_to_address@example.com
sleep 1
echo "DATA
sleep 1
echo "Subject: My Subject Line"
sleep 1
echo "To: my_to_address@example.com"
sleep 1
echo "From: my_from_address@example.com"
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.