1. Products
  2.   Email
  3.   GO
  4.   Mailgun-go
 
  

Open Source Go  API to Manage Email Messages

GO Library that has included built-in support for polling and email templates and enables software programmers to compose, send & track emails using SMTP Server.

What is Mailgun-go Library?

A powerful Go API that gives software programmers the ability to manage their email messages inside their own apps using Mailgun API. The mailgun-go library is very easy to use and allows developers to send, receive as well as track their email messages painlessly.

The library has built-in support for polling and email templates. It fully supports some common email-related features such as composing an email message, sending using BCC and Cc fields, adding custom email headers, adding attachments, reading receipts, and many more.

The library is very reliable and has a powerful email-sending mechanism. You can easily send emails to a high number of users with maximum accuracy. It has included several important features related to emails such as email sending and receiving, tracking emails, event polling, email validation, sending emails using email templates, and so on.

Previous Next

Getting Started with mailgun-go

The easiest way to install mailgun-go is via GitHub. Please install it using the following command for easy installation. .

Install mailgun-go via GitHub

 $go get github.com/mailgun/mailgun-go

Compose and Send Email Messages via Go API

The open source mailgun-go library has included complete support for sending and receiving email messages using a couple of lines of Go commands. You can easily retrieve and view your email messages as well as other related information. It allows you to compose your email messages, attaché files or images, add a subject, use a recipient list, and so on. Here is a useful example that shows how software developers can create and send an email message inside their Go applications.

How to Create & Send an Email Messages using Go API?

func main() {
	// Create an instance of the Mailgun Client
	mg := mailgun.NewMailgun(yourDomain, privateAPIKey)
	
	//When you have an EU-domain, you must specify the endpoint:
	//mg.SetAPIBase("https://api.eu.mailgun.net/v3")

	sender := "sender@example.com"
	subject := "Fancy subject!"
	body := "Hello from Mailgun Go!"
	recipient := "recipient@example.com"

	// The message object allows you to add attachments and Bcc recipients
	message := mg.NewMessage(sender, subject, body, recipient)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	defer cancel()

	// Send the message with a 10 second timeout
	resp, id, err := mg.Send(ctx, message)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("ID: %s Resp: %s\n", id, resp)
}

Attach Files to Email Message via Go

The email attachment is a useful process for sending a computer file along with your email messages.  The free library mailgun-go enables software engineers to develop apps that can easily attach files to an email message with just a couple of lines of Go code. Developers can easily attach multiple files such as PDF, MS Word, Spreadsheets, ZIP files to an email message. The library also provides support for retrieving or deleting the existing attachments. It is very simple to attach files to an email messages as shown in the below example.

How to Attach Files to an Email Messages inside Go Apps?

// The message object allows you to add attachments and Bcc recipients
message := mg.NewMessage(sender, subject, body, recipient)

Email Templates Support

The open source mailgun-go library gives software developers the capability to use email templates for creating and sending emails. It allows developers to create message templates on your Mailgun account and then can call it on the client side. This allows you to have your layout and design managed on the server and handle the data on the client.

Email Validations Support in Go Apps

Quality data collection is always the demand for an organization. The email validation process is one of the most effective way of verifying the validity of the email address. The open source library mailgun-go has provided functionality for validating email addresses with just a couple of lines of code. The following example shows, how software developers can perform email validations inside their own applications using Go commands.

How to Perform Email Validations using Go API?

func main() {
	// To use the /v4 version of validations define MG_URL in the environment
	// as `https://api.mailgun.net/v4` or set `v.SetAPIBase("https://api.mailgun.net/v4")`

	// Create an instance of the Validator
	v := mailgun.NewEmailValidator(apiKey)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	defer cancel()

	email, err := v.ValidateEmail(ctx, "recipient@example.com", false)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Valid: %t\n", email.IsValid)
}
 English