使用Gomail创建联系表单

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

Creating a contact form with Gomail

问题

我目前正在学习Go,并尝试创建一个联系表单。我之前使用默认的net/smtp包来发送邮件,但后来我发现了Gomail,它使发送电子邮件变得更加简单。

以下是联系表单的HTML代码:

  1. <h1>Contact Us</h1>
  2. <form action="/" method="post" novalidate>
  3. <div>
  4. <label>Email Address</label>
  5. <input type="email" name="email" value="{{ .Email }}">
  6. </div>
  7. <div>
  8. <label>Message:</label>
  9. <textarea name="content">{{ .Content }}</textarea>
  10. </div>
  11. <div>
  12. <input type="submit" value="Submit">
  13. </div>
  14. </form>

我使用Go的html/template包来获取这些值。

以下是main.go的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/bmizerany/pat"
  5. "gopkg.in/gomail.v2"
  6. "html/template"
  7. "log"
  8. "net/http"
  9. )
  10. func main() {
  11. mux := pat.New()
  12. mux.Get("/", http.HandlerFunc(index))
  13. mux.Post("/", http.HandlerFunc(send))
  14. mux.Get("/confirmation", http.HandlerFunc(confirmation))
  15. log.Println("Listening...")
  16. http.ListenAndServe(":2016", mux)
  17. }
  18. func index(w http.ResponseWriter, r *http.Request) {
  19. render(w, "templates/index.html", nil)
  20. }
  21. func send(w http.ResponseWriter, r *http.Request) {
  22. m := &Message{
  23. Email: r.FormValue("email"),
  24. Content: r.FormValue("content"),
  25. }
  26. if err := m.Deliver(); err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. return
  29. }
  30. http.Redirect(w, r, "/confirmation", http.StatusSeeOther)
  31. }
  32. func confirmation(w http.ResponseWriter, r *http.Request) {
  33. render(w, "templates/confirmation.html", nil)
  34. }
  35. func render(w http.ResponseWriter, filename string, data interface{}) {
  36. tmpl, err := template.ParseFiles(filename)
  37. if err != nil {
  38. http.Error(w, err.Error(), http.StatusInternalServerError)
  39. }
  40. if err := tmpl.Execute(w, data); err != nil {
  41. http.Error(w, err.Error(), http.StatusInternalServerError)
  42. }
  43. }
  44. type Message struct {
  45. Email string
  46. Content string
  47. }
  48. func (m *Message) Deliver() {
  49. msg := gomail.NewMessage()
  50. msg.SetHeader("From", "John Smith <jsmith@gmail.com>")
  51. msg.SetHeader("To", "John Smith <jsmith@gmail.com>")
  52. msg.SetAddressHeader("reply-to", m.Email)
  53. msg.SetHeader("Subject", "Contact")
  54. msg.SetBody("text/html", "<b>Message</b>: "+m.Content)
  55. d := gomail.NewDialer("smtp.gmail.com", 587, "jsmith@gmail.com", "password")
  56. if err := d.DialAndSend(msg); err != nil {
  57. panic(err)
  58. }
  59. }

基本上,这段代码会提供索引页面(即联系表单)和确认页面。它还定义了EmailContent字符串。如果我想要打印出消息的内容,我可以使用m.Content,但由于Gomail要求提供邮件正文并提供HTML,我不知道如何从表单中获取字符串并像这样添加:

  1. m.SetBody("text/html", "<b>Message</b>: <!-- Content Goes Here -->")

以上是你要翻译的内容。

英文:

I am currently learning Go, and I am trying to create a contact form. I was using the default net/smtp package to send my mail, but then I stumbled upon Gomail. It made it so much easier to send an email.

Here is the html for the contact form:

  1. &lt;h1&gt;Contact Us&lt;/h1&gt;
  2. &lt;form action=&quot;/&quot; method=&quot;post&quot; novalidate&gt;
  3. &lt;div&gt;
  4. &lt;label&gt;Email Address&lt;/label&gt;
  5. &lt;input type=&quot;email&quot; name=&quot;email&quot; value=&quot;{{ .Email }}&quot;&gt;
  6. &lt;/div&gt;
  7. &lt;div&gt;
  8. &lt;label&gt;Message:&lt;/label&gt;
  9. &lt;textarea name=&quot;content&quot;&gt;{{ .Content }}&lt;/textarea&gt;
  10. &lt;/div&gt;
  11. &lt;div&gt;
  12. &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
  13. &lt;/div&gt;
  14. &lt;/form&gt;

