2012-07-26 872 views
1

我試圖通過標準庫SMTP包發送郵件,它完全沒有10分鐘,然後失敗,無用的文件結束錯誤我無法理解。smtp.SendMail 10分鐘後與EOF失敗

// excerpt from the calling function 
// --- snip --- 
if e := mailNotify(board, validPosts, ipMinusPort, delTime); e != nil { 
    errorPage(w, tmpl, "Contact Administrator", 
     "Email notification failed, please contact archive admin.") 
    log.Println("ERROR: Failed to send notification email: " + e.Error()) 
} 
// --- snip --- 

func mailNotify(board *config.Board, validPosts []int64, 
         ip string, delTime time.Time) error { 

    addresses := strings.Split(board.NotifyAddr, ",") 
    if len(addresses) < 1 { 
     return nil 
    } 

    msg := new(bytes.Buffer) 
    type values struct { 
     IP  string 
     Posts []int64 
     DelTime time.Time 
    } 
    t.ExecuteTemplate(msg, "post_reported", &values{ip, validPosts, delTime}) 
    auth:= smtp.PlainAuth("", board.NotifyAddr, board.NotifyPass, 
     strings.Split(board.SMTPServer, ":")[0]) 
    return smtp.SendMail(board.SMTPServer, auth, 
     board.FromEmail, addresses, msg.Bytes()) 
} 

是被記錄的確切消息是:

2012/07/25 22:57:58 ERROR: Failed to send notification email: EOF

log.Printf調試該功能並驗證在這一點上所發送的值是有效的。電子郵件地址是gmail.com上的真實地址,密碼正確,board.SMTPServersmtp.googlemail.com:465。我已經嘗試將msg.Bytes()的結果存儲在一個單獨的變量中,並將長度確定爲生成要發送的字節數組,並且長度確實非零。我猜EOF從標準庫中的SendMail函數本身更深層的東西冒出來,但我不知道它在哪裏窒息,我不知道我做了什麼來擾亂它。

+0

這裏沒有什麼可以繼續下去的。你能否啓用庫的調試日誌記錄?有沒有網絡流量? – tripleee 2012-07-26 07:48:37

回答

11

Gmail的端口465用於通過TLS進行連接,但SendMail預計普通的舊TCP。嘗試連接到端口587。 SendMail會在可用時自動升級到TLS(在這種情況下)。

+0

我正要將我的頭埋在牆上。錯誤中沒有描述。它在說「EOF」。您的解決方案完美運作 – 2017-04-26 10:50:43