2016-04-10 33 views
0

我目前正在学习Go,并且我正在尝试创建一个联系表单。我使用默认的net/smtp包来发送我的邮件,但后来我偶然发现了Gomail。它使发送电子邮件变得更容易。与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 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 <[email protected]>") 
    m.SetHeader("To", "John Smith <[email protected]>") 
    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, "[email protected]", "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 -->")` 

回答

1

在这种情况下你可以做什么,是使用Sprintf格式化方法。在你的具体情况:

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

感谢您的答案,我不得不将m.Content更改为msg.Content以避免与编译器混淆。这是我不得不将我的代码更改为:http://pastebin.com/raw/TxZPmiw6。现在,当我编译时,我得到'./main.go:59:undefined:味精in msg.Content' – cmelone

+0

你正在实例化send函数中的消息结构,这就是为什么你得到未定义的错误。您必须将结构移出到主执行线程中。因为我想'Deliver' func在开始时就会执行。 –

+0

如果我将'Message'结构添加到主函数中,(https://git.io/vVSXR0),我得到以下错误:msg.Content中的./main.go:29:undefined:msg ./ main.go:43:undefined:Message' – cmelone