英文:
Mail service using AWS ses in GO
问题
我正在使用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>版权所有 © <script>document.write(new Date().getFullYear())</script> Hive Wealth, 保留所有权利。</em><br></span><br>
当我在浏览器中打开我的HTML时,它显示了正确的年份。但在邮件中查看时,它显示为空值。
这是我的邮件调用部分。但年份没有按预期显示,只是显示为空。
我该如何实现这一点?
在我看来,HTML被转换为字符串结构,这就是为什么日期没有显示的原因。这个理解正确吗?
我该如何在我的邮件模板页脚部分获取当前年份?
英文:
I am using AWS SES for mail service. Following the package
"github.com/aws/aws-sdk-go/service/ses"
using this I found that HTML data is passing as a string
in one of my scenario i want to show year dynamically.. means now i want to show 2021 next year 2022 in mail footer section
<span style="font-family:lato,helvetica neue,helvetica,arial,sans-serif"><em>Copyright © 2021 Hive Wealth, All rights reserved.</em><br></span><br>
so I rewrite it as
<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>
When i Open my html in browser it showing correct year.. But after looking in mail its showing empty value
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)
this is my mail calling section. But the year is not coming as expected.. it just showing empty..
How can I achieve this ?
In my view, the HTML is converting as a string structure thats why date is not showing ? is that correct ??
how can i Get my current year in my mail template footer section?
答案1
得分: 0
答案是:
版权所有 © {{ .Year }} Hive Wealth,保留所有权利。
templateData := struct {
Year int
}{
Year: time.Now().Year(),
}
// 邮件的 HTML 正文。
newtemp, err := temp.ParseFiles(fmt.Sprintf("%s", "./schemas/html/"+template+".html"))
if err != nil {
Logger.Error("模板解析失败", zap.Any("err", err))
}
buf := new(bytes.Buffer)
if err = newtemp.Execute(buf, templateData); err != nil {
Logger.Error("模板执行失败", zap.Any("err", err))
}
htmlBody := buf.String()
英文:
Answer is
<em>Copyright © {{ .Year }} Hive Wealth, All rights reserved.</em>
templateData := struct {
Year int
}{
Year: time.Now().Year(),
}
// The HTML body for the email.
newtemp, err := temp.ParseFiles(fmt.Sprintf("%s", "./schemas/html/"+template+".html"))
if err != nil {
Logger.Error("template failed", zap.Any("err", err))
}
buf := new(bytes.Buffer)
if err = newtemp.Execute(buf, templateData); err != nil {
Logger.Error("template failed", zap.Any("err", err))
}
htmlBody := buf.String()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论