如何使用golang中的附件实现aws se SendRawEmail [英] How to implement aws ses SendRawEmail with attachment in golang

查看:526
本文介绍了如何使用golang中的附件实现aws se SendRawEmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用golang中的附件实现Amazon ses SendRawEmail,

i用以下代码尝试:

  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> \\\\
To:xxx< xxx@xxx.com> \\\ \\ nSubject:测试电子邮件(包含附件)\\\\
MIME-Version:1.0 \\\\
Content-type:Multipart / Mixed; boundary = \NextPart \\\\\
\\\\
- NextPart \\\\
Content-Type:text / plain \\\\
\\\\
This是邮件正文。\\\\
\\\\
- NextPart \\\\
Content-Type: text / plain; \\\\
Content-Disposition:attachment; filename = \sample.txt\\\\\
\\\\
This是attac中的文本
input:= ses.SendRawEmailInput {source:source,Destinations:destinations,RawMessage:& message}
- NextPart--`)}输出,err:= svc.SendRawEmail(& input)

但在收到的邮件中,显示了我在消息中给出的内容,而不是附件。不知道究竟发生了什么错误???

解决方案

请参阅 AWS示例,用于发送包含附件的RAW电子邮件。

实现建议:为了便于撰写电子邮件并以字节为单位获取电子邮件并将其发送到SES,如上面的参考示例中所述。

使用库 gopkg.in/gomail.v2 用附件撰写您的电子邮件,然后致电 WriteTo 方法。

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

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

祝您好运




编辑:对于c

撰写电子邮件 -

  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>和< i> Cora< / i> ;!)
msg.Attach(/ home / Alex / lolcat.jpg)

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

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

//剩余部分与您提到的问题相同。


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, &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)

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???

解决方案

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(&emailRaw)

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

Good luck!


EDIT: For the comment.

Compose the email-

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() }

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

这篇关于如何使用golang中的附件实现aws se SendRawEmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