SoFunction
Updated on 2025-04-14

A complete guide to easily implementing email notifications in Go

In modern web applications, email notification is an indispensable feature. Whether it is user registration, password reset, or system alarm, email can provide timely information transmission. In this article, we will analyze an in-depth analysis of a Go-based languagesmtpAgreement andemailThe library's email sending tool and provides detailed code examples to help you quickly integrate email functions!

How to send emails in Go

In Go language, you can usenet/smtpThe standard library sends emails, but its API design is relatively low-level and is relatively troublesome to use. Therefore, we use the jordan-wright/email library to simplify the email sending logic and make it easier to read and use.

This article is based ongin-vue-adminThe framework's plug-in encapsulates the email sending tool and supports functions such as normal email sending, error alarm emails, and email testing.

Code implementation parsing

First, let's look at the core code:

package utils

import (
    "crypto/tls"
    "fmt"
    "net/smtp"
    "strings"

    "/flipped-aurora/gin-vue-admin/server/plugin/email/global"
    "/jordan-wright/email"
)

// The main method of sending emailsfunc send(to []string, subject string, body string) error {
    from := 
    nickname := 
    secret := 
    host := 
    port := 
    isSSL := 

    auth := ("", from, secret, host)
    e := ()
    if nickname != "" {
         = ("%s <%s>", nickname, from)
    } else {
         = from
    }
     = to
     = subject
     = []byte(body)

    var err error
    hostAddr := ("%s:%d", host, port)
    if isSSL {
        err = (hostAddr, auth, &{ServerName: host})
    } else {
        err = (hostAddr, auth)
    }
    return err
}

// Ordinary email sending methodfunc Email(To, subject, body string) error {
    to := (To, ",")
    return send(to, subject, body)
}

// Send error alarm emailfunc ErrorToEmail(subject, body string) error {
    to := (, ",")
    if to[len(to)-1] == "" {
        to = to[:len(to)-1]
    }
    return send(to, subject, body)
}

// Test the email sending methodfunc EmailTest(subject, body string) error {
    to := []string{}
    return send(to, subject, body)
}

How to use

To use this tool to send emails, you need to configure SMTP mail server information first, such as Gmail, QQ mailbox, corporate mailbox, etc.

1. Configure email service information

existIn  , add the configuration information of the SMTP server, for example:

GlobalConfig = struct {
    From     string
    Nickname string
    Secret   string
    Host     string
    Port     int
    IsSSL    bool
    To       string
}{
    From:     "your_email@",
    Nickname: "Go Mailer",
    Secret:   "your_smtp_secret", // QQ/163/Gmail requires an authorization code    Host:     "",
    Port:     465,
    IsSSL:    true,
    To:       "receiver@",
}

2. Example of sending emails

Here is an example of sending mail by calling the mail tool:

Send regular emails

err := ("receiver@", "Go Mail Test", "<h1>Hello from Go!</h1>")
if err != nil {
    ("Email send failed:", err)
} else {
    ("The email was sent successfully!")
}

Send error alarm email

err := ("System Error", "<p>Exception was detected, please deal with it immediately!</p>")
if err != nil {
    ("Alarm mail failed to send:", err)
} else {
    ("The alarm email was sent successfully!")
}

Send test email

err := ("Test email", "<p>This is a test email</p>")
if err != nil {
    ("Test email failed:", err)
} else {
    ("The test email was sent successfully!")
}

Frequently Asked Questions

1.The email cannot be sent, and the return authentication failed?examineSecretWhether it is correct, some mailboxes (such as QQ mailboxes) require an SMTP authorization code, not a password.

2.The email was sent successfully, but the content was empty?make surebodyThe content is correct, HTML-formatted emails need<html>Tag package content.

3.Gmail/QQ ​​email failed?Make sure the SMTP server address is correct, for example:

  • Gmail, port465(SSL) or587(TLS)
  • QQ Email, port465(SSL)

Summarize

This article introduces how to use Go language to passsmtpSend emails and provide a complete code implementation. Whether it is normal email, error alarm email, or test email, this tool can be easily done!

This is the article about this complete guide to easily implementing the email notification function in Go. For more information about Go email notifications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!