Search code examples
javascripthtmlnode.jsamazon-ses

How can I make email requests work with dynamic HTML responses?


I am sending HTML email using AWS SES (Nodejs). It's working with static HTML, but I don't know how to make it work with a dynamic HTML.

nodejs file

HTML = "..." // load external html file
var params = {
    Destination: {/* required */
        ToAddresses:[abc@gmail.com, test@gmail.com]
    },
    Message: {/* required */
        Body: { /* required */
            Html: {
                Data: HTML,
                Charset: 'UTF-8'
            },
        },
        Subject: { /* required */
            Data: 'Test email', /* required */
            Charset: 'UTF-8'
        }
    },
    Source: "myemail@gmail.com"
}

ses.sendEmail(params, function(err, data) {
  // If something goes wrong, print an error message.
  if(err) {
    console.log(err.message);
  } else {
    console.log("Email sent! Message ID: ", data.MessageId);
  }
});

html file

<html>
<head></head>
<body>
  <h1>Amazon SES Test</h1>
  <p>Your order is placed.
    <a href='https://example.com/id=1234125'>View your order</a> 
   </p>
</body>
</html>

My question: how to pass href from nodejs file to html file as a variable.

Any suggestion is appreciated.


Solution

  • You could have a placeholder in your HTML, e.g: <a href="HREF_PLACEHOLDER">...</a>.

    And then use string.replace() on your HTML to replace it with the actual href you want.

    For example:

    function dynamicHtml(href) {
      return HTML.replace("HREF_PLACEHOLDER", href);
    }
    

    And instead of passing HTML to your params object, pass it dynamicHtml()