An example of sending SMTP mail with go language

Time:2021-4-17

Recently, I saw go sending SMTP mail, so I summarized it

Simple example

First, the simplest code (online code changed)

package main

import (
  "fmt"
  "net/smtp"
)

const (
  //Mail server address
  SMTP_MAIL_HOST = "smtp.qq.com"
  //Port
  SMTP_MAIL_PORT = "587"
  //Email user account
  SMTP_MAIL_USER = "[email protected]"
  //Authorization password
  SMTP_MAIL_PWD = "xxxx"
  //Send email nickname
  SMTP_MAIL_NICKNAME = "lewis"
)

func main() {
  //Declare err, subject and body types, and assign values to address, auth and contenttype, 
  //The subject is the subject, the body is the content, and the address is the recipient
  
  var err error
  var subject, body string
  address := "[email protected]"
  auth := smtp.PlainAuth("", SMTP_MAIL_USER, SMTP_MAIL_PWD, SMTP_MAIL_HOST)
  contentType := "Content-Type: text/html; charset=UTF-8"

  //The message to be sent can be written directly in [] bytes, but it looks too messy, so format it
  s := fmt.Sprintf("To:%s\r\nFrom:%s<%s>\r\nSubject:%s\r\n%s\r\n\r\n%s",
    address, SMTP_MAIL_NICKNAME, SMTP_MAIL_USER, subject, contentType, body)
  msg := []byte(s)

  //The email service address format is“ host:port ", so format addr to this format, and assign value directly.
  addr := fmt.Sprintf("%s:%s", SMTP_MAIL_HOST, SMTP_MAIL_PORT)

  //Send mail
  err = smtp.SendMail(addr, auth, SMTP_MAIL_USER, []string{address}, msg)
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println("send email succeed")
  }
}

Screenshot of email received

Brief description

  • SMTP in const_ MAIL_ Pwd is the password sent by Tencent when I applied to open the SMTP service of QQ mailbox, not the password of QQ mailbox.
  • You can see that the screenshot of the e-mail shows (no subject), and the e-mail has no content, because I only declare the subeject and body, but they have no assignment, so they are empty.
  • The content type is set to text / HTML. Of course, there are other formats.
  • The rest of the notes should be clearer

 A little improvement

Let’s add some common functions

  • There are multiple recipients
  • Of course, you have to set the subject and email content
  • Email function is encapsulated as a function, and then called in main
package main

import (
  "fmt"
  "net/smtp"
)

const (
  //Mail server address
  SMTP_MAIL_HOST = "smtp.qq.com"
  //Port
  SMTP_MAIL_PORT = "587"
  //Email user account
  SMTP_MAIL_USER = "[email protected]"
  //Authorization password
  SMTP_MAIL_PWD = "jhguvicvhrnwgaeh"
  //Send email nickname
  SMTP_MAIL_NICKNAME = "lewis"
)

func main() {
  address := []string{"[email protected]", "[email protected]"}
  subject := "test mail"
  body :=
    `<br>hello!</br>
  <br>this is a test email, pls ignore it.</br>`
  SendMail(address, subject, body)

}

func SendMail(address []string, subject, body string) (err error) {
  //Authentication, content type setting
  auth := smtp.PlainAuth("", SMTP_MAIL_USER, SMTP_MAIL_PWD, SMTP_MAIL_HOST)
  contentType := "Content-Type: text/html; charset=UTF-8"

  //Because there are multiple recipients, i.e. addresses, we need to traverse and send them one by one
  for _, to := range address {
    s := fmt.Sprintf("To:%s\r\nFrom:%s<%s>\r\nSubject:%s\r\n%s\r\n\r\n%s", to, SMTP_MAIL_NICKNAME, SMTP_MAIL_USER, subject, contentType, body)
    msg := []byte(s)
    addr := fmt.Sprintf("%s:%s", SMTP_MAIL_HOST, SMTP_MAIL_PORT)
    err = smtp.SendMail(addr, auth, SMTP_MAIL_USER, []string{to}, msg)
    if err != nil {
      return err
    }
  }
  return err
}

Screenshot of email received

Brief description

  • Two recipients, both received
  • Subject has
  • The body uses the br label to wrap lines. It’s no good to try to internalize the line wrap directly
  • In addition, if an email error is reported: 550 mailbox not found or access denied, Baidu will find that the recipient you want to send receives a large number of emails in a short period of time. In order to avoid malicious attacks, sending a letter to the recipient is temporarily prohibited, but this error will also be reported when the recipient’s address does not exist.

So far, this article about the implementation example of go sending SMTP mail is introduced here. For more information about go sending SMTP mail, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!

Recommended Today

Envoy announced alpha version of native support for windows

Author: sunjay Bhatia Since 2016, porting envoy to the windows platform has been an important part of the projectOne of the goalsToday, we are excited to announce the alpha version of envoy’s windows native support. The contributor community has been working hard to bring the rich features of envoy to windows, which is another step […]