Table of Contents
Question content
Solution
Home Backend Development Golang go package smtp not showing message body on email

go package smtp not showing message body on email

Feb 09, 2024 pm 11:18 PM
go language

go 包 smtp 未在电子邮件上显示消息正文

php editor Zimo reminds everyone that you may encounter a problem when sending emails using the smtp package of the Go language, that is, the message body is not displayed correctly in the email. This issue may prevent emails from conveying the required information properly. Before solving this problem, let us first understand how to use the smtp package and the possible reasons.

Question content

So, I want to send an email. The email was successfully sent to the gmail inbox, but the message or body is missing or empty. This is the code

Package Assistant

func randomstringbytes() string {
    
    rand.seed(time.now().unixnano())
    number := []byte("0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")
    
    b := make([]byte, 6)
    
    for i := range b{
      b[i] =  number[rand.intn(len(number))] 
    }
    
      return string(b)
}
  
func sendemail(to string, code string) error {
    from := "xxx"
    password := "xxx"
    smptserver := "smtp.gmail.com:587"
    subject := "verification code"
    body := "your verification code is: "+code

    message := "from: "+ from + "\n" +
    "to: " + to + "\n" +
    "subject: " + subject + "\n" +
    body
    
    auth := smtp.plainauth("", from, password, "smtp.gmail.com")

    return smtp.sendmail(smptserver, auth, from, []string{to}, []byte(message))
}
Copy after login

包主

func emailverify() {
    email := "xxx"
    code := helper.randomstringbytes()
    fmt.println("code:", code)
    err := helper.sendemail(email, code)
    if err != nil {
        fmt.println("error sending email:", err)
        return
    }
}

func main(){
    emailverify()
}
Copy after login

In the following code in the helper package, it already exists and is used as a parameter in smtp.sendmail(), but the email still has no message or is empty

message := "From: "+ from + "\n" +
    "To: " + to + "\n" +
    "Subject: " + subject + "\n" +
    body
Copy after login

How to solve?

Solution

body := "Your verification code is: "+code

message := "From: "+ from + "\n" +
"To: " + to + "\n" +
"Subject: " + subject + "\n" +
body
Copy after login

There needs to be a blank line between the message header and the message body, but it is missing here.

Some mail servers will solve this problem by adding this line before anything that does not look like a header, some mail servers will solve this problem in other ways, some servers will accept this message as is, but display it in some way If this method fails, some servers will refuse to send this malformed email directly.

For more information on the expected appearance of messages, see rfc 5322.

The above is the detailed content of go package smtp not showing message body on email. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use reflection to access private fields and methods in golang How to use reflection to access private fields and methods in golang May 03, 2024 pm 12:15 PM

How to use reflection to access private fields and methods in golang

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Tips for dynamically creating new functions in golang functions

The difference between performance testing and unit testing in Go language The difference between performance testing and unit testing in Go language May 08, 2024 pm 03:09 PM

The difference between performance testing and unit testing in Go language

What pitfalls should we pay attention to when designing distributed systems with Golang technology? What pitfalls should we pay attention to when designing distributed systems with Golang technology? May 07, 2024 pm 12:39 PM

What pitfalls should we pay attention to when designing distributed systems with Golang technology?

Golang technology libraries and tools used in machine learning Golang technology libraries and tools used in machine learning May 08, 2024 pm 09:42 PM

Golang technology libraries and tools used in machine learning

The evolution of golang function naming convention The evolution of golang function naming convention May 01, 2024 pm 03:24 PM

The evolution of golang function naming convention

The role of Golang technology in mobile IoT development The role of Golang technology in mobile IoT development May 09, 2024 pm 03:51 PM

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values? Can golang variable parameters be used for function return values? Apr 29, 2024 am 11:33 AM

Can golang variable parameters be used for function return values?

See all articles