Sending Email with Mailgun

The Mailgun API is built on HTTP and is RESTful. It uses predictable, resource-oriented URLs and built-in HTTP capabilities for passing parameters and authentication. The Mailgun API responds with standard HTTP response codes to indicate errors, and returns JSON.

Mailgun has published libraries for various languages. You can use these libraries or your favorite HTTP/REST library to make HTTP calls to Mailgun.

For more code samples in other programming languages, see Mailgun's documentation.

Pricing

As a Google Cloud Platform user, your first 30,000 messages are free every month. See the monthly pricing calculator on the sign up page for pricing on additional messages and volume discounts.

Setup

  • The mailgun-go library provides an interface to easily interact with the Mailgun API. To install the library, run:

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

Sending a plain text email

func SendSimpleMessageHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	httpc := urlfetch.Client(ctx)

	mg := mailgun.NewMailgun(
		"YOUR_DOMAIN_NAME", // Domain name
		"YOUR_API_KEY",     // API Key
		"YOUR_PUBLIC_KEY",  // Public Key
	)
	mg.SetClient(httpc)

	msg, id, err := mg.Send(mg.NewMessage(
		/* From */ "Excited User <mailgun@YOUR_DOMAIN_NAME>",
		/* Subject */ "Hello",
		/* Body */ "Testing some Mailgun awesomness!",
		/* To */ "bar@example.com", "YOU@YOUR_DOMAIN_NAME",
	))
	if err != nil {
		msg := fmt.Sprintf("Could not send message: %v, ID %v, %+v", err, id, msg)
		http.Error(w, msg, http.StatusInternalServerError)
		return
	}

	w.Write([]byte("Message sent!"))
}

Sending an HTML and text email

Sending a message with HTML and text parts. This example also attaches files to the message:

func SendComplexMessageHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	httpc := urlfetch.Client(ctx)

	mg := mailgun.NewMailgun(
		"YOUR_DOMAIN_NAME", // Domain name
		"YOUR_API_KEY",     // API Key
		"YOUR_PUBLIC_KEY",  // Public Key
	)
	mg.SetClient(httpc)

	message := mg.NewMessage(
		/* From */ "Excited User <mailgun@YOUR_DOMAIN_NAME>",
		/* Subject */ "Hello",
		/* Body */ "Testing some Mailgun awesomness!",
		/* To */ "foo@example.com",
	)
	message.AddCC("baz@example.com")
	message.AddBCC("bar@example.com")
	message.SetHtml("<html>HTML version of the body</html>")
	message.AddAttachment("files/test.jpg")
	message.AddAttachment("files/test.txt")

	msg, id, err := mg.Send(message)
	if err != nil {
		msg := fmt.Sprintf("Could not send message: %v, ID %v, %+v", err, id, msg)
		http.Error(w, msg, http.StatusInternalServerError)
		return
	}

	w.Write([]byte("Message sent!"))
}

Sample response:

{
    "message": "Queued. Thank you.",
    "id": "<20111114174239.25659.5817@samples.mailgun.org>"
}

Learning more

For more detailed examples and information, including how to track and route messages, see Mailgun's documentation.

Send feedback about...

App Engine standard environment for Go