forms - 使用 Gomail 创建联系表单

标签 forms go gomail

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

这是联系表格的 html:

<h1>Contact Us</h1>
<form action="/" method="post" novalidate>
  <div>
    <label>Email Address</label>
    <input type="email" name="email" value="{{ .Email }}">
  </div>
  <div>
    <label>Message:</label>
    <textarea name="content">{{ .Content }}</textarea>
  </div>
  <div>
    <input type="submit" value="Submit">
  </div>
</form>

我正在使用 Go 的 html/template 包来获取值。

ma​​in.go:

package main

import (
    "fmt"
    "github.com/bmizerany/pat"
    "gopkg.in/gomail.v2"
    "html/template"
    "log"
    "net/http"
)

func main() {
    mux := pat.New()
    mux.Get("/", http.HandlerFunc(index))
    mux.Post("/", http.HandlerFunc(send))
    mux.Get("/confirmation", http.HandlerFunc(confirmation))

    log.Println("Listening...")
    http.ListenAndServe(":2016", mux)
}

func index(w http.ResponseWriter, r *http.Request) {
    render(w, "templates/index.html", nil)
}

func send(w http.ResponseWriter, r *http.Request) {
  m := &Message{
    Email: r.FormValue("email"),
    Content: r.FormValue("content"),
  }

  if err := m.Deliver(); err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
  }
  http.Redirect(w, r, "/confirmation", http.StatusSeeOther)
}

func confirmation(w http.ResponseWriter, r *http.Request) {
    render(w, "templates/confirmation.html", nil)
}

func render(w http.ResponseWriter, filename string, data interface{}) {
    tmpl, err := template.ParseFiles(filename)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
    if err := tmpl.Execute(w, data); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

type Message struct {
    Email   string
    Content string
}

func (m *Message) Deliver() {
    m := gomail.NewMessage()
    m.SetHeader("From", "John Smith <jsmith@gmail.com>")
    m.SetHeader("To", "John Smith <jsmith@gmail.com>")
    m.SetAddressHeader("reply-to", "m.Email")
    m.SetHeader("Subject", "Contact")
    m.SetBody("text/html", "<b>Message</b>: m.Content")
    d := gomail.NewDialer("smtp.gmail.com", 587, "jsmith@gmail.com", "password")
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}

基本上,它所做的是提供索引页面(联系表)和确认页面。它还定义了 EmailContact 字符串。如果我想打印出消息的内容,我可以只使用 m.Content,但由于 Gomail 要求正文并提供 html,我真的不知道如何获取字符串从表单中添加如下:

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

最佳答案

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

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

关于forms - 使用 Gomail 创建联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526304/

相关文章:

python - Django:空表单错误

javascript - 如何序列化表单,而不是输入的值获取输入的 ID

css - 关于 css 和 &lt;input&gt; 的问题

go - 是否可以在另一个包中实现具有未导出方法的接口(interface)?

Golang 多个收件人与 gomail.v2

go - 我如何检测 golang 中的 net smtp 验证错误?

javascript - 使用 jQuery 检测提交事件的来源

go - 使用指针时,omitempty 在编码/xml 中不起作用?

post - 如何在 golang 中编码 POST 策略 - 基于浏览器的上传到 amazon S3?

gomail noauth 示例崩溃