DEV Community

Gustavo Castillo
Gustavo Castillo

Posted on

How to email your users without sending them spam?

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:

    image.jpg // May be any image you want to.
Enter fullscreen mode Exit fullscreen mode

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

package main

import (

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

        // put your own credentials instead
        // order: Host, port, username, password
        m := gomail.NewMessage("", 2525, "bc580f0dd3826c", "caec98e37af35d")
        m.SetHeader("From", "")
        m.SetHeader("To", "")
        m.SetHeader("Subject", "welcome to my site")
        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())
        fmt.Println("Email sent successfully!")

Enter fullscreen mode Exit fullscreen mode

Now open mail.html file and write the following:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
        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;
    <h1>Saludo de bienvenida</h1>
    <img src="cid:image.jpg" alt="Image">
    <a href="" target="_blank">Click me!</a>
Enter fullscreen mode Exit fullscreen mode

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

go run main.go
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

pnrmx profile image

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