使用GO语言实现AWS SES的邮件服务

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

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

 &lt;span style=&quot;font-family:lato,helvetica neue,helvetica,arial,sans-serif&quot;&gt;&lt;em&gt;Copyright &#169; 2021 Hive Wealth, All rights reserved.&lt;/em&gt;&lt;br&gt;&lt;/span&gt;&lt;br&gt; 

so I rewrite it as

 &lt;span style=&quot;font-family:lato,helvetica neue,helvetica,arial,sans-serif&quot;&gt;&lt;em&gt;Copyright &#169; &lt;script&gt;document.write(new Date().getFullYear())&lt;/script&gt; Hive Wealth, All rights reserved.&lt;/em&gt;&lt;br&gt;&lt;/span&gt;&lt;br&gt;

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(&quot;%s&quot;, &quot;./schemas/html/&quot;+template+&quot;.html&quot;))
	if err != nil {
		Logger.Error(&quot;err&quot;, zap.Any(&quot;err&quot;, err))
	}
	charSet := &quot;UTF-8&quot;
	input := &amp;ses.SendEmailInput{
		Destination: &amp;ses.Destination{
			CcAddresses: []*string{},
			ToAddresses: []*string{
				aws.String(recipient),
			},
		},
		Message: &amp;ses.Message{
			Body: &amp;ses.Body{
				Html: &amp;ses.Content{
					Charset: aws.String(charSet),
					Data:    aws.String(string(htmlBody)),
				},
				Text: &amp;ses.Content{
					Charset: aws.String(charSet),
					Data:    aws.String(textBody),
				},
			},
			Subject: &amp;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

&lt;em&gt;Copyright &#169; {{ .Year }} Hive Wealth, All rights reserved.&lt;/em&gt;


templateData := struct {
		Year int
	}{
		Year: time.Now().Year(),
	}
	// The HTML body for the email.
	newtemp, err := temp.ParseFiles(fmt.Sprintf(&quot;%s&quot;, &quot;./schemas/html/&quot;+template+&quot;.html&quot;))
	if err != nil {
		Logger.Error(&quot;template failed&quot;, zap.Any(&quot;err&quot;, err))
	}
	buf := new(bytes.Buffer)
	if err = newtemp.Execute(buf, templateData); err != nil {
		Logger.Error(&quot;template failed&quot;, zap.Any(&quot;err&quot;, err))
	}
	htmlBody := buf.String()

huangapple
  • 本文由 发表于 2021年11月20日 02:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70039203.html
匿名

发表评论

匿名网友

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

确定