1
我有一個應該通過Gmail發送電子郵件的聯繫頁面。無法通過Gmail發送電子郵件
下面是代碼:
func sendContactEmail(subject string, email string, message string) {
auth := smtp.PlainAuth(
"contact form submit",
"[email protected]",
"mypassword",
"smtp.gmail.com",
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
body := subject + "\r\n" + email +"\r\n" + message
msg := "Subject: Contact us" + "\r\n\r\n" + body + "\r\n"
err := smtp.SendMail(
"smtp.gmail.com:587",
auth,
"[email protected]",
[]string{"[email protected]"},
[]byte(msg),
)
if err != nil {
log.Fatal(err)
}
return
}
func ContactPOST(w http.ResponseWriter, r *http.Request) {
// Get form values
subject := r.FormValue("subject")
email := r.FormValue("email")
message := r.FormValue("message")
go sendContactEmail(subject, email, message)
// Display the thank you page
v := view.New(r)
v.Name = "contact/thanks"
v.Render(w)
return
}
這裏是錯誤消息:
contact.go:41: 534 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbti
5.7.14 HcGWI2H6QjVTNjHS4X49PcBxQQGNhL9TKnzdQxqYgeUXkWxpHj90RSAaIbI-ySSrKFTV4q
5.7.14 IVZeXExVeqhuZnPhtvUtx9p5Ly7gBxwFLzrrgWcm4NZ3_vhDOWiH-uDsPb5eoa4rbYCepd
5.7.14 PlD9kBBz1dAlhdRDJ7mwqsUMJUV7MHTgNWqTcT_R89Uq9oYtwurtmGAuv2YAkPTCBtPwXq
5.7.14 9ooL5edn_sTI6WJW72sK2ilMCIUB0> Please log in via your web browser and
5.7.14 then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 63sm17219759wmg.2 - gsmtp
奇怪的是,表格工作正常,當我自己的計算機上發送電子郵件,和只有在顯示thank you
頁面後http服務器剛剛死亡的VPS上部署應用程序時纔會發生錯誤。
我也試過在沒有goroutine的情況下調用sendContactEmail
,但仍然得到相同的錯誤。
我很感激你的提示來解決這個問題。
那麼,我使用個人帳戶(而不是G套件),我設置了「訪問不太安全的應用程序」。 – graco
更新:按照谷歌的故障排除鏈接建議訪問此頁面'https:// accounts.google.com/DisplayUnlockCaptcha'解決了這個問題。請更新您的答案,我會接受它。 – graco