我需要在golang中实现带有附件的Amazon ses SendRawEmail, 我尝试使用以下代码:
session, err := session.NewSession() svc := ses.New(session, &aws.Config{Region: aws.String("us-west-2")}) source := aws.String("XXX <xxx@xxx.com>") destinations := []*string{aws.String("xxx <xxx@xxx.com>")} message := ses.RawMessage{ Data: []byte(` From: xxx <xxx@xxx.com>\\nTo: xxx <xxx@xxx.com>\\nSubject: Test email (contains an attachment)\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\"sample.txt\"\\n\\nThis is the text in the attachment.\\n\\n--NextPart--" `)} input := ses.SendRawEmailInput{Source: source, Destinations: destinations, RawMessage: &message} output, err := svc.SendRawEmail(&input)
但在收到的邮件中,它显示的是我在邮件中提供的内容,而不是附件。不确定到底是什么错误???
请参阅AWS示例以发送带有附件的RAW电子邮件。
实施建议:如以上参考示例所述,为了易于编写电子邮件并以字节为单位获取电子邮件并将其发送到SES。
使用库gopkg.in/gomail.v2来编写带有附件的电子邮件,然后调用WriteTo方法。
gopkg.in/gomail.v2
var emailRaw bytes.Buffer emailMessage.WriteTo(&emailRaw) // while create instance of RawMessage RawMessage: &ses.RawMessage{ Data: emailRaw.Bytes(), }
祝好运!
编辑: 对于评论。
撰写电子邮件-
msg := gomail.NewMessage() msg.SetHeader("From", "alex@example.com") msg.SetHeader("To", "bob@example.com", "cora@example.com") msg.SetHeader("Subject", "Hello!") msg.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!") msg.Attach("/home/Alex/lolcat.jpg") var emailRaw bytes.Buffer msg.WriteTo(&emailRaw) message := ses.RawMessage{ Data: emailRaw.Bytes() } // Remaining is same as what you mentioned the question.