0

Summary

SendGrid inbound parse calls webhook with an email payload. This question is about extracting the time with timezone for the received mail. The email payload can contain forwarded emails too.

How can the time of each email thread in an email payload be accurately known?

Sample code

For a sample code for SendGrid, please refer to this repo by shammelburg https://github.com/shammelburg/express-sendgrid-inbound-parse/blob/main/index.js

You'll need a SendGrid account to test this.

Attempted solution

The `Received' header in the fields from SendGrid contains timezone information.

let receivedHeader = fields.headers.trim().match(/.*X-Received:.*/)[0];

  const timezoneCode = receivedHeader
    .match(/\(.*\)/)[0]
    .replace("(", "")
    .replace(")", "");

  console.log({ timezoneCode });

Why this failed: This timezone was inaccurate and didn't reflect the time for each mail. The timezone was based on one of the senders only in a forwarded email thread and was inaccurate for the other emails in the thread.

Additional snippets to help with testing

Below code allows you to view each email in a forwarded email thread

const replyParser = require("node-email-reply-parser");

let fragments = replyParser(fields.text).getFragments();

  fragments.forEach(f => {
    console.log(f.getContent());
  })
Swimburger
  • 6,681
  • 6
  • 36
  • 63
AliAvci
  • 1,127
  • 10
  • 20

0 Answers0