8

我正在尝试通过标准库 SMTP 包发送邮件,它在整整 10 分钟内什么都不做,然后失败并出现我无法理解的无用的文件结束错误。

// 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 错误:无法发送通知电子邮件:EOF

我已经log.Printf调试了这个函数并验证了此时发送的值是有效的。电子邮件地址是 gmail.com 上的真实地址,密码正确,并且board.SMTPServersmtp.googlemail.com:465. 我尝试将 msg.Bytes() 的结果存储在一个单独的变量中,并获取长度以确保它正在生成要发送的字节数组,并且长度确实非零。我猜 EOF 是从标准库中比 SendMail 函数本身更深的地方冒出来的,但我不知道它在哪里窒息,我不知道我做了什么来扰乱它。

4

1 回答 1

30

Gmail 的 465 端口用于通过 TLS 进行连接,但 SendMail 需要普通的旧 TCP。尝试连接到端口 587。SendMail 将在可用时自动升级到 TLS(在这种情况下)。

于 2012-07-26T07:08:24.277 回答