amazon-web-services - 如何在golang中实现带有附件的aws ses SendRawEmail

标签 amazon-web-services go amazon-ses

我需要在 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.

关于amazon-web-services - 如何在golang中实现带有附件的aws ses SendRawEmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44669384/

相关文章:

amazon-web-services - 将 Google Cloud Messaging 的 'User Notification' 功能与 Amazon SNS 移动推送结合使用

string - 具有字符串索引的多级 slice

go - 如何覆盖默认的 404 未找到处理程序?

variables - 有没有办法获取范围内变量的名称和值?

bash - 来自 CRON 的 AWS SES sendmail 失败

amazon-web-services - Amazon SES通知(SNS)不起作用

ruby-on-rails-3.1 - ICS 文件的 Amazon SES 附件错误

amazon-web-services - AWS Cloudformation Elastic Load Balancing 账户 ID

amazon-web-services - Elastic Beanstalk Http 重定向到 Https

python - 无法从 FARGATE 任务内访问实例元数据