So I am sending an email reply through SendGrid and I have a message object something like this:
message = {
"personalizations": context["personalizations"],
"from": {"email": context["sender_email"]},
"subject": context["subject"],
"content": [{"type": MimeType.html, "value": context["body"]}],
"reply_to": {"email": context["reply_to"]},
"headers": {"In-Reply-To": "<Prev-Message-ID>",
"References": "<Prev-Message-ID>",
}
}
sg = SendGridAPIClient(os.environ.get("SENDGRID_API_KEY"))
sg.send(message)
Now when I go to the 'Show Original' in Gmail, the email does have References and In-Reply-To in headers. Something like this:
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=us-ascii
Date: Thu, 04 Aug 2022 05:47:05 +0000 (UTC)
From: test.Taylor-Hood_b56ef494-4d5e-4568-bcf5-
bc68d489f86b@hirecinch.com
Mime-Version: 1.0
Message-ID: <3S2bF8n9Rj-0eNQWf172Gw@geopod-ismtpd-4-0>
Subject: Hakunamatata!!!
Reply-To:
3b0b71af9b8ba94577730eb010f0887e@mailer.local.hirecinch.com
In-Reply-To: <CABQc7oqgKENUUAz6Mg4kdS7ZS8Q3Wq95DPNo-O2-
18wyaVaXgw@mail.gmail.com>
References: <CABQc7oqgKENUUAz6Mg4kdS7ZS8Q3Wq95DPNo-O2-
18wyaVaXgw@mail.gmail.com>
However, the email I send is never appended as a reply and always makes a new thread. What am I doing wrong?? Is it about the subject of the email which I send in reply?? I have tried doing Re: The Subject, but it still doesn't work. I have to display the whole conversation as a thread having sub-threads in my product and I'm stuck.
To reply in a thread you need to include a few things.
Firstly, add the In-Reply-To
and References
headers to the email both with the ID of the email you are replying to.
That will cause the email to thread, but if you are looking to emulate email client behaviour, you will also need to send the previous email's content, normally after the new content in the email.