亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Gomail 創建聯系表單

使用 Gomail 創建聯系表單

Go
守候你守候我 2022-01-04 10:31:35
我目前正在學習 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包來獲取值。main.go :package mainimport (    "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}基本上它的作用是提供索引頁面(聯系表格)和確認頁面。它還定義了Email和Contact字符串。如果我想打印消息的內容,我可以只使用m.Content,但是由于 Gomail 要求提供正文并提供 html,我真的不知道如何從表單中獲取字符串并像這樣添加它:m.SetBody("text/html", "<b>Message</b>: <!-- Content Goes Here -->")`
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

在這種情況下,您可以做的是使用Sprintf格式化方法。在您的特定情況下:

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


查看完整回答
反對 回復 2022-01-04
  • 1 回答
  • 0 關注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號