aws-lambda-ses-forwarder icon indicating copy to clipboard operation
aws-lambda-ses-forwarder copied to clipboard

Support for dynamic list of recipients

Open dgorbash opened this issue 7 years ago • 0 comments

Summary

  1. Add support for functions/Promises in forwardMapping recipients list. For example:
{
  ...
  forwardMapping: {
    "[email protected]": [
      Promise.resolve("[email protected]"),
      () => Promise.resolve("[email protected]"),
      data => {
         // do some stuff
         return "[email protected]";
      }
    ]
  },
  ...
}
  1. New config option skipRejectedRecipients (boolean). If true all rejected promises in forwardMapping will be silently ignored. If false (by default) any rejected promise will cancel forwarding (may be used for validation).

Examples

  1. Validate incoming emails and send junk messages into a separate mailbox:
{
  ...
  forwardMapping: {
    "[email protected]": [
      data => {
        let dataReceipt = data.event.Records[0].ses.receipt;
        if (dataReceipt.spamVerdict.status !== 'PASS' ||
            dataReceipt.dkimVerdict.status !== 'PASS' ||
            dataReceipt.spfVerdict.status !== 'PASS' ||
            dataReceipt.virusVerdict.status !== 'PASS') {  
          return '[email protected]';
        } else {
          return '[email protected]';
        }
      }
    ]
  }
}
  1. Check for custom header and stop forwarding if check fails:
{
  ...
  skipRejectedRecipients: false,
  forwardMapping: {
    "[email protected]": [
      '[email protected]',
      data => {
        let headers = data.event.Records[0].ses.mail.headers;
        let header = headers.find(h => h.name === 'X-MyCompany-Signature');
        if (!header || header.value !== 'VALID-SIGNATURE') {  
          return Promise.reject('Invalid signature');
        }
        // Empty values will be ignored so return nothing
      }
    ]
  }
}
  1. Get list of recipients from remote source:
{
  ...
  skipRejectedRecipients: true,
  forwardMapping: {
    "[email protected]": [
      '[email protected]',
      () => {
        // If returned Promise will be rejected due to errors, message will be forwarded to [email protected] only
        return MyAwesomeApi.getEmails() // example response: [email protected], [email protected]
          .then(list => {
            // It is ok to return array of email addresses
            return list.split(/\s*,\s*/); // output: [ "[email protected]", "[email protected]" ]
          })
      }
    ]
  }
}

dgorbash avatar Oct 23 '17 19:10 dgorbash