arithmetric / aws-lambda-ses-forwarder

Serverless email forwarding using AWS Lambda and SES
MIT License
1.7k stars 450 forks source link

10MB+ Attachments Silently Fail #124

Open adrianmace opened 4 years ago

adrianmace commented 4 years ago

It would be excellent to have this function call sendMessage() again, but this time email the intended recipient address with a simple, "I couldn't process this message. The file in S3 is {FILENAME}."

adrianmace commented 4 years ago

Here's a crappy workaround I wrote super quickly just so that I ensured I was not missing emails.

/**
 * Send email using the SES sendRawEmail command.
 *
 * @param {object} data - Data bundle with context, email, etc.
 *
 * @return {object} - Promise resolved with data.
 */
exports.sendMessage = function(data) {
  var params = {
    Destinations: data.recipients,
    Source: data.originalRecipient,
    RawMessage: {
      Data: data.emailData
    }
  };
  data.log({
    level: "info",
    message: "sendMessage: Sending email via SES. Original recipients: " +
      data.originalRecipients.join(", ") + ". Transformed recipients: " +
      data.recipients.join(", ") + "."
  });
  return new Promise(function(resolve, reject) {
    data.ses.sendRawEmail(params, function(err, result) {
      if (err) {
        // If we encounter an error, we should let the recipient know at least so they can go and get the message from S3.
        params = {
          Destination: {
            ToAddresses: data.recipients
          },
          Source: data.originalRecipient,
          Message: {
            Subject: {
              Data: "Lambda: Email received but could not be forwarded!"
            },
            Body: {
              Text: {
                Data: "Hello from Lambda. I failed to forward something to you. The email ID is: " + data.email.messageId
              }
            }
          }
        };
        data.ses.sendEmail(params, function(err, result) {
          if (err) {
            data.log({
              level: "error",
              message: "sendEmail() failed to send a failure notification.",
              error: err,
              stack: err.stack
            });
            return reject(new Error('Error: Sending of failure notification failed.'));
          }
          data.log({
            level: "info",
            message: "Original failed, but sendEmail() of failure was successful.",
            result: result
          });
          resolve(data);
        });
      };
      data.log({
        level: "info",
        message: "sendRawEmail() successful.",
        result: result
      });
      resolve(data);
    });
  });
};