How to setup ZeptoMail in Golang

iqquee

Mathias Jiya

Posted on November 14, 2023

How to setup ZeptoMail in Golang

Hello ninjas,
In today's article, I bring you ZeptoMail.
ZeptoMail is an email service built exclusively to send transactional emails from applications or websites such as welcome emails, password reset emails, invoice emails, etc.
Building your backend service is enough work already. Including an emailing functionality should not add to your problems. That is why I decided to build a Golang library for ZeptoMail after I realized that ZeptoMail does not have a Golang library that implements its APIs. You should know just how much I love to build Golang libraries already. The reason for this is simple - it makes the work of other Golang developers out there easier and faster.

Using this library, you can send HTML email, templated email, etc to your users\customers. Here is how to set up ZeptoMail in your Golang project:

Get Started

In order to use this package, you need to first head over to https://zoho.com to create an account to get your Authorization Token Key.

Documentation

See the ZeptoMail API Docs

Installation

This package can in installed using the go command below.

go get github.com/iqquee/zeptomail
Enter fullscreen mode Exit fullscreen mode

Quick start

# assume the following codes in example.go file
$ touch example.go
# open the just created example.go file in the text editor of your choice
Enter fullscreen mode Exit fullscreen mode

SendHTMLEmail

SendHTMLEmail() sends a HTML template email.

This method takes in the zeptomail.SendHTMLEmailReq{} struct as a parameter.

List of all the fields available in the zeptomail.SendHTMLEmailReq{} struct.

type ( 
    // SendHTMLEmailReq is the SendHTMLEmail() request object
    SendHTMLEmailReq struct {
        From     EmailAddress  `json:"from" validate:"required"`
        To       []SendEmailTo `json:"to" validate:"required"`
        Subject  string        `json:"subject" validate:"required"`
        Htmlbody string        `json:"htmlbody" validate:"required"`
    }

    // EmailAddress is an email address object
    EmailAddress struct {
        /*
            The email address to which the recipient's email responses will be addressed.

            Allowed value - A valid email address containing a domain that is verified in your Mail Agent.
        */
        Address string `json:"address" validate:"required"`
        // Recipient's name.
        Name string `json:"name" validate:"required"`
    }
)
Enter fullscreen mode Exit fullscreen mode
package main

import (
    "fmt"
    "net/http"

    "github.com/iqquee/zeptomail"
)

func main() {
    zeptomailToken := "your zeptomail authorization token"

    client := zeptomail.New(*http.DefaultClient, zeptomailToken)
    sendTo := []zeptomail.SendEmailTo{}
    sendTo = append(sendTo, zeptomail.SendEmailTo{
        EmailAddress: zeptomail.EmailAddress{
            Address: "rudra.d@zylker.com",
            Name:    "Rudra",
        },
    })

    data := "<div><b> Kindly click on Verify Account to confirm your account </b></div>"

    req := zeptomail.SendHTMLEmailReq{
        To: sendTo,
        From: zeptomail.EmailAddress{
            Address: "accounts@info.zylker.com",
            Name:    "Paula",
        },
        Subject:  "Account Confirmation",
        Htmlbody: data,
    }
    res, err := client.SendHTMLEmail(req)
    if err != nil {
        fmt.Printf("This is the error: %v", err.Error())
    }

    for _, e := range res.Data {
        fmt.Printf("response message: %v\n", e.Message)
    }
}
Enter fullscreen mode Exit fullscreen mode

You can check out the library for documentation of other available methods. I will be adding some more methods to the library soon because my goal is to ensure that this library implements all the functionalities supported by ZeptoMail.

Have a wonderful day ahead devs - Happy coding ;)

💖 💪 🙅 🚩
iqquee
Mathias Jiya

Posted on November 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to setup ZeptoMail in Golang
zeptomail How to setup ZeptoMail in Golang

November 14, 2023