1

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.

Ahmad
  • 15
  • 5
  • In [this answer](https://stackoverflow.com/a/70416867/28376) they set the `Message-ID` header to the same as the original message too. Can you try that? – philnash Aug 04 '22 at 10:51
  • I can't have a Message-ID when I'm sending the mail. I mean I do receive it as a webhook response by the name smtp_id but while I'm sending the email through SendGrid, I can't know the Message-ID beforehand. – Ahmad Aug 04 '22 at 11:19
  • I thought you were trying to send a message in reply. The previous answer suggested using the same message ID in the `In-Reply-To`, `References` and `Message-ID` headers. – philnash Aug 04 '22 at 19:41
  • Yes, I'm trying to send a message in reply. When you look at the previous answer closely, the In-Reply-To and Reference have the same IDs but not the Message-ID. Some last digits are different. Plus it doesn't really make sense to have the same Message-ID and In-Reply-To because the Message-ID has to be the ID of the message that I'm sending. No??? – Ahmad Aug 05 '22 at 04:23
  • The detailed data of the email on Gmail does have In-Reply-To and References but I can't click on the email and see which email it was a reply of. So basically Gmail doesn't really treat it as a reply even though the headers have all the information a reply email should have. – Ahmad Aug 05 '22 at 07:03
  • Do you have [conversation view](https://i.stack.imgur.com/vfVeZ.png) turned on in Gmail? I just sent a reply to myself with these headers and it worked. – philnash Aug 08 '22 at 01:17
  • Yes, my conversation view is on. By worked, do you mean that the email was sent as a reply and that you could click on it(the 3 dots) and then the email body to which it was a reply, appended right there??? Did it work like a legit Gmail reply? – Ahmad Aug 08 '22 at 08:10
  • My emails definitely arrived in the thread. I could only get the previous email content to show by including that previous email content in the email I sent (which is how it works when receiving replies from Gmail). [This is what I saw](https://i.stack.imgur.com/lYUfY.png) The top email in the screenshot was sent from Gmail, the middle email, with the dots, was sent by me through SendGrid with the previous content, and the last one was a reply from SendGrid, but without including the previous content. – philnash Aug 09 '22 at 00:27
  • Okay, now this is kind of confusing. I thought Gmail would just be smart enough to know the previous content when I tell it which email it's the reply of using In-Reply-To. Now, do I have to manually send all the previous content? Can I please see how that works? Because I don't know if there is a way to tell SendGrid about the content of the previous email that I'm replying to. – Ahmad Aug 10 '22 at 04:10
  • If you reply to an email from Gmail and look at the contents, you'll find all the previous emails there. Gmail's three dots is actually a courtesy to the user to not show them all of the contents of all of the emails because of the threading that, if I recall correctly, they invented. Other email clients that don't thread display all the content in the email. So, in order to send a reply and have the previous content in it, you will need to take the previous content from the incoming email that you received and include it in the body of the email that you are sending. – philnash Aug 10 '22 at 04:16
  • This is pretty dumb from Gmail, lol. I thought if I provide the In-Reply-To, it will just know all the content of the replied email. Now I have to rethink my solution. Anyways, thank you very very much for helping me out all the while Phil. – Ahmad Aug 10 '22 at 04:26
  • It's the opposite of dumb, from Gmail at least. Before Gmail all we had were hundreds of emails that all contained the contents of all the previous emails. Gmail tidied them into threads and hid the old content because you can see it in the thread. But you still have to deal with other email clients that don't thread responses, and they expect all the previous contents. Also, check what happens when you write a reply in Gmail. Those three dots are there in the reply hiding away all the previous content you're sending. Sending all the content makes it backwards compatible across over 25 years. – philnash Aug 10 '22 at 04:43
  • Now I am starting to understand how actually does Gmail work. Plus the three-dot thing while replying, I always knew it was there but never really thought it the way that it was actually appending my previous content. It does look really tough to replicate a Gmail-like structure for emails in my product. Wish me luck :) – Ahmad Aug 10 '22 at 05:45

1 Answers1

2

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.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • 1
    On all of the internet, I could just find the solutions with In-Reply-To and References thing. Never knew the content of the previous email had to be sent too. Thank mate! – Ahmad Aug 10 '22 at 05:59