在App Engine上使用Go语言的联系表单邮件处理程序示例

huangapple go评论114阅读模式
英文:

Contact form mail handler example for go on appengine

问题

令我惊讶的是,我没有找到一个关于Go语言的联系表单邮件处理器的示例。我今天不想重复造轮子,是否有现成的示例可用?

编辑:(剪切和粘贴的答案)

  1. package bin
  2. import (
  3. "fmt"
  4. "net/http"
  5. netMail "net/mail"
  6. "appengine"
  7. "appengine/mail"
  8. )
  9. func contact(w http.ResponseWriter, r *http.Request) {
  10. c := appengine.NewContext(r)
  11. name := r.FormValue("name")
  12. email := r.FormValue("email")
  13. subject := r.FormValue("subject")
  14. message := r.FormValue("message")
  15. msg := &mail.Message{
  16. Sender: name + " <...@yourappid.appspotmail.com>",
  17. To: []string{"...@..."},
  18. ReplyTo: email,
  19. Subject: subject,
  20. Body: message,
  21. Headers: netMail.Header{
  22. "On-Behalf-Of": []string{email},
  23. },
  24. }
  25. if err := mail.Send(c, msg); err != nil {
  26. c.Errorf("Couldn't send email: %v", err)
  27. fmt.Fprint(w, "Mail NOT send! Error")
  28. } else {
  29. fmt.Fprint(w, "Mail send.")
  30. }
  31. }

注意:

1)如果SenderTo不同,ReplyTo只在Gmail中起作用。

2)Sender应该在Google Cloud控制台中拥有管理员角色,或者是something@yourappid.appspotmail.com

英文:

To my surprise I did not find a contact form mail handler example for go? I don't feel like making a wheel today, are there examples available?

EDIT: (cut and paste answer)

  1. package bin
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. netMail &quot;net/mail&quot;
  6. &quot;appengine&quot;
  7. &quot;appengine/mail&quot;
  8. )
  9. func contact(w http.ResponseWriter, r *http.Request) {
  10. c := appengine.NewContext(r)
  11. name := r.FormValue(&quot;name&quot;)
  12. email := r.FormValue(&quot;email&quot;)
  13. subject := r.FormValue(&quot;subject&quot;)
  14. message := r.FormValue(&quot;message&quot;)
  15. msg := &amp;mail.Message{
  16. Sender: name + &quot; &lt;...@yourappid.appspotmail.com&gt;&quot;,
  17. To: []string{&quot;...@...&quot;},
  18. ReplyTo: email,
  19. Subject: subject,
  20. Body: message,
  21. Headers: netMail.Header{
  22. &quot;On-Behalf-Of&quot;: []string{email},
  23. },
  24. }
  25. if err := mail.Send(c, msg); err != nil {
  26. c.Errorf(&quot;Couldn&#39;t send email: %v&quot;, err)
  27. fmt.Fprint(w, &quot;Mail NOT send! Error&quot;)
  28. }else{
  29. fmt.Fprint(w, &quot;Mail send.&quot;)
  30. }
  31. }

NOTE:

  1. ReplyTo only works in gmail if Sender and To are different.

  2. Sender should have admin role in google cloud console or something@yourappid.appspotmail.com.

答案1

得分: 3

这很可能失败是因为Sender只能是以下两种情况之一:

  • 你的应用程序开发者的电子邮件,或者
  • something@yourappid.appspotmail.com

我建议你硬编码发送者的电子邮件,并在其中使用On-Behalf-Of头部,其中包括原始发送者的姓名/电子邮件。

另外,WriteString只接受一个string,而不是[]string切片。

对于你的示例,最小的修改应该是:

  1. msg := &amp;mail.Message{
  2. Sender: name + &quot; &lt;developer@yourapp.com&gt;&quot;,
  3. To: []string{&quot;...@gmail.com&quot;},
  4. Subject: subject,
  5. Body: message,
  6. Headers: netMail.Header{
  7. &quot;On-Behalf-Of&quot;: []string{email},
  8. },
  9. }

另外,你需要确保用户的姓名实际上不包含电子邮件地址。那可能会给你带来问题...

最好按照 @elithrar 的建议验证你的表单。

英文:

This is most likely failing because the Sender can only be

  • an email of a developer of you app, or
  • something@yourappid.appspotmail.com

I suggest that you hard-code the sender's email, and use the On-Behalf-Of header in which you include the original sender's name/email.

Also, WriteString accepts a single string, not a []string slice.

The minimum modifications for your example would be:

  1. msg := &amp;mail.Message{
  2. Sender: name + &quot; &lt;developer@yourapp.com&gt;&quot;,
  3. To: []string{&quot;...@gmail.com&quot;},
  4. Subject: subject,
  5. Body: message,
  6. Headers: netMail.Header{
  7. &quot;On-Behalf-Of&quot;: []string{email},
  8. },
  9. }

Also, you'll need to make sure the user's name doesn't actually contain an email address. That could cause you problems…

The best would be to do as @elithrar suggested and validate your form.

huangapple
  • 本文由 发表于 2013年12月5日 10:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/20390454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定