cmelone
cmelone

Reputation: 69

Creating a contact form with Gomail

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 <[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)
    }
}

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 -->")`

Upvotes: 0

Views: 1127

Answers (1)

Endre Simo
Endre Simo

Reputation: 11541

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))

Upvotes: 1

Related Questions