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

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

Golang AWS SES xlsx 附件已損壞

Golang AWS SES xlsx 附件已損壞

Go
慕哥9229398 2022-08-24 11:36:29
在golang中,我正在嘗試發送一封附加了XLSX文件的電子郵件。我用于將XLSX文件生成為字節數組,并且當與Web服務器(此處為gin)一起提供時,它工作良好,如下所示:github.com/tealeg/xlsx/v3c.Header("Content-Description", "File Transfer")c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")c.Header("Content-Disposition", "attachment; filename="+time.Now().UTC().Format("daily-alerts-20060102.xlsx"))c.Data(http.StatusOK, "application/octet-stream", fileContent)但是,當使用SES發送時,當CSV文件正確提供時,6ko XLSX文件會變成10ko XLSX文件,并且不可能使用Excel打開它:func (s *EmailSenderParams) SendEmailWithAttachment(content string, data *models.RawEmailData, attachment []byte, attachmentFilename string) error {    sess, err := session.NewSession(&aws.Config{        Region: aws.String(s.awsRegion)},    )    creds := credentials.NewStaticCredentials(s.apiID, s.apiKey, "")    // Create an SES session.    svc := ses.New(sess, &aws.Config{Credentials: creds})    // Assemble the email.    buf := new(bytes.Buffer)    writer := multipart.NewWriter(buf)    // email main header:    h := make(textproto.MIMEHeader)    //h.Set("From", source)    h.Set("To", data.ReceiverMail)    //h.Set("Return-Path", source)    h.Set("Subject", data.Subject)    h.Set("Content-Language", "en-US")    h.Set("Content-Type", "multipart/mixed; boundary=\""+writer.Boundary()+"\"")    h.Set("MIME-Version", "1.0")    _, err = writer.CreatePart(h)    if err != nil {        return err    }    // body:    h = make(textproto.MIMEHeader)    h.Set("Content-Transfer-Encoding", "7bit")    h.Set("Content-Type", "text/plain; charset=us-ascii")    part, err := writer.CreatePart(h)    if err != nil {        return err    }    _, err = part.Write([]byte(content))    if err != nil {        return err    }我認為MIMEHeaders,多部分或編碼可能有問題,但我正在努力尋找什么。您是否知道任何成功的方法可以發送帶有附加了 AWS SES 的 XLSX 文件的電子郵件?
查看完整描述

1 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

解決方案是將文件設置為 base64 編碼,并將 header 設置為 base64Content-Transfer-Encoding


    h.Set("Content-Transfer-Encoding", "base64")

    part, err = writer.CreatePart(h)

    if err != nil {

        return err

    }


    b := make([]byte, base64.StdEncoding.EncodedLen(len(attachment)))

    base64.StdEncoding.Encode(b, attachment)


    _, err = part.Write(b)


查看完整回答
反對 回復 2022-08-24
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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