1

I am using Amplify and Appsync for a small website and I am trying to create a contact form and I need to send an email after the mutation. Can anyone suggest the best way to approach this?

1 Answer 1

1

It is actually quite simple. When you do a Mutation you can Invoke a lambda and execute the following code using SES from the aws-sdk.

You trigger the lambda within AppSync as you choose the function as the Mutations datasource (don't forget to have the proper IAM permission for this). Then you need two mapping templates one for the request and one for the response. With the request you can pass the input parameters of the Mutation endpoint to the lambda.

It could look like this for the mappingTemplate.request.vtl

{
    "version": "2018-05-29",
    "operation": "Invoke",
    "payload": {
        "field": "fieldVariable"
        "arguments": $utils.toJson($context.arguments)
    }
}

And for the mappingTemplate.response.vtl

#if( $context.result && $context.result.error )
    $utils.error($context.result.error)
#else
    $utils.toJson($context.result)
#end

This will execute your lambda and you have your passed arguments within event.aguments

import { SES } from 'aws-sdk';
...

exports.handler = async event => {
  const bccEmailAddresses = [];
  const ccEmailAddresses = [];
  const toEmailAddresses = [];
  const bodyData = '';
  const bodyCharset = 'UTF-8';
  const subjectdata = '';
  const subjectCharset = 'UTF-8';
  const sourceEmail = '';
  const replyToAddresses = [];

  const emailParams = {
    Destination: {
      BccAddresses: bccEmailAddresses,
      CcAddresses: ccEmailAddresses,
      ToAddresses: toEmailAddresses
    },
    Message: {
      Body: {
        Text: {
          Data: bodyData,
          Charset: bodyCharset
        }
      },
      Subject: {
        Data: subjectdata,
        Charset: subjectCharset
      }
    },
    Source: sourceEmail,
    ReplyToAddresses: replyToAddresses
  };

  await SES.sendEmail(emailParams).promise();
}
3
  • And how to trigger the Lambda?
    – MaiKaY
    Sep 16, 2019 at 7:35
  • 1
    I edited my answer so you see how to trigger your lambda. There is also a lot of documentation for this topic since there is a lot more to it. You need to have the right permission for example etc. etc. aws.amazon.com/de/blogs/mobile/…
    – benra
    Sep 16, 2019 at 7:47
  • 1
    @benra Thanks for your suggestion. One question. As far as I understand, there are 2 graphql calls there, the first one is the mutation itself (not here), and the second one is sending the email. How do you trigger that? Do you have one of those queries with 2 queries inside? Or do you launch the second one from the client? Is it possible to delegate everything to the server and only send the mutation from the client? Thanks a lot for your suggestions.
    – Ricardo
    Jan 1, 2020 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.