Home > Back-end >  Mail service using AWS ses in GO
Mail service using AWS ses in GO

Time:11-25

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?

CodePudding user response:

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()
  • Related