英文:
Creating a contact form with 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
包来获取这些值。
以下是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() {
msg := gomail.NewMessage()
msg.SetHeader("From", "John Smith <jsmith@gmail.com>")
msg.SetHeader("To", "John Smith <jsmith@gmail.com>")
msg.SetAddressHeader("reply-to", m.Email)
msg.SetHeader("Subject", "Contact")
msg.SetBody("text/html", "<b>Message</b>: "+m.Content)
d := gomail.NewDialer("smtp.gmail.com", 587, "jsmith@gmail.com", "password")
if err := d.DialAndSend(msg); err != nil {
panic(err)
}
}
基本上,这段代码会提供索引页面(即联系表单)和确认页面。它还定义了Email
和Content
字符串。如果我想要打印出消息的内容,我可以使用m.Content
,但由于Gomail要求提供邮件正文并提供HTML,我不知道如何从表单中获取字符串并像这样添加:
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:
<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>
I'm using Go's html/template
package to get the values.
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 <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)
}
}
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:
m.SetBody("text/html", "<b>Message</b>: <!-- Content Goes Here -->")`
答案1
得分: 1
在这种情况下,你可以使用Sprintf
格式化方法。在你的特定情况下:
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:
m.SetBody("text/html", fmt.Sprintf("<b>Message</b>: %s", m.Content))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论