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

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

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)如果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)

package bin

import (
    &quot;fmt&quot;
    &quot;net/http&quot;
    netMail &quot;net/mail&quot;
    &quot;appengine&quot;
    &quot;appengine/mail&quot;
)

func contact(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)
    name := r.FormValue(&quot;name&quot;)
    email := r.FormValue(&quot;email&quot;)
    subject := r.FormValue(&quot;subject&quot;)
    message := r.FormValue(&quot;message&quot;)
    msg := &amp;mail.Message{
            Sender:  name + &quot; &lt;...@yourappid.appspotmail.com&gt;&quot;,
            To:      []string{&quot;...@...&quot;},
            ReplyTo: email,
            Subject: subject,
            Body:    message,
            Headers: netMail.Header{
                &quot;On-Behalf-Of&quot;: []string{email},
            },
    }
    if err := mail.Send(c, msg); err != nil {
        c.Errorf(&quot;Couldn&#39;t send email: %v&quot;, err)
        fmt.Fprint(w, &quot;Mail NOT send! Error&quot;)
    }else{
        fmt.Fprint(w, &quot;Mail send.&quot;)
    }
}

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切片。

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

…
msg := &amp;mail.Message{
        Sender:  name + &quot; &lt;developer@yourapp.com&gt;&quot;,
        To:      []string{&quot;...@gmail.com&quot;},
        Subject: subject,
        Body:    message,
        Headers: netMail.Header{
            &quot;On-Behalf-Of&quot;: []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 := &amp;mail.Message{
        Sender:  name + &quot; &lt;developer@yourapp.com&gt;&quot;,
        To:      []string{&quot;...@gmail.com&quot;},
        Subject: subject,
        Body:    message,
        Headers: netMail.Header{
            &quot;On-Behalf-Of&quot;: []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.

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:

确定