我有 postfix+dovecot。我想制作可以使用 SMTP 的 bash 脚本。我不想使用 sendmail。
可能吗?也许有人有一些代码示例?
男孩,当那个挑战被抛出时,它总是
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"
刚刚发现了这个微小但美妙的实用程序
sendemail
(不是sendmail
)。语法太简单解释不了。
示例:
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
可通过帮助或作者网站获取更多信息: https://github.com/mogaal/sendemail。
已使用 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
安装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
然后在您的 sh 文件中,执行您需要的操作并将其通过管道发送到邮件,例如:
#!/bin/bash
du -sh | mail -s "Disk usage report" [email protected]
或
#!/bin/bash
echo "Today's DB backup is ok." | mail -s "DB daily backup alert" [email protected]
当你说你不想使用sendmail时,我不清楚。可能您不想使用 sendmail 进程。
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"
您可以使用 SSMTP。也许这个也有帮助:
http://tecadmin.net/send-email-smtp-server-linux-command-line-ssmtp/
还想在这里提出另一个解决方案,如果你无法访问 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: [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.
通过专用 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="[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