在 GO 中使用 AWS ses 的邮件服务

我正在使用 AWS SES 进行邮件服务。按照包“github.com/aws/aws-sdk-go/service/ses”


使用这个我发现 HTML 数据作为字符串传递


在我的一个场景中,我想动态显示年份..意味着现在我想在邮件页脚部分显示 2021 明年 2022


 <span style="font-family:lato,helvetica neue,helvetica,arial,sans-serif"><em>Copyright © 2021 Hive Wealth, All rights reserved.</em><br></span><br> 

所以我将其重写为


 <span style="font-family:lato,helvetica neue,helvetica,arial,sans-serif"><em>Copyright © <script>document.write(new Date().getFullYear())</script> Hive Wealth, All rights reserved.</em><br></span><br>

当我在浏览器中打开我的 html 时,它显示正确的年份。但是在查看邮件后,它显示为空值


htmlBody, err := ioutil.ReadFile(fmt.Sprintf("%s", "./schemas/html/"+template+".html"))

    if err != nil {

        Logger.Error("err", zap.Any("err", err))

    }

    charSet := "UTF-8"

    input := &ses.SendEmailInput{

        Destination: &ses.Destination{

            CcAddresses: []*string{},

            ToAddresses: []*string{

                aws.String(recipient),

            },

        },

        Message: &ses.Message{

            Body: &ses.Body{

                Html: &ses.Content{

                    Charset: aws.String(charSet),

                    Data:    aws.String(string(htmlBody)),

                },

                Text: &ses.Content{

                    Charset: aws.String(charSet),

                    Data:    aws.String(textBody),

                },

            },

            Subject: &ses.Content{

                Charset: aws.String(charSet),

                Data:    aws.String(subject),

            },

        },

        Source: aws.String(sender),

    }

    result, err := ns.SendEmail(input)

这是我的邮件呼叫部分。但是这一年并没有像预期的那样到来..它只是显示为空..


我怎样才能做到这一点?


在我看来,HTML 正在转换为字符串结构,这就是为什么没有显示日期?那是对的吗 ??


如何在我的邮件模板页脚部分获取我的当前年份?


梦里花落0921
浏览 122回答 1
1回答

FFIVE

答案是<em>Copyright © {{ .Year }} Hive Wealth, All rights reserved.</em>templateData := struct {&nbsp; &nbsp; &nbsp; &nbsp; Year int&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; Year: time.Now().Year(),&nbsp; &nbsp; }&nbsp; &nbsp; // The HTML body for the email.&nbsp; &nbsp; newtemp, err := temp.ParseFiles(fmt.Sprintf("%s", "./schemas/html/"+template+".html"))&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; Logger.Error("template failed", zap.Any("err", err))&nbsp; &nbsp; }&nbsp; &nbsp; buf := new(bytes.Buffer)&nbsp; &nbsp; if err = newtemp.Execute(buf, templateData); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; Logger.Error("template failed", zap.Any("err", err))&nbsp; &nbsp; }&nbsp; &nbsp; htmlBody := buf.String()
打开App,查看更多内容
随时随地看视频慕课网APP