I have a webhook setup for parsing inbound emails, which works fine, but I'm using the mail api to reply to the sender of that email. I can send the email just fine, but I want to reply to the senders email and build an email thread. I couldn't find anything on this topic in Sendgrid's docs other than the ability to add custom headers.
After looking into how emails work in general, I learned about the In-Reply-To field and the References field. I successfully pulled the Message-ID from the sender's email and I set the In-Reply-To field to it in my messages header. On top of this, I append the Message-ID to the References string and send that along as well.
When I look at the raw email in Gmail, I can see that both fields are set, but it still sends as a new email rather than a reply to the original.
This is basically my code, I can't show everything, but if anything needs clarifying, I'll be more than willing to do so.
const msg = {
to: from,
headers: {
References: `${references} ${messageId}`,
'In-Reply-To': messageId
},
from: '[email protected]',
subject: 'subject',
text: 'reply',
};
sgMail.send(msg)
As far as the References
field goes, I'm just taking the previous references
from the sender's email and then appending the Message-ID to it. I have a feeling I might not be doing this right but I'm not totally sure.
Thanks for the help!
Best Regards,
Chris
It turns out in order to make a reply, you need to have to sender's email subject in your email's subject.
const msg = {
to: from,
headers: {
References: `${references} ${messageId}`,
'In-Reply-To': messageId
},
from: '[email protected]',
subject: `Re: ${subject}`, // where subject is the sender's subject.
text: 'reply'
};
Thanks for providing the solution @Chrischuck!
what is this variable supposed to be?
${references}
This SO post says that it's supposed to be the msg ID..is that what you use?
Most helpful comment
It turns out in order to make a reply, you need to have to sender's email subject in your email's subject.