Sending Email In Golang - Getting Started with Golang

Опубликовано: 02 Ноябрь 2024
на канале: Maharlikans Code
1,950
30

In this Getting Started with Golang, we will learn how to Send Email using Golang programming language with Sulat package as the helper tool that supports both SendGrid and the Classic SMTP server with step by step guide in Golang programming language.

#MaharlikansCode
#GolangSMTP
#Sulat
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper

If you go with extra mile for buying me a cup of coffee, I appreciate it guys: https://ko-fi.com/maharlikanscode

Source Codes:
package main

import (
"fmt"

"github.com/itrepablik/itrlog"
"github.com/itrepablik/sulat"
)

// HTMLHeader is the HTML skeletal framework head section of the standard HTML structure that serves as an email content
const HTMLHeader = ``

// HTMLFooter is the HTML skeletal framework footer section of the standard HTML structure that serves as an email content
const HTMLFooter = ``

// bodyHTML is your custom email contents, this is just an example of a password reset auto email from your Go's project
var bodyHTML = ``

// FullHTML use this when you preferred the full HTML template as your content
var FullHTML = ``

// SGC to initialize SendGrid API
var SGC = sulat.SGC{}

func init() {
SGC = sulat.SGC{
SendGridAPIKey: "YOUR_SENDGRID_API_KEY_HERE",
SendGridEndPoint: "/v3/mail/send",
SendGridHost: "https://api.sendgrid.com",
}
}

func main() {
fmt.Println("Hello Maharlikans!")

mailOpt := &sulat.SendMail{
Subject: "Inquiry from Maharlikans Code!",
From: sulat.NewEmail("Maharlikans Code", "[email protected]"),
To: sulat.NewEmail("Politz", "[email protected]"),
}

// Method 1 by using the Full HTML or Static Content
htmlContent, err := sulat.SetHTML(&sulat.EmailHTMLFormat{
IsFullHTML: true,
FullHTMLTemplate: FullHTML,
})

isSend, err := sulat.SendEmailSG(mailOpt, htmlContent, &SGC)
if err != nil {
itrlog.Fatal(err)
}
fmt.Println("is sent: ", isSend)
}