Creating Email Threads

[This article was first published on R - datawookie, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The ability to specify a message ID in emails sent from the {emayili} package makes it possible to create email threads.

Create a Server

First set up the SMTP server details. Credentials are stored in environment variables.

library(emayili)

GMAIL_USERNAME = Sys.getenv("GMAIL_USERNAME")
GMAIL_PASSWORD = Sys.getenv("GMAIL_PASSWORD")

smtp <- gmail(
  username = GMAIL_USERNAME,
  password = GMAIL_PASSWORD
)

Create a Message ID

Create a suitably unique message ID.

id <- c(letters, 0:9) %>%
  sample(size = 24, replace = TRUE) %>%
  paste0(collapse = "") %>%
  paste0("@mx.google.com")

print(paste("Message ID:", id))

[email protected]

Send a Message with an ID

Now create a message specifying that message ID. Set the sender and receiver to the same email address so that we’ll be able to see the thread in a single inbox.

msg <- envelope(
  to      = GMAIL_USERNAME %>% address(display = "Receiver"),
  from    = GMAIL_USERNAME %>% address(display = "Sender"),
  subject = "Hello!",
  id = id,
  # importance = "high",
  priority = "urgent"
) %>%
  return_path(GMAIL_USERNAME %>% address(display = "Sender"))

Send the message.

smtp(msg, verbose = TRUE)

This is what the message source looks like in my email client:

Date: Fri, 21 Jun 2024 22:07:56 -0700 (PDT)
X-Google-Original-Date: Sat, 22 Jun 2024 05:07:56 GMT
X-Mailer:                     {emayili}-0.8.0
MIME-Version:                 1.0
To:                           Receiver
From:                         Sender
Subject:                      Hello!
Message-ID:                   <[email protected]>
Priority:                     urgent
Return-Path:                  Sender

Notice that the message ID we specified is included as the Message-ID header.

Reply on Same Thread

Next we’ll respond to that message. You can again specify the message ID for the response, but since we won’t be proceeding any further with this thread we’ll just let the SMTP server generate a message ID. However, we do specify the message ID from the original message in the

msg <- envelope(
  to      = GMAIL_USERNAME %>% address(display = "Sender"),
  from    = GMAIL_USERNAME %>% address(display = "Receiver"),
  subject = "Hello! (reply)"
) %>%
  inreplyto(id) %>%
  references(id)

Send the reply.

smtp(msg, verbose = TRUE)

This is the source for the reply in my email client:

Message-ID: <[email protected]>
Date: Fri, 21 Jun 2024 22:08:56 -0700 (PDT)
X-Google-Original-Date: Sat, 22 Jun 2024 05:08:56 GMT
X-Mailer:                     {emayili}-0.8.0
MIME-Version:                 1.0
To:                           Sender
From:                         Receiver
Subject:                      Re: Re: Hello! (reply)
Return-Path:                  Receiver
In-Reply-To:                  <[email protected]>
References:                   <[email protected]>

Notice the generated message ID but that the ID of the original message is included in both the In-Reply-To and References header fields.

Thread in Email Client

Finally, check that the thread is recognised by email client.

Yes indeed, Thunderbird recognises that the original message and reply are related and renders them as a thread.

To leave a comment for the author, please follow the link and comment on their blog: R - datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)