亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 AWS Pinpoint and Go 發送包含 RAW 內容的電子郵件返回 403

使用 AWS Pinpoint and Go 發送包含 RAW 內容的電子郵件返回 403

Go
蝴蝶不菲 2022-11-28 16:52:31
我正在嘗試通過 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))    }}
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

我不是 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"


<h1>Tremble, victim</h1>

<p>We don't send <tt>text/plain</tt> because we

hate our users.</p>


--foobar

Content-Type: application/octet-stream

Content-Transfer-Encoding: base64

Content-Disposition: attachment; filename=skull_crossbones.jpg


YmluYXJ5ZGF0YQ==

--foobar--


查看完整回答
反對 回復 2022-11-28
?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

好的,找到問題了。事實證明,這個 403 錯誤與我的代碼無關,而是與 AWS 中的 IAM 權限有關。顯然,必須打開 IAM 權限才能啟用 RAW 電子郵件內容。



查看完整回答
反對 回復 2022-11-28
  • 2 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號