I'm using Go's html/template package to get the values.

main.go:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;github.com/bmizerany/pat&quot;
  5. &quot;gopkg.in/gomail.v2&quot;
  6. &quot;html/template&quot;
  7. &quot;log&quot;
  8. &quot;net/http&quot;
  9. )
  10. func main() {
  11. mux := pat.New()
  12. mux.Get(&quot;/&quot;, http.HandlerFunc(index))
  13. mux.Post(&quot;/&quot;, http.HandlerFunc(send))
  14. mux.Get(&quot;/confirmation&quot;, http.HandlerFunc(confirmation))
  15. log.Println(&quot;Listening...&quot;)
  16. http.ListenAndServe(&quot;:2016&quot;, mux)
  17. }
  18. func index(w http.ResponseWriter, r *http.Request) {
  19. render(w, &quot;templates/index.html&quot;, nil)
  20. }
  21. func send(w http.ResponseWriter, r *http.Request) {
  22. m := &amp;Message{
  23. Email: r.FormValue(&quot;email&quot;),
  24. Content: r.FormValue(&quot;content&quot;),
  25. }
  26. if err := m.Deliver(); err != nil {
  27. http.Error(w, err.Error(), http.StatusInternalServerError)
  28. return
  29. }
  30. http.Redirect(w, r, &quot;/confirmation&quot;, http.StatusSeeOther)
  31. }
  32. func confirmation(w http.ResponseWriter, r *http.Request) {
  33. render(w, &quot;templates/confirmation.html&quot;, nil)
  34. }
  35. func render(w http.ResponseWriter, filename string, data interface{}) {
  36. tmpl, err := template.ParseFiles(filename)
  37. if err != nil {
  38. http.Error(w, err.Error(), http.StatusInternalServerError)
  39. }
  40. if err := tmpl.Execute(w, data); err != nil {
  41. http.Error(w, err.Error(), http.StatusInternalServerError)
  42. }
  43. }
  44. type Message struct {
  45. Email string
  46. Content string
  47. }
  48. func (m *Message) Deliver() {
  49. m := gomail.NewMessage()
  50. m.SetHeader(&quot;From&quot;, &quot;John Smith &lt;jsmith@gmail.com&gt;&quot;)
  51. m.SetHeader(&quot;To&quot;, &quot;John Smith &lt;jsmith@gmail.com&gt;&quot;)
  52. m.SetAddressHeader(&quot;reply-to&quot;, &quot;m.Email&quot;)
  53. m.SetHeader(&quot;Subject&quot;, &quot;Contact&quot;)
  54. m.SetBody(&quot;text/html&quot;, &quot;&lt;b&gt;Message&lt;/b&gt;: m.Content&quot;)
  55. d := gomail.NewDialer(&quot;smtp.gmail.com&quot;, 587, &quot;jsmith@gmail.com&quot;, &quot;password&quot;)
  56. if err := d.DialAndSend(m); err != nil {
  57. panic(err)
  58. }
  59. }

Basically what this does is serve the index page (the contact form) and the confirmation page. It also defines the Email and Contact strings. If I wanted to print out the content of the message, I can just use m.Content, but since Gomail asks for the body and serves html, I don't really know a way to get the string from the form and add it like this:

  1. m.SetBody(&quot;text/html&quot;, &quot;&lt;b&gt;Message&lt;/b&gt;: &lt;!-- Content Goes Here --&gt;&quot;)`

答案1

得分: 1

在这种情况下,你可以使用Sprintf格式化方法。在你的特定情况下:

  1. m.SetBody("text/html", fmt.Sprintf("<b>Message</b>: %s", m.Content))
英文:

In this case what you can do, is to use the Sprintf formatting method. In your particular case:

  1. m.SetBody(&quot;text/html&quot;, fmt.Sprintf(&quot;&lt;b&gt;Message&lt;/b&gt;: %s&quot;, m.Content))

huangapple
  • 本文由 发表于 2016年4月10日 13:45:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/36526304.html
匿名

发表评论

匿名网友

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

确定