loading...

How to email your users without sending them spam?

gcdcoder profile image Gustavo Castillo ・2 min read

Email functionality is something we use (almost) all the time in our web applications, so it's really important to test this feature while we're in development stage and avoid sending spam to our users. Because of that I'm writing this post to tell you about Mailtrap which is a fake smtp testing server for dev teams. In order to use Mailtrap you have to follow this steps to setup your inbox:

  1. Create an account clicking here.
  2. Write a name for your inbox (I'm going to use "testing") and click the green button with the text Create Inbox.
  3. Click in your testing inbox in order to see your credentials.

Now let's talk about Go code, for this tutorial I'm going to use gomail package for sending emails click here to download it. Create the following project structure inside your go workspace:

mailing\
    image.jpg // May be any image you want to.
    mail.html
    main.go

Open mailing folder in your favourite code editor, then open main.go file and write the following code:

package main

import (
        "fmt"
        "io/ioutil"
        "gopkg.in/gomail.v2"
)

func main() {
        file, err := ioutil.ReadFile("mail.html") 
        if err != nil {
                fmt.Errorf("Could not open the file due to %v ", err.Error())
                return
        }

        // put your own credentials instead
        // order: Host, port, username, password
        m := gomail.NewMessage("smtp.mailtrap.io", 2525, "bc580f0dd3826c", "caec98e37af35d")
        m.SetHeader("From", "gcdcoder@gmail.com")
        m.SetHeader("To", "fake-user@mail.com")
        m.SetHeader("Subject", "welcome to my site")
        m.Embed("image.jpg")
        m.SetBody("text/html", string(file))

        dialer := gomail.NewDialer()

        if err := dialer.DialAndSend(m); err != nil {
                fmt.Errorf("Could not send the Email due to %v ", err.Error())
                return
        }
        fmt.Println("Email sent successfully!")
}

Now open mail.html file and write the following:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Email</title>
    <style>
        body, html {
            background: #eee;
            width: 100%;
            height: 100vh;
            overflow: hidden;
            margin: 0;
            padding: .5em 0;
            text-align: center;
        }

        img {
            max-width: 100%;
            width: 250px;
            height: 150px;
            margin-top: 1em;
        }

        h1 {
            margin: 0;
            color: steelblue;
            text-align: center;
            font-family: sans-serif;
        }

        a {
            text-decoration: none;
            display: block;
            width: 200px;
            color: #fff;
            background: steelblue;
            text-align: center;
            padding: .5em 0;
            font-family: sans-serif;
            margin-left: auto;
            margin-right: auto;
            margin-top: 1em;
        }
    </style>
</head>
<body>
    <h1>Saludo de bienvenida</h1>
    <img src="cid:image.jpg" alt="Image">
    <a href="http://twitter.com/gcdcoder" target="_blank">Click me!</a>
</body>
</html>

Time to test our code open a terminal in your mailing folder and write the following command:

go run main.go

After a while you should see the message Email sent successfully!, then visit your inbox in Mailtrap and you should see a new email open it and vualÑ there you have it!.

Note: I am reading the mail.html file using ioutil package for simplicity, I know this is not the best way of doing it you should use html/template package instead.

Thanks for reading and happy coding! ;)

Posted on by:

gcdcoder profile

Gustavo Castillo

@gcdcoder

I'm a web developer I won't fix your computer >:(

Discussion

pic
Editor guide
 

only gomail.NewMessage() //without anything
and gomail.NewDialer("smtp.mailtrap.io", 2525, "bc580f0dd3826c", "caec98e37af35d")
will be right.