Sending Bulk Emails with Golang using SMTP: A Step-by-Step Guide

Sending Bulk Emails with Golang using SMTP: A Step-by-Step Guide

In this tutorial, we'll delve into the creation of a Golang program for sending bulk emails through the Simple Mail Transfer Protocol (SMTP) with custom reply_to and rich HTML support. Utilizing the built-in net/smtp package and the external github.com/joho/godotenv package for environment variable management, you can seamlessly set up and configure your Golang project to efficiently dispatch emails.

Access the complete source code on my GitHub repository.

This blog is the part of Golang series, click here to see the series

Prerequisites

  • Golang is installed on your device.

  • Access to an SMTP server for sending emails.

Code

Install Dependencies

go get github.com/joho/godotenv

Create a .env file

# Rename this file to .env 

# SMTP credentials
SMTP_USERNAME=""
SMTP_PASSWORD=""


# SMTP server information
SMTP_HOST=""
SMTP_PORT=""


# Senders email address
FROM_EMAIL=""

Create a main.go file

package main

import(
    "fmt"
    "github.com/joho/godotenv"
    "log"
    "os"
    "net/smtp"
)

func main(){

    // Loading environment variables
    err := godotenv.Load(".env")
    if err != nil {
        log.Fatalf("Error loading environment variables file")
    }

    // SMTP server Credentials from .env file
    SMTP_USERNAME := os.Getenv("SMTP_USERNAME")
    SMTP_PASSWORD := os.Getenv("SMTP_PASSWORD")
    SMTP_HOST :=os.Getenv("SMTP_HOST")
    FROM_EMAIL :=os.Getenv("FROM_EMAIL")
    SMTP_PORT :=os.Getenv("SMTP_PORT")

    log.Println("SMTP CREDS init ",SMTP_USERNAME, " ", SMTP_PASSWORD," ",SMTP_HOST )

    // Setup authentication variable
    auth:=smtp.PlainAuth("",SMTP_USERNAME,SMTP_PASSWORD,SMTP_HOST)


    // List of emails you want to send the email
    // toList := []string{"email1@gmail.com","email2@gmail.com","email3@gmail.com"}
    toList := []string{"recipient1@gmail.com"}


    // mail
    subject:="Test Golang Program"
    body:="<html><body><h1>Hello, this is an HTML-rich email template!</h1></body></html>"
    // Can add custom reply-to email ID, leave blank to use the default 
    reply_to:=""

    if reply_to==""{
        reply_to=FROM_EMAIL
    }

    var msg []byte
    //For basic text
    // msg = []byte(
    //     "Reply-To: "+reply_to+"\r\n"+
    //     "Subject: "+subject+"\r\n" +
    //     "\r\n" +
    //     body+"\r\n")

    //For rich html support
    msg = []byte(
        "From: "+FROM_EMAIL+"\r\n"+
        "Reply-To: " + reply_to + "\r\n" +
            "Subject: " + subject + "\r\n" +
            "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n" +
            "\r\n" +
            body + "\r\n")


    // send the mail
    err = smtp.SendMail(SMTP_HOST+":"+SMTP_PORT, auth, FROM_EMAIL, toList, msg)

    // handling the errors
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }

    fmt.Println("Successfully sent mail to all user in toList")

}

Run the code

go run .

Conclusion

This step-by-step guide walks you through the process of setting up your Golang project, configuring SMTP credentials, and running the program to send bulk emails effortlessly. Enhance your Golang skills while accomplishing the practical task of email communication. This blog adheres to the standards of 2024.

Feel free to explore and customize the code for your specific needs. Happy coding!

Did you find this article valuable?

Support Aswin Benny by becoming a sponsor. Any amount is appreciated!