Back

Step by Step Guide to Building a Mailjet API Integration in Go

Aug 14, 2024 • 5 minute read

Introduction

Hey there, fellow Go developer! Ready to supercharge your email capabilities? Let's dive into integrating Mailjet's powerful API into your Go application. We'll be using the mailjet-apiv3-go package, which makes our lives a whole lot easier. Buckle up!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine (I know you probably do, but just checking!)
  • A Mailjet account with API credentials handy

Got those? Great! Let's roll.

Setting up the project

First things first, let's create a new Go project and grab that mailjet-apiv3-go package:

mkdir mailjet-integration && cd mailjet-integration go mod init mailjet-integration go get github.com/mailjet/mailjet-apiv3-go

Initializing the Mailjet client

Now, let's get that Mailjet client up and running:

package main import ( "fmt" "os" mailjet "github.com/mailjet/mailjet-apiv3-go" ) func main() { client := mailjet.NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE")) // We'll use this client for all our Mailjet operations }

Pro tip: Keep those API keys safe in environment variables!

Sending a simple email

Let's send our first email. It's easier than you might think:

messagesInfo := []mailjet.InfoMessagesV31{ { From: &mailjet.RecipientV31{ Email: "[email protected]", Name: "Mailjet Pilot", }, To: &mailjet.RecipientsV31{ mailjet.RecipientV31{ Email: "[email protected]", Name: "Mailjet Passenger", }, }, Subject: "Your Mailjet journey!", TextPart: "Dear passenger, welcome to Mailjet!", HTMLPart: "<h3>Dear passenger, welcome to Mailjet!</h3>", }, } messages := mailjet.MessagesV31{Info: messagesInfo} res, err := client.SendMailV31(&messages)

Handling responses and errors

Always check for errors and handle responses gracefully:

if err != nil { fmt.Println("Error:", err) return } fmt.Printf("Email sent successfully. Response: %+v\n", res)

Advanced features

Adding attachments

Want to spice up your email with an attachment? Here's how:

attachment := mailjet.Attachment{ Filename: "sample.pdf", ContentType: "application/pdf", Base64Content: "your-base64-encoded-content", } messagesInfo[0].Attachments = &mailjet.AttachmentsV31{attachment}

Using templates

Mailjet templates are awesome. Here's how to use them:

messagesInfo[0].TemplateID = 1234567 messagesInfo[0].TemplateLanguage = true messagesInfo[0].Variables = map[string]interface{}{ "name": "Mailjet Passenger", }

Best practices

  • Respect rate limits: Mailjet has them, so be nice!
  • Implement error retries: Sometimes things fail, give it another shot.
  • Keep your credentials safe: Use environment variables or a secure vault.

Testing the integration

Don't forget to test! Here's a simple example:

func TestSendEmail(t *testing.T) { // Set up your test client and message // Send the email // Assert on the response and any errors }

Conclusion

And there you have it! You're now equipped to send emails like a pro using Mailjet and Go. Remember, the Mailjet API can do a lot more than what we've covered here. Don't be afraid to explore the docs and experiment.

Happy coding, and may your emails always reach their destination!