expect telnet 發送郵件

python通過smtplib鏈接到smtp.163.com可以正常發送,換perl就不行了。很詭異的事件啊。

perl程序大致代碼如下:

use Net::SMTP;
$smtp = Net::SMTP->new('smtp.163.com', Hello =>
'smtp.163.com' , Timeout => 120, Debug => 1);
$smtp->auth('[email protected]','xx') or die("-- $!");
# 部分代碼省略,具體可以看perldoc Net::SMTP


換了幾個測試環境都不通過,顯示認證失敗,還有命令錯誤。很靈異的是同樣的代碼在別人機器就可以正常使用,我的就不行不論是win還是linux,估計是perl版本問題。折騰的有點崩潰。而且perl裏MIME::Base64裏的base64函數之後會多加一個/n。

最後很邪惡的實現了,用的系統expect。
$ chmod a+x aa.ex
$ ./aa.ex stmp.163.com base64之後的用戶名 base64之後的密碼 frommail tomail title content


aa.ex腳本如下:

#!/usr/bin/expect
set smtp    [lindex $argv 0]
set user    [lindex $argv 1]
set pass    [lindex $argv 2]
set from    [lindex $argv 3]
set to      [lindex $argv 4]
set title   [lindex $argv 5]
set content [lindex $argv 6]
spawn telnet $smtp 25
expect "220"
send "helo root/r"
expect "250"
send "auth login/r"
expect "334"
send "$user/r"
expect "334"
send "$pass/r"
expect "235"
send "MAIL FROM: <$from>/r"
expect "250"
send "RCPT TO: <$to>/r"
expect "250"
send "DATA/r"
expect "354"
send "TO: $to/r"
send "FROM: $from/r"
send "SUBJECT: $title/r"
send "/r"
send "$content/r"
send "./r"
expect "250"
send "QUIT"

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章