登录
首页 >  Golang >  Go问答

使用 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学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>