2016-12-19 146 views
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,但仍然得到相同的错误。

我很感激你的提示来解决这个问题。

回答

1

我的组织也有类似的问题。我们必须在Gmail上登录到我们的管理邮件控制台,并打开我们IP的smtp中继访问权限,以便不再需要Web认证。看到这里寻求帮助。

https://support.google.com/a/answer/2956491?hl=en


还赞赏澄清。对于个人帐户,您绝对正确,您可以转到该链接(https://accounts.google.com/DisplayUnlockCaptcha),并且能够通过邮件中继获得适当的身份验证。

+0

那么,我使用个人帐户(而不是G套件),我设置了“访问不太安全的应用程序”。 – graco

+0

更新:按照谷歌的故障排除链接建议访问此页面'https:// accounts.google.com/DisplayUnlockCaptcha'解决了这个问题。请更新您的答案,我会接受它。 – graco

相关问题