dtnpzghys01643322 2018-06-01 20:39
浏览 77
已采纳

Golang SMTP发送空电子邮件

Why when I try to send email i'm received empty email?

    func contactHandler(w http.ResponseWriter, r *http.Request) {
        if r.Method == "GET" {
            t, _ := template.ParseFiles("template/static/contact.html")
            t.Execute(w, nil)
        } else if r.Method == "POST" {
            r.ParseForm()
            msg := "Name: " + r.Form["name"][0] + ", Email: " + r.Form["email"][0] + ", Message: " + r.Form["message"][0]
            println("out ", msg)
            smtp.SendMail("smtp.gmail.com:587", smtp.PlainAuth("", "login", "password", "smtp.gmail.com"), "sender@mail.com", []string{"me@mail.com"}, []byte(msg))
            http.Redirect(w, r, "/", 301)
        }
    }

But if I change []byte(msg) to []byte("TEST") I received email with TEST word

  • 写回答

1条回答 默认 最新

  • drxm72811 2018-06-02 02:22
    关注

    You can't make up the email header format as you please. The email header must follow a specific format defined in RFC2822. Try to build your variable 'msg' by using this format:

    from := fmt.Sprintf("From: <%s>
    ", "sender@gmail.com")
    to := fmt.Sprintf("To: <%s>
    ", "recipient@gmail.com")
    subject := "Subject: This is the subject
    "
    body := "This is the email body.
    Bye
    "
    
    msg := from+to+subject+"
    "+body
    

    See details in https://tools.ietf.org/html/rfc2822

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退