我正在尝试在 Go (Golang) 中发送一封包含电子邮件正文和文件附件(CSV 文件)的电子邮件。
我遵循mime
多部分消息的标准,但是我不太熟悉遵循该标准的消息的结构。我模糊地遵循一位同事的Python代码片段作为使用Python库email
(我认为这是来自标准库)的指南,例如MIMEText
和MIMEMultipart
.
执行以下 Go 代码时,电子邮件正文未显示:
这有什么问题吗?
如何发送包含该文件附件和电子邮件正文的电子邮件?
该函数应返回一个字节切片,用作smtp.SendMail
从 Go 标准库调用的参数。请参阅下面的注释,了解收到的电子邮件发生的情况( 和THIS DOES NOT SHOW UP [...]
)THIS ALSO DOES NOT SHOW UP [...]
。
func msgWithAttachment(subject, filePath string) ([]byte, error) {
// this is the separator used for the various parts of the MIME message structure
// identified as "boundary"
bPlaceholder := "our-custom-separator"
// the message setup of the common/standard initial part
mime := bytes.NewBuffer(nil)
mime.WriteString(fmt.Sprintf("Subject: %s\r\nMIME-Version: 1.0\r\n", subject))
mime.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\r\n", bPlaceholder))
// THIS DOES NOT SHOW UP AS THE BODY OF THE EMAIL...
// mime.WriteString("\r\n")
// mime.WriteString(fmt.Sprintf("--%s\r\n", bPlaceholder))
// mime.WriteString("This should be the email message body (v1)...")
// mime.WriteString("\r\n")
// THIS ALSO DOES NOT SHOW UP AS THE BODY OF THE EMAIL...
// BUT IS NEEDED OTHERWISE THE EMAIL MESSAGE SEEMS TO CONTAIN AS ATTACHMENT THE EMAIL MESSAGE ITSELF
// (CONTAINING ITSELF THE REAL ATTACHMENT)
mime.WriteString(fmt.Sprintf("--%s\r\n", bPlaceholder))
mime.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
mime.WriteString("This should be the email message body (v2)...")
}
ABOUTYOU
森栏
相关分类