Home > Article > Backend Development > GoLang: Unable to send email with attachments
php editor Strawberry will share with you an article about GoLang problem today: Unable to send emails with attachments. In daily development, sending emails with attachments is a common need, but sometimes we encounter some problems. This article will introduce how to solve this problem and help you successfully send emails with attachments. We will analyze the cause of the problem step by step and provide corresponding solutions, hoping to help everyone.
I am trying to send an email with an attachment using the following code:
package mail import ( "bytes" logging "project/logging" "fmt" "log" "mime/multipart" "net/textproto" "gopkg.in/mail.v2" ) func SendWithAttachment(from string, to []string, subject, bodyHTML string, attachmentName string, attachmentData []byte) error { logoURL := "/path/to/cs.jpg" m := mail.NewMessage() m.SetHeader("From", from) m.SetHeader("To", to...) m.SetHeader("Subject", subject) m.Embed(logoURL) // Create a multipart message body := &bytes.Buffer{} writer := multipart.NewWriter(body) // Add HTML part htmlPart := make(textproto.MIMEHeader) htmlPart.Set("Content-Type", "text/html") htmlPart.Set("Content-Transfer-Encoding", "quoted-printable") htmlPartWriter, err := writer.CreatePart(htmlPart) if err != nil { logging.NewDefaultLogger().Errorf("Error creating html part: %s", err) return err } htmlPartWriter.Write([]byte(bodyHTML)) // Add Attachment part attachmentPart := make(textproto.MIMEHeader) attachmentPart.Set("Content-Type", "application/octet-stream") attachmentPart.Set("Content-Transfer-Encoding", "base64") attachmentPart.Set("Content-Disposition", `attachment; filename="`+attachmentName+`"`) attachmentPartWriter, err := writer.CreatePart(attachmentPart) if err != nil { logging.NewDefaultLogger().Errorf("Error creating attachment part: %s", err) return err } attachmentPartWriter.Write(attachmentData) writer.Close() m.SetBody("multipart/mixed", body.String()) fmt.Printf("Sending mail.....") err = gs.dialer.DialAndSend(m) if err != nil { logging.NewDefaultLogger().Errorf("Error sending email..: %s", err) return err } return nil }
Both the text content and the attachment content appear as attachments in the file named "noname".
The content of the attached file is as follows:
--d885a04b467ae7d6100eb844c09574a6bdaef0a9796dc6fdde03a90613c9 Content Transfer Encoding: Quote Printable Content type: text/html
... --d885a04b467ae7d6100eb844c09574a6bdaef0a9796dc6fdde03a90613c9 Content Disposition: Attachment; filename="resource_details.csv" Content transfer encoding: base64 Content type: application/octet streamAttachment content..
--d885a04b467ae7d6100eb844c09574a6bdaef0a9796dc6fdde03a90613c9--
I was able to get it to work:
m := mail.NewMessage() m.SetHeader("From", emailDetails.From) m.SetHeader("To", emailDetails.To...) m.SetHeader("Subject", emailDetails.Subject) if emailDetails.BodyHTML != "" { m.SetBody("text/html", emailDetails.BodyHTML) } else { m.SetBody("text/plain", emailDetails.BodyPlain) } if emailDetails.AttachmentName != "" && len(emailDetails.AttachmentData) > 0 { m.Attach(emailDetails.AttachmentName, mail.SetCopyFunc(func(writer io.Writer) error { _, err := writer.Write(emailDetails.AttachmentData) return err })) }
The above is the detailed content of GoLang: Unable to send email with attachments. For more information, please follow other related articles on the PHP Chinese website!