运行以下脚本:
#!/bin/bash
cvs_domain=abc.com
cvs_mail_server=mail.${cvs_domain}
cvs_port=25
telnet $cvs_mail_server $cvs_port<<_EOF_
EHLO $cvs_domain
MAIL FROM:[email protected]
RCPT TO:[email protected]
DATA
Subject:Test!
Don't panic. This is only a test.
.
QUIT
_EOF_
Connection closed by host
在服务器用转义字符回复之后、发送消息之前,出现一条消息失败220
。
以交互模式运行相应的序列(当然,没有“here-doc”)就实现了我的目标。
我怀疑将命令行“馈送”到服务器的过程并没有完全按照线路另一端的预期进行。
我的假设正确吗? 有没有办法缓解这个问题?
答案1
当您需要编写一个交互式命令行工具脚本时,典型的解决方案是使用expect(1)
。
答案2
为了完整起见,我在这里发布了完整的“丑陋但有效”的解决方案(修改后的最终形式是,它会向更多人发送电子邮件,并提供附件):
cd "$(dirname "$0")"
working_dir=$(pwd) # switching to the folder this script has been started from
cvs_domain=mail.org
cvs_mail_server=mail.${cvs_domain}
cvs_port=25
[email protected]
cvs_recipients=([email protected] [email protected])
cvs_delimiter=-----nEXt_paRt_frontier!!VSFCDVGGERHERZZ@$%^zzz--- # MIME multi-part delimiter, do not change
{ echo HELO $cvs_domain; sleep 1
# set up the email (sender, receivers):
echo MAIL FROM:$cvs_sender; sleep 1
for r in ${cvs_recipients[@]}; do
echo RCPT TO:$r; sleep 1
done
echo DATA; sleep 1
echo From:$cvs_sender; sleep 1
for r in ${cvs_recipients[@]}; do
echo To:$r; sleep 1
done
echo Subject:Test for build; sleep 1
# build the mail structure, according to the MIME standard:
echo MIME-Version: 1.0; sleep 1
echo "Content-Type: multipart/mixed; boundary=\"$cvs_delimiter\""; sleep 1
echo --${cvs_delimiter}; sleep 1
echo Content-Type: text/plain; sleep 1
echo; sleep 1
echo Don\'t panic. This is only a test.; sleep 1
echo; sleep 1
echo --${cvs_delimiter}; sleep 1
echo "Content-Type: text/plain; name=\"test.txt\""; sleep 1
echo "Content-Disposition: attachment; filename=\"test.txt\""; sleep 1
echo "Content-Transfer-Encoding: base64"; sleep 1
echo; sleep 1
encoded_file=$( base64 ./change.log ) # encoding the contents of the file, according to the declaration above
echo "$encoded_file"; sleep 1
echo; sleep 1
echo --${cvs_delimiter}; sleep 1
echo .; sleep 1
echo QUIT
sleep 1; } | telnet $cvs_mail_server $cvs_port
有人可能会选择摆弄延迟。而对于(我认为可能是)更强大的解决方案,我会选择expect(1)
。