killmenot / nodemailer-postmark-transport

Postmark transport for Nodemailer
MIT License
32 stars 12 forks source link

`sendEmailBatch` breaks the message order from Postmark #23

Closed danielmcconville closed 11 months ago

danielmcconville commented 11 months ago

Hi,

The _requestFactory function splits the results from client calls into 2 arrays, accepted and rejected.

Although this is useful, it causes a problem when sending batch emails and trying to reconcile failed messages against the input payload. When a message fails, Postmark sends back the following JSON:

{
  "ErrorCode": 405,
  "Message": "details"
}

This does not contain any identifier to indicate the original message that failed. This is fine in single emails but a problem in batch emails where you may have both successes and failures.

Postmark docs say that the results will be ordered the same as the original messages and leaves it to the user to compare the input against the results to match up the responses with the original messages.

Unfortunately this order is lost in the following code of the _requestFactory:

      results.forEach((result) => {
        if (result.ErrorCode === 0) {
          accepted.push(result);
        } else {
          rejected.push(result);
        }
      });

      return callback(null, {
        messageId: (results[0] || {}).MessageID,
        accepted: accepted,
        rejected: rejected
      });

A simple fix would be to update the _requestFactory to return an extra field that contains the original results, e.g.:

  export interface SentMessageInfo {
    messageId?: string;
    accepted: Array<Models.MessageSendingResponse>;
    rejected: Array<Models.MessageSendingResponse>;
    originalResults: Array<Models.MessageSendingResponse>;
  }

Consumers can then access that array if they wish to match the input to the output. I need this code so will raise a PR.

killmenot commented 11 months ago

Fixed via https://github.com/killmenot/nodemailer-postmark-transport/commit/7b0928995d939de884a81655c41f060f798193e6