在 OS X CLI 上使用 telnet 和 FTP 自動傳送電子郵件

在 OS X CLI 上使用 telnet 和 FTP 自動傳送電子郵件

運行以下腳本:

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

相關內容