go package smtp not showing message body on email
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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











The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...
