Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email as reply to thread

I am using sendgrid to send emails when users are offline and cannot chat in realtime

Problem that right now, when I send an email, it is always creating a new 'thread' in the recipient's email. I want to have a conversation thread

I am using the Send HTTP endpoint https://sendgrid.com/docs/API_Reference/Web_API/mail.html

Any ideas?

like image 805
Tarlen Avatar asked Feb 20 '16 09:02

Tarlen


People also ask

How do I reply to a specific email in a thread?

Tap the 3 dots icon on the top right of the message. You will see a set of actions (Reply, Reply All, Forward, Delete) that you can perform on this message come up from the bottom.

Can I forward an email to an email thread?

Another way to insert emails in a new thread is to select the emails and then from the overflow (three-dot) menu select “Forward as attachment”. To reply to an existing thread, you can pop-out the compose window and then drag and drop emails to it.


Video Answer


1 Answers

Try the following:

First, I assume you are tracking the conversation(s) somehow using a unique conversation id? If not, start doing this.

You'll want to send in custom headers: Message-ID, In-Reply-To, and References.

Below is some sample code using C# and the 6.3.4 SendGrid nuGet Package:

using System;
using System.Net;
using System.Net.Mail;
using System.Threading;
using SendGrid;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var conversationId = Guid.NewGuid().ToString(); // TO DO: get the real conversation ID from dtaabase

            var testConversationEmailsToSend = 7;

            for (var i = 1; i <= testConversationEmailsToSend; i++)
            {
                var emailNumberForConversation = GetConversationEmailCount(i);
                var messageId = string.Format("{0}-{1}@yourdomain.com", conversationId, emailNumberForConversation);
                var previousMessageId = GetPreviousMessaageId(conversationId, emailNumberForConversation);

                var msg = new SendGridMessage();

                msg.Headers.Add("Message-ID", messageId);
                msg.Headers.Add("In-Reply-To", string.Format("<{0}>", previousMessageId));
                SetReferences(msg, conversationId, emailNumberForConversation);

                msg.AddTo("[email protected]");
                msg.From = new MailAddress("[email protected]");

                msg.Subject = "continuing the conversation";
                msg.Text = string.Format("{0} messaage #{1}", msg.Subject, i);

                var web = new Web(new NetworkCredential("sendgridusername", "sendgridpassword"));
                var task = web.DeliverAsync(msg);

                task.Wait(new TimeSpan(0, 0, 15));

                // sleep 1 minute before sending next email... for testing sample code
                Thread.Sleep(new TimeSpan(0, 0, 15));
            }
        }

        private static object GetPreviousMessaageId(string conversationId, int emailNumberForConversation)
        {
            var previousMessageCount = Math.Max(emailNumberForConversation - 1, 1);

            return string.Format("{0}-{1}@yourdomain.com", conversationId, previousMessageCount);
        }

        private static int GetConversationEmailCount(int dumbyParameterForSampleCode)
        {
            // TO DO: look in database to see how many of these messaages our system has sent.
            // if this is the first email for the conversation we'll return 1;

            return dumbyParameterForSampleCode; // hard setting value only for example code purposes
        }

        private static void SetReferences(SendGridMessage msg, string conversationId, int emailCount)
        {
            var referencesValue = "";

            for (var i = emailCount - 1; i > 0; i--)
            {
                referencesValue += string.Format("<{0}-{1}@yourdomain.com>{2}", conversationId, i, Environment.NewLine);
            }

            msg.Headers.Add("References", referencesValue);
        }
    }
}
like image 126
Justin Steele Avatar answered Nov 14 '22 04:11

Justin Steele