go package smtp not showing message body on email

王林
Release: 2024-02-09 23:18:09
forward
998 people have browsed it

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!

Related labels:
source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!