英文:
How to implement aws ses SendRawEmail with attachment in golang
问题
我需要在golang中实现Amazon SES发送带附件的原始邮件,我尝试了以下代码:
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)
但是在我收到的邮件中,显示的是我在消息中提供的内容,而不是附件。不确定出了什么问题?
英文:
I need to implement Amazon ses SendRawEmail with attachment in golang,
i tried with the following code :
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)
but in the mail I receive, it shows the content which I have given in the message, instead of the attachment. Not sure what exactly is wrong???
答案1
得分: 14
参考这个AWS示例来发送带附件的原始邮件。
实现建议:使用gopkg.in/gomail.v2
库来组合带附件的电子邮件消息,并调用WriteTo方法。
var emailRaw bytes.Buffer
emailMessage.WriteTo(&emailRaw)
// 创建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() }
// 其余部分与你在问题中提到的相同。
英文:
Refer to AWS example for Sending RAW email with attachment.
Implementation Suggestion: for an easy to compose email and get email as bytes and send it to SES as mentioned in the above reference example.
Use library gopkg.in/gomail.v2
to compose your email message with attachment and then call WriteTo method.
var emailRaw bytes.Buffer
emailMessage.WriteTo(&emailRaw)
// while create instance of RawMessage
RawMessage: &ses.RawMessage{
Data: emailRaw.Bytes(),
}
Good luck!
EDIT: For the comment.
Compose the email-
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.
答案2
得分: 2
如果你想从字节中附加一个文件:
msg.Attach("report.pdf", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(reportData)
return err
}))
英文:
if you're trying to attach a file from bytes:
msg.Attach("report.pdf", gomail.SetCopyFunc(func(w io.Writer) error {
_, err := w.Write(reportData)
return err
}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论