使用 AWS Pinpoint 和 Go 发送包含 RAW 内容的电子邮件返回 403
来源:stackoverflow
时间:2024-03-17 17:18:28 306浏览 收藏
通过 AWS Pinpoint 发送包含附件的电子邮件时,可以使用“raw”电子邮件内容。通过示例代码了解如何生成“raw”内容,包括主题、收件人、发件人、HTML 正文和附件。需要注意的是,发送“raw”电子邮件内容需要启用 AWS 中的 IAM 权限。
我正在尝试通过 aws pinpoint 发送包含附件的电子邮件。要通过电子邮件发送附件,您必须使用“raw”电子邮件内容。我能找到的关于此的唯一文档在这里:https://docs.aws.amazon.com/pinpoint-email/latest/apireference/api_rawmessage.html,但它缺少很多东西(例如,需要什么)标题?)
当我使用“简单”内容发送电子邮件时,它工作正常:
emailinput := &pinpointemail.sendemailinput{ destination: &pinpointemail.destination{ toaddresses: []*string{&address}, }, fromemailaddress: &sender, content: &pinpointemail.emailcontent{ simple: &pinpointemail.message{ body: &pinpointemail.body{ html: &pinpointemail.content{ charset: &charset, data: &emailhtml, }, text: &pinpointemail.content{ charset: &charset, data: &emailtext, }, }, subject: &pinpointemail.content{ charset: &charset, data: &emailsubject, }, }, }
由于我想添加附件,因此必须使用“raw”内容类型。我编写了一个生成电子邮件内容的函数,基于:https://gist.github.com/douglasmakey/90753ecf37ac10c25873825097f46300:
func generaterawemailcontent(subject, to, from, htmlbody string, attachments *[]emailattachment) []byte { buf := bytes.newbuffer(nil) buf.writestring(fmt.sprintf("subject: %s\n", subject)) buf.writestring(fmt.sprintf("to: %s\n", to)) buf.writestring(fmt.sprintf("from: %s\n\n", from)) buf.writestring("mime-version: 1.0;\ncontent-type: text/html; charset=\"utf-8\";\n\n") buf.writestring(htmlbody) writer := multipart.newwriter(buf) boundary := writer.boundary() buf.writestring(fmt.sprintf("content-type: multipart/mixed; boundary=%s\n", boundary)) buf.writestring(fmt.sprintf("--%s\n", boundary)) for _, attachment := range *attachments { buf.writestring(fmt.sprintf("\n\n--%s\n", boundary)) buf.writestring(fmt.sprintf("content-type: %s\n", http.detectcontenttype(attachment.data))) buf.writestring("content-transfer-encoding: base64\n") buf.writestring(fmt.sprintf("content-disposition: attachment; filename=%s\n", attachment.filename)) b := make([]byte, base64.stdencoding.encodedlen(len(attachment.data))) base64.stdencoding.encode(b, attachment.data) buf.write(b) buf.writestring(fmt.sprintf("\n--%s", boundary)) } buf.writestring("--") log.println(string(buf.bytes())) return buf.bytes() }
这会生成以下内容(电子邮件已更改):
subject: welcome \nto: [email protected]\nfrom: [email protected]\n\nmime-version: 1.0;\ncontent-type: text/html; charset=\"utf-8\";\n\n\u003ch1\u003ehello ,\u003c/h1\u003e\u003cp\u003eyou now have an account.\u003c/p\u003e\ncontent-type: multipart/mixed; boundary=8f6b2cc498b79f5a99550b930ba1ecab1fc1ee2d3425a0a69ab67b83b647\n--8f6b2cc498b79f5a99550b930ba1ecab1fc1ee2d3425a0a69ab67b83b647\n\n\n--8f6b2cc498b79f5a99550b930ba1ecab1fc1ee2d3425a0a69ab67b83b647\ncontent-type: text/plain; charset=utf-8\ncontent-transfer-encoding: base64\ncontent-disposition: attachment; filename=test.json\newogicj0zxn0ijogdhj1zqp9\n--8f6b2cc498b79f5a99550b930ba1ecab1fc1ee2d3425a0a69ab67b83b647--
然后我按如下方式构建电子邮件:
&pinpointemail.sendemailinput{ destination: &pinpointemail.destination{ toaddresses: []*string{&address}, }, fromemailaddress: &sender, content: &pinpointemail.emailcontent{ raw: &pinpointemail.rawmessage{ data: generaterawemailcontent(emailsubject, address, sender, emailhtml, emailattachments), }, }
通过 github.com/aws/aws-sdk-go/service/pinpoint
包发送此电子邮件时,我收到返回的 403,我不知道为什么。 403 意味着我尝试访问的资源被禁止,但我不明白这与这里有什么关系?此外,没有任何文档表明 403 甚至是可能的响应。任何帮助将不胜感激!
我也尝试过使用库,例如 gomail-v2 库,如下所示:
m := gomail.NewMessage() m.SetHeader("From", from) m.SetHeader("To", to) m.SetHeader("Subject", subject) m.SetBody("text/plain", textBody) m.AddAlternative("text/html", HTMLBody) m.Attach("foo.txt", gomail.SetCopyFunc(func(w io.Writer) error { _, err := w.Write((*attachments)[0].Data) return err })) buf := bytes.NewBuffer(make([]byte, 0, 2048)) _, werr := m.WriteTo(buf) if werr != nil { return nil, common.NewStackError(werr) }
但这仍然给我一个 403 错误。
正确答案
好的,找到问题了。事实证明,这个 403 错误与我的代码无关,而是与 aws 中的 iam 权限有关。显然,必须打开 iam 权限才能启用 raw 电子邮件内容。
我不是 go 用户,因此这只是一次粗暴的尝试,旨在对代码行进行调整,以期生成有效的 mime 结构。
func generaterawemailcontent(subject, to, from, htmlbody string, attachments *[]emailattachment) []byte { buf := bytes.newbuffer(nil) // creating headers by gluing together strings is precarious. // i'm sure there must be a better way. buf.writestring(fmt.sprintf("subject: %s\n", subject)) buf.writestring(fmt.sprintf("to: %s\n", to)) // remove spurious newline buf.writestring(fmt.sprintf("from: %s\n", from)) writer := multipart.newwriter(buf) boundary := writer.boundary() buf.writestring(fmt.sprintf("mime-version: 1.0\n", boundary)) buf.writestring(fmt.sprintf("content-type: multipart/mixed; boundary=%s\n", boundary)) // end of headers buf.writestring("\n") buf.writestring(fmt.sprintf("--%s\n", boundary)) buf.writestring("content-type: text/html; charset=\"utf-8\";\n\n") buf.writestring(htmlbody) for _, attachment := range *attachments { buf.writestring(fmt.sprintf("\n\n--%s\n", boundary)) buf.writestring(fmt.sprintf("content-type: %s\n", http.detectcontenttype(attachment.data))) buf.writestring("content-transfer-encoding: base64\n") buf.writestring(fmt.sprintf("content-disposition: attachment; filename=%s\n", attachment.filename)) b := make([]byte, base64.stdencoding.encodedlen(len(attachment.data))) base64.stdencoding.encode(b, attachment.data) buf.write(b) // don't add a second boundary here buf.writestring("\n") } // final terminating boundary, notice -- after buf.writestring(fmt.sprintf("\n--%s--\n", boundary)) log.println(string(buf.bytes())) return buf.bytes() }
结果输出应该类似于
Subject: subject To: recipient <[email protected]> From: me <[email protected]> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=foobar --foobar Content-Type: text/html; charset="UTF-8"Tremble, victim
We don't send text/plain because we hate our users.
--foobar Content-Type: application/octet-stream Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=skull_crossbones.jpg YmluYXJ5ZGF0YQ== --foobar--
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习