英文:
Contact form mail handler example for go on appengine
问题
令我惊讶的是,我没有找到一个关于Go语言的联系表单邮件处理器的示例。我今天不想重复造轮子,是否有现成的示例可用?
编辑:(剪切和粘贴的答案)
package bin
import (
"fmt"
"net/http"
netMail "net/mail"
"appengine"
"appengine/mail"
)
func contact(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
name := r.FormValue("name")
email := r.FormValue("email")
subject := r.FormValue("subject")
message := r.FormValue("message")
msg := &mail.Message{
Sender: name + " <...@yourappid.appspotmail.com>",
To: []string{"...@..."},
ReplyTo: email,
Subject: subject,
Body: message,
Headers: netMail.Header{
"On-Behalf-Of": []string{email},
},
}
if err := mail.Send(c, msg); err != nil {
c.Errorf("Couldn't send email: %v", err)
fmt.Fprint(w, "Mail NOT send! Error")
} else {
fmt.Fprint(w, "Mail send.")
}
}
注意:
1)如果Sender
和To
不同,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)
package bin
import (
"fmt"
"net/http"
netMail "net/mail"
"appengine"
"appengine/mail"
)
func contact(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
name := r.FormValue("name")
email := r.FormValue("email")
subject := r.FormValue("subject")
message := r.FormValue("message")
msg := &mail.Message{
Sender: name + " <...@yourappid.appspotmail.com>",
To: []string{"...@..."},
ReplyTo: email,
Subject: subject,
Body: message,
Headers: netMail.Header{
"On-Behalf-Of": []string{email},
},
}
if err := mail.Send(c, msg); err != nil {
c.Errorf("Couldn't send email: %v", err)
fmt.Fprint(w, "Mail NOT send! Error")
}else{
fmt.Fprint(w, "Mail send.")
}
}
NOTE:
-
ReplyTo
only works in gmail ifSender
andTo
are different. -
Sender
should have admin role in google cloud console orsomething@yourappid.appspotmail.com
.
答案1
得分: 3
这很可能失败是因为Sender
只能是以下两种情况之一:
- 你的应用程序开发者的电子邮件,或者
something@yourappid.appspotmail.com
我建议你硬编码发送者的电子邮件,并在其中使用On-Behalf-Of
头部,其中包括原始发送者的姓名/电子邮件。
另外,WriteString
只接受一个string
,而不是[]string
切片。
对于你的示例,最小的修改应该是:
…
msg := &mail.Message{
Sender: name + " <developer@yourapp.com>",
To: []string{"...@gmail.com"},
Subject: subject,
Body: message,
Headers: netMail.Header{
"On-Behalf-Of": []string{email},
},
}
…
另外,你需要确保用户的姓名实际上不包含电子邮件地址。那可能会给你带来问题...
最好按照 @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:
…
msg := &mail.Message{
Sender: name + " <developer@yourapp.com>",
To: []string{"...@gmail.com"},
Subject: subject,
Body: message,
Headers: netMail.Header{
"On-Behalf-Of": []string{email},
},
}
…
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论