Home  >  Article  >  Backend Development  >  go package smtp not showing message body on email

go package smtp not showing message body on email

王林
王林forward
2024-02-09 23:18:091004browse

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))
}

包主

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()
}

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

How to solve?

Solution

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

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

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:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete