coops22
coops22

Reputation: 481

AWS Cognito - Custom email message with Lambda trigger being overwritten

I'm using a lambda function to customize confirmation emails with AWS Cognito. My lambda function seems to work fine and looks like this:

exports.handler = async (event, context, callback) => {
  const sampleTemplate = `<html>
    <body>
      <div>${event.request.codeParameter}</div>
      <div>${event.userName}</div>
    </body>
  </html>`


  if (event.triggerSource === "CustomMessage_AdminCreateUser") {
    event.response.emailSubject = 'Lets hope this works'
    event.response.emailMessage = sampleTemplate

    console.log(event.response) // Logs look as expected
  }

  callback(null, event);
};

The problem is that when the emails arrive the message body is being overwritten by the content in the User Pools > Message Customizations tab. The subject line is working fine, but the email body is being overwritten. For example, the cognito settings look like this: enter image description here

And the emails look like this: enter image description here

As you can see, the lambda function worked for setting the email's subject line, but not the actual content. I can't find any setting to turn off that content and it can't be left blank... Any help is much appreciated.

Upvotes: 6

Views: 6728

Answers (4)

Raymel Ramos Guerra
Raymel Ramos Guerra

Reputation: 11

It is mandatory to add event.request.codeParameter and event.request.usernameParameter in your template, if not, cognito will replace your template with the one defined by default

Upvotes: 0

Fuzzy Neo
Fuzzy Neo

Reputation: 71

We MUST include both the "{username}" and "{####}" placeholder for the custom template for CustomMessage_AdminCreateUser to work. We can place this ourselves in the html file or via the event object's values for the keys event.request.usernameParameter and event.request.codeParameter respectively.

In summary, the html file for CustomMessage_AdminCreateUser must include these two values:

event.request.usernameParameter (has value "{username}") and event.request.codeParameter (has value "{####}")

Upvotes: 6

Mohib Arshi
Mohib Arshi

Reputation: 830

If after doing everything, your custom email template doesn't show up then check the following:

Upvotes: 5

coops22
coops22

Reputation: 481

For anyone finding this, I was able to find the answer. When using the CustomMessage_AdminCreateUser event, cognito will silently throw an error if you use event.userName in the template. Instead use event.request.usernameParameter and it will work

Upvotes: 11

Related Questions