Sending Emails from Revel App

Sending emails is a very important functionality for any web app. Go provides an inbuilt net/smtp package. Sending emails is straight forward and simple.

However, if you are using Revel, it provides a mail package which is an abstraction over the net/smtp package and makes you lazier.

Lets explore Revel’s mail package a bit. We will create a function, and use it to send emails.

In general to send emails we need a list of email addresses (recipients), a subject, a body of email which will be some HTML generated using a predefined template and some settings for our mail server, using which we will send out a mail.

Achieving this is simple.

func SendEmail(to []string, subject string, body string, templatePath string, args map[string]interface{}) {

	username := "email address of sender"
	password := "password"
	server := "smtp.gmail.com"

	auth := smtp.PlainAuth("",
		username,
		password,
		server,
	)

	mailer := mail.Mailer{Server:server, Port:587, UserName:username, Password:password, Auth:auth}
	message := mail.NewHtmlMessage(to, subject, body)
	err := message.RenderTemplate(templatePath, args)
	if err!= nil {
		message.HtmlBody = bytes.NewBufferString(body)
	}
	mailer.SendMessage(message)
}

Here we pass in a template path, which we want to use for generating HTML for emails and map of arguments for the template.

If you notice I provided a body string and a template too, If all goes well, user will see a mail which is generated using template and body string will be ignored. message.RenderTemplate(templatePath, args) actually overrides the body in message object.

However, if there was an error like, you misspelled the name of template or something, the mail will be sent using the body string.

So in a way body string is a fallback option in this case. Isn’t that Awesome!

Thats about it.

For sake of completeness, Here is the call to this function

var args = make(map[string] interface{})
args["FirstName"] = "Whizdumb"
service.SendEmail([]string{"xxxxxx@gmail.com"}, "This is a test message subject", "This is a test message.", "RegistrationMail", args)

and the template

<p>
Hello, {{.FirstName}},
</p>

<div style="color:red">
    We have this email coming from an email template!
</div>

Cheers!

~~ Whizdumb ~~
Email : sachin.xpert@gmail.com

2 responses

  1. Somehow this didn’t work for me with mail.NewHtmlMessage()

    Here’s a similar example for anyone that might find this interesting:

    import (
    “github.com/revel/revel/mail”
    )

    type Email struct {
    To []string
    Subject, From, Body string
    TemplatePath string
    }

    func (t Email) SendEmail(args map[string]interface{}) {

    mailer := mail.Mailer{Server:”127.0.0.1″, Port: 25}
    message := mail.Message{
    From: t.From,
    To: t.To,
    Subject: t.Subject,
    }
    message.RenderTemplate(t.TemplatePath, args)
    mailer.SendMessage(&message)
    }

    // Usage
    func SendEmailNow() {
    var args = make(map[string]interface{})
    args["FirstName"] = “Ryan”

    email := new(Email)
    email.Subject = “Subject”
    email.From = “test@example.com”
    email.To = []string{“test@example.com”}
    email.TemplatePath = “EmailTemplate” //.html inside /views folder
    email.SendEmail(args)
    }

Comments are closed.