Skip to main content

Prerequisites

Installation and Setup

This example is based on the community-maintained unsend-go library.
This is a community-maintained package and not maintained by Mailrify.
1

Installation

go get github.com/QGeeDev/unsend-go
2

Set the environment variables

export UNSEND_API_KEY="YOUR_API_KEY"
export UNSEND_API_URL="https://app.mailrify.com"
3

Initialize

client, err := unsend.NewClient()

Usage

Below is an example of how to use the library to send an email and retrieve email information.

Sending Emails

To send an email you will need to define the payload. After definition, you can use the .SendEmail method to send emails with the payload as a parameter.
func main() {
	godotenv.Load()

	client, err := unsend.NewClient()

	if err != nil {
		fmt.Printf("[ERROR] - %s\n", err.Error())
		os.Exit(1)
	}

	request := &unsend.SendEmailRequest{
		To: []string{"[email protected]"},
		From: "[email protected]",
    Subject: "Mailrify test email",
		Text: "hello,\n\nThis is a test message sent from Mailrify to confirm that emails are delivering correctly.",
    Html: "<p>hello,</p><p>This is a test message sent from Mailrify to confirm that emails are delivering correctly.</p>",
	}

	response, err := client.Emails.SendEmail(context.Background(), *request)

	if err != nil {
		fmt.Printf("[ERROR] - %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("[SUCCESS] - %s\n", response.EmailId)
}

Retrieve Emails using the id

The email will be retrieved using the ID you get after sending the mail.
func main() {
	godotenv.Load()

	client, err := unsend.NewClient()

	getEmailRequest := unsend.GetEmailRequest{
		EmailId: "your-email-id",
	}

	email, err := client.Emails.GetEmail(context.Background(), getEmailRequest)

	if err != nil {
		fmt.Printf("[ERROR] - %s\n", err.Error())
		os.Exit(1)
	}

	fmt.Printf("[SUCCESS] - %s\n", email.Id)
}

More

Checkout more examples in the GitHub Repository. It handles emails, domains & contacts APIs.