如何在golang中实现带有附件的aws ses SendRawEmail
How to implement aws ses SendRawEmail with attachment in golang
我需要在 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 example 发送带附件的 RAW 电子邮件。
实施建议:如上述参考示例中所述,易于撰写电子邮件并以字节形式获取电子邮件并将其发送到 SES。
使用库 gopkg.in/gomail.v2
编写带附件的电子邮件,然后调用 WriteTo 方法。
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.
如果您尝试从字节附加文件:
msg.Attach("report.pdf", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(reportData)
return err
}))
我需要在 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 example 发送带附件的 RAW 电子邮件。
实施建议:如上述参考示例中所述,易于撰写电子邮件并以字节形式获取电子邮件并将其发送到 SES。
使用库 gopkg.in/gomail.v2
编写带附件的电子邮件,然后调用 WriteTo 方法。
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.
如果您尝试从字节附加文件:
msg.Attach("report.pdf", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(reportData)
return err
}))