如何在Golang中使用AWS SES的SendRawEmail方法实现带附件的邮件发送?

huangapple go评论84阅读模式
英文:

How to implement aws ses SendRawEmail with attachment in golang

问题

我需要在golang中实现Amazon SES发送带附件的原始邮件,我尝试了以下代码:

session, err := session.NewSession()
svc := ses.New(session, &aws.Config{Region: aws.String("us-west-2")})

source := aws.String("XXX <xxx@xxx.com>")
destinations := []*string{aws.String("xxx <xxx@xxx.com>")}
message := ses.RawMessage{ Data: []byte(`From: xxx <xxx@xxx.com>\nTo: xxx <xxx@xxx.com>\nSubject: Test email (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary="NextPart"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename="sample.txt"\n\nThis is the text in the attachment.\n\n--NextPart--`)}
input := ses.SendRawEmailInput{Source: source, Destinations: destinations, RawMessage: &message}
output, err := svc.SendRawEmail(&input)

但是在我收到的邮件中,显示的是我在消息中提供的内容,而不是附件。不确定出了什么问题?

英文:

I need to implement Amazon ses SendRawEmail with attachment in golang,
i tried with the following code :

session, err := session.NewSession()
svc := ses.New(session, &amp;aws.Config{Region: aws.String(&quot;us-west-2&quot;)})

source := aws.String(&quot;XXX &lt;xxx@xxx.com&gt;&quot;)
destinations := []*string{aws.String(&quot;xxx &lt;xxx@xxx.com&gt;&quot;)}
message := ses.RawMessage{ Data: []byte(` From: xxx &lt;xxx@xxx.com&gt;\\nTo: xxx  &lt;xxx@xxx.com&gt;\\nSubject: Test email (contains an attachment)\\nMIME-Version: 1.0\\nContent-type: Multipart/Mixed; boundary=\&quot;NextPart\&quot;\\n\\n--NextPart\\nContent-Type: text/plain\\n\\nThis is the message body.\\n\\n--NextPart\\nContent-Type: text/plain;\\nContent-Disposition: attachment; filename=\&quot;sample.txt\&quot;\\n\\nThis is the text in the attachment.\\n\\n--NextPart--&quot; `)}
input := ses.SendRawEmailInput{Source: source, Destinations: destinations, RawMessage: &amp;message}
output, err := svc.SendRawEmail(&amp;input)

but in the mail I receive, it shows the content which I have given in the message, instead of the attachment. Not sure what exactly is wrong???

答案1

得分: 14

参考这个AWS示例来发送带附件的原始邮件。

实现建议:使用gopkg.in/gomail.v2库来组合带附件的电子邮件消息,并调用WriteTo方法。

var emailRaw bytes.Buffer
emailMessage.WriteTo(&emailRaw)

// 创建RawMessage实例时
RawMessage: &ses.RawMessage{
    Data: emailRaw.Bytes(),
}

祝你好运!


**编辑:**针对评论。

组合电子邮件:

msg := gomail.NewMessage()
msg.SetHeader("From", "alex@example.com")
msg.SetHeader("To", "bob@example.com", "cora@example.com")
msg.SetHeader("Subject", "Hello!")
msg.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
msg.Attach("/home/Alex/lolcat.jpg")

var emailRaw bytes.Buffer
msg.WriteTo(&emailRaw)

message := ses.RawMessage{ Data: emailRaw.Bytes() }

// 其余部分与你在问题中提到的相同。
英文:

Refer to AWS example for Sending RAW email with attachment.

Implementation Suggestion: for an easy to compose email and get email as bytes and send it to SES as mentioned in the above reference example.

Use library gopkg.in/gomail.v2 to compose your email message with attachment and then call WriteTo method.

var emailRaw bytes.Buffer
emailMessage.WriteTo(&amp;emailRaw)

// while create instance of RawMessage
RawMessage: &amp;ses.RawMessage{
    Data: emailRaw.Bytes(),
}

Good luck!


EDIT: For the comment.

Compose the email-

msg := gomail.NewMessage()
msg.SetHeader(&quot;From&quot;, &quot;alex@example.com&quot;)
msg.SetHeader(&quot;To&quot;, &quot;bob@example.com&quot;, &quot;cora@example.com&quot;)
msg.SetHeader(&quot;Subject&quot;, &quot;Hello!&quot;)
msg.SetBody(&quot;text/html&quot;, &quot;Hello &lt;b&gt;Bob&lt;/b&gt; and &lt;i&gt;Cora&lt;/i&gt;!&quot;)
msg.Attach(&quot;/home/Alex/lolcat.jpg&quot;)

var emailRaw bytes.Buffer
msg.WriteTo(&amp;emailRaw)

message := ses.RawMessage{ Data: emailRaw.Bytes() }

// Remaining is same as what you mentioned the question.

答案2

得分: 2

如果你想从字节中附加一个文件:

msg.Attach("report.pdf", gomail.SetCopyFunc(func(w io.Writer) error {
    _, err := w.Write(reportData)
    return err
}))
英文:

if you're trying to attach a file from bytes:

msg.Attach(&quot;report.pdf&quot;, gomail.SetCopyFunc(func(w io.Writer) error {
	_, err := w.Write(reportData)
	return err
}))

huangapple
  • 本文由 发表于 2017年6月21日 15:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/44669384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定