DEV Community

Cover image for How to integrate zeptomail in Golang
Mathias Jiya
Mathias Jiya

Posted on • Updated on

How to integrate zeptomail in Golang

Transaction emailing have become a very vital part of any application. Wondering what transactional email means?
Transactional emails are messages that are sent in response to an action a user takes on a website or application. Examples include: Password reset emails etc.
There are several transactional email service providers out there just to name a few:

  • Postmark
  • ZeptoMail
  • SendGrid

In this article, I will be talking about ZeptoMail. As we already know, ZeptoMail is a transactional emailing service. It is tailor-made for sending transactional emails, ensuring fast delivery and inbox placement for your application emails.

So, while working on a project for a company I worked at. We decided to use ZeptoMail to send transactional emails. After my search, I realized that ZeptoMail does not have a Golang library. And as you already know by now, I opted in to build one. My reasons for always building Golang libraries is so as to make the work of other Golang developers easier and for re-usability as well.
Now lets get to the fun part :). Here's how to setup 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.

Installation

This package can in installed using the go command below.

go get github.com/iqquee/zeptomail@latest
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.

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

SendTemplatedEmail

SendTemplatedEmail() sends a templated email set from your ZeptoMail dashboard.

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

package main

import (
    "fmt"
    "net/http"

    "github.com/iqquee/zeptomail"
)

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

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

    req := zeptomail.SendTemplatedEmailReq{
        To: sendTo,
        From: zeptomail.EmailAddress{
            Address: "accounts@info.zylker.com",
            Name:    "Paula",
        },
        TemplateKey: tempKey,
    }

    res, err := client.SendTemplatedEmail(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

For a detailed documentation, you can check out https://github.com/iqquee/zeptomail. For more sample codes, you can see https://github.com/iqquee/zeptomail/blob/main/examples/main.go. Please do not hesitate to create an issue on GitHub if you encounter any issue while using this library as it will be attended to and resolved as soon as possible.

Top comments (0)