Sendgrid-csharp: Help Wanted: Guidance on properly creating a conversation thread

Created on 7 Jun 2019  路  6Comments  路  Source: sendgrid/sendgrid-csharp

I contacted support and was told to ask this question here, so I hope that's okay.

We鈥檙e trying to develop our app much like an email client that handles proper threading back and forth. I noticed others were trying to resolve this:

https://github.com/sendgrid/sendgrid-nodejs/issues/690#issuecomment-393336215
https://github.com/sendgrid/sendgrid-nodejs/issues/914
https://github.com/sendgrid/sendgrid-php/issues/386

Here's what I'm doing:

When I send the first email to the recipient, I create a conversation in my own database and store the that as a custom argument in the send grid email. When the email is sent, I grab the sg_message_id and store that in the conversation. Also, when creating the email, I create a "Message-ID" (i.e. 1233-1233-1233-1421@mydomain.com).

When the person responds, I use inbound parse and I look for In-Reply-To and/or References header to figure out what conversation this belongs to.

When I reply back, I use the "Message-ID" from the recipients reply and that as In-Reply-To. I then find all previous messages and loop over them and add them to the References header.

In addition, I ensure the subject is the same. (i.e. Re: Some conversation)

Now this seems to be correct, but the struggle with a clean implementation of keeping the "conversation" included in an email, like what you'd typically see when you reply back and forth.

Adding content like this does not seem to work:

message.AddContent("text/html", newMessage);
I do a foreach...
message.AddContent("text/html", oldmessage);

It seems I have to pack it all into htmlContent, and I'm also having to simulate my own "John Doe wrote on MM/dd/yyyy:

Like this:

var content =
          "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body style=\"word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;\">" +
          data.Message + "<br/><br/></body></html>";

        if (!string.IsNullOrEmpty(previousMessage))
        {
          content +=
            "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body style=\"word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;\">" +
            "On " + previousDate + ", " + previousFrom + " wrote:<br><br></body></html>";
          content += previousMessage;
        }

        message.HtmlContent = content;

It just seems like there should be a cleaner way to do this. I found an actual project that had an example of "keeping a conversation going" and it simply didn't work. All emails came in as a new email:

https://stackoverflow.com/questions/35521459/send-email-as-reply-to-thread

Is there a newer example that someone could share or am I implementing this the way it should be? I feel like I'm potentially missing a minor detail or two.

Thank you!

Technical details:

  • sendgrid-csharp Version: 9.11.0
  • Platform (ASP.NET)
  • .NET Version: 4.6
hard help wanted help wanted community enhancement up for grabs up-for-grabs

Most helpful comment

@thinkingserious, I think if you added a prop, like List<SendGridMessage> PreviousMessages, perhaps with a method like AddPreviousMessages(List<SendGridMessage> messages, autoReference = false) that handled propagating the html and plain content appropriately, as well as iterating over the messages and adding populating the references, as well as using the latest as the ReplyTo, that would be helpful. But I can certainly do that on my own... it just took time figuring out what, or how the proper way to handle it would be. So in this case, at an SDK level it would just be beneficial to save time figuring out how to handle the previous messages.

My issues arose more out of confusion and lack of clear examples I think. The examples that you linked, (same in my original post) just didn't seem to work. The concept or primary question is, how can we make our code interact back and forth in the same way that an email client would, properly including the previous messages with the indentions, and referencing everything appropriately.

Like I stated in my example, I had to add my own "On MM/dd/yyyy hh:mm t, John Doe wrote:"

There was also difficulty getting the addcontent to behave. I wasn't able to just add content, and then add another content. I had to concatenate all my content and then add it all at once as HtmlContent.

Not sure if I did something wrong or if others ran into the same issue or not.

Hope this helps/makes sense. If there was a super clear guide from SendGrid on creating a back and forth email client like conversation thread, that would be awesome.

All 6 comments

Hello @patrickryan79,

Thanks for alerting us to this issue and for adding such detail!

While I don't believe this is a SDK issue, I will leave this open so that our community may assist and if enough people +1 or add comments it will rise higher on our own backlog.

That said, here are some threads that may help in the meantime:
https://github.com/sendgrid/sendgrid-nodejs/issues/690
https://stackoverflow.com/questions/35521459/send-email-as-reply-to-thread

With Best Regards,

Elmer

Thanks @thinkingserious .. The SO link you sent was one that I looked at. I did look at the other one as well. I wouldn't have even used GH Issues if support didn't send me this way. Hopefully someone can provide some insight. Perhaps my implementation is correct after all. I just feel like it could be refactored or handled a bit differently.

Hi @patrickryan79,

Do you think there is something we can do at the SDK level to make this cleaner? Perhaps we can work with the community to create helpers that make this process smoother.

With Best Regards,

Elmer

@thinkingserious, I think if you added a prop, like List<SendGridMessage> PreviousMessages, perhaps with a method like AddPreviousMessages(List<SendGridMessage> messages, autoReference = false) that handled propagating the html and plain content appropriately, as well as iterating over the messages and adding populating the references, as well as using the latest as the ReplyTo, that would be helpful. But I can certainly do that on my own... it just took time figuring out what, or how the proper way to handle it would be. So in this case, at an SDK level it would just be beneficial to save time figuring out how to handle the previous messages.

My issues arose more out of confusion and lack of clear examples I think. The examples that you linked, (same in my original post) just didn't seem to work. The concept or primary question is, how can we make our code interact back and forth in the same way that an email client would, properly including the previous messages with the indentions, and referencing everything appropriately.

Like I stated in my example, I had to add my own "On MM/dd/yyyy hh:mm t, John Doe wrote:"

There was also difficulty getting the addcontent to behave. I wasn't able to just add content, and then add another content. I had to concatenate all my content and then add it all at once as HtmlContent.

Not sure if I did something wrong or if others ran into the same issue or not.

Hope this helps/makes sense. If there was a super clear guide from SendGrid on creating a back and forth email client like conversation thread, that would be awesome.

Thats great feedback @patrickryan79, thanks for taking the time to write that up!

I will keep this issue open and add to our backlog.

Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!

Was this page helpful?
0 / 5 - 0 ratings