使用 AWS Pinpoint and Go 发送包含 RAW 内容的电子邮件返回 403

我正在尝试通过 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))

    }

}


蝴蝶不菲
浏览 187回答 2
2回答

开满天机

我不是 Go 的人,所以这只是一次粗暴的尝试,试图绕过代码行以产生有效的 MIME 结构。func generateRawEmailContent(subject, to, from, HTMLBody string, attachments *[]EmailAttachment) []byte {&nbsp; &nbsp; buf := bytes.NewBuffer(nil)&nbsp; &nbsp; // Creating headers by gluing together strings is precarious.&nbsp; &nbsp; // I'm sure there must be a better way.&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("Subject: %s\n", subject))&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("To: %s\n", to))&nbsp; &nbsp; // Remove spurious newline&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("From: %s\n", from))&nbsp; &nbsp; writer := multipart.NewWriter(buf)&nbsp; &nbsp; boundary := writer.Boundary()&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("MIME-Version: 1.0\n", boundary))&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\n", boundary))&nbsp; &nbsp; // End of headers&nbsp; &nbsp; buf.WriteString("\n")&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("--%s\n", boundary))&nbsp; &nbsp; buf.WriteString("Content-Type: text/html; charset=\"UTF-8\";\n\n")&nbsp; &nbsp; buf.WriteString(HTMLBody)&nbsp; &nbsp; for _, attachment := range *attachments {&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteString(fmt.Sprintf("\n\n--%s\n", boundary))&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteString(fmt.Sprintf("Content-Type: %s\n", http.DetectContentType(attachment.Data)))&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteString("Content-Transfer-Encoding: base64\n")&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteString(fmt.Sprintf("Content-Disposition: attachment; filename=%s\n", attachment.FileName))&nbsp; &nbsp; &nbsp; &nbsp; b := make([]byte, base64.StdEncoding.EncodedLen(len(attachment.Data)))&nbsp; &nbsp; &nbsp; &nbsp; base64.StdEncoding.Encode(b, attachment.Data)&nbsp; &nbsp; &nbsp; &nbsp; buf.Write(b)&nbsp; &nbsp; &nbsp; &nbsp; // Don't add a second boundary here&nbsp; &nbsp; &nbsp; &nbsp; buf.WriteString("\n")&nbsp; &nbsp; }&nbsp; &nbsp; // Final terminating boundary, notice -- after&nbsp; &nbsp; buf.WriteString(fmt.Sprintf("\n--%s--\n", boundary))&nbsp; &nbsp; log.Println(string(buf.Bytes()))&nbsp; &nbsp; return buf.Bytes()}结果输出应该类似于Subject: subjectTo: recipient <victim@example.org>From: me <sender@example.net>MIME-Version: 1.0Content-Type: multipart/mixed; boundary=foobar--foobarContent-Type: text/html; charset="UTF-8"<h1>Tremble, victim</h1><p>We don't send <tt>text/plain</tt> because wehate our users.</p>--foobarContent-Type: application/octet-streamContent-Transfer-Encoding: base64Content-Disposition: attachment; filename=skull_crossbones.jpgYmluYXJ5ZGF0YQ==--foobar--

慕尼黑的夜晚无繁华

好的,找到问题了。事实证明,这个 403 错误与我的代码无关,而是与 AWS 中的 IAM 权限有关。显然,必须打开 IAM 权限才能启用 RAW 电子邮件内容。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go