go package smtp not showing message body on email
Feb 09, 2024 pm 11:18 PMphp 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

Tips for dynamically creating new functions in golang functions

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?

Golang technology libraries and tools used in machine learning

The evolution of golang function naming convention

The role of Golang technology in mobile IoT development

Can golang variable parameters be used for function return values?
