Golang SMTP Integration Guide

Minimum code that you need to integrate Mailazy with your Golang application


Prerequisites

You need to complete these given prerequisites, you can skip the step if you have already completed.

  1. Sign up for a Mailazy account.
  2. Complete Domain Authentication.
  3. Generate the Mailazy Access Key

Integrate Mailazy with Golang

Start Sending mail using the code provided below:

package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "sender@example.com")
	m.SetHeader("To", "bob@example.com")
	m.SetHeader("Subject", "Hello!")
	m.AddAlternative("text/html", "Hello <b>Bob</b>!")
	m.AddAlternative("text/plain", "Hello Bob!")

	// Send the email to Bob
	dialer := gomail.NewPlainDialer("smtp.mailazy.com", 587, "access_key", "access_secret")
	if err := dialer.DialAndSend(m); err != nil {
		panic(err)
	}
}

Or send an email with attachment, using the code provided below:

package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "sender@example.com")
	m.SetHeader("To", "bob@example.com")
	m.SetHeader("Subject", "Hello!")
	m.AddAlternative("text/html", "Hello <b>Bob</b>!")
	m.AddAlternative("text/plain", "Hello Bob!")

	//To send attachment just attach file to your SMTP mutipart content boundary
	m.Attach("path/to/attachment.pdf")

	// Send the email to Bob
	dialer := gomail.NewPlainDialer("smtp.mailazy.com", 587, "access_key", "access_secret")
	if err := dialer.DialAndSend(m); err != nil {
		panic(err)
	}
}