

Sending Threaded Emails Using Rails ActionMailer
source link: https://flexport.engineering/sending-threaded-emails-using-rails-actionmailer-8549bd246283
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sending Threaded Emails Using Rails ActionMailer


Introduction
At Flexport, we communicate with our customers — and their software — in many ways. API and EDI integrations are ideal because they allow structured data to flow seamlessly with no humans involved. Sometimes, however, emails are necessary to facilitate coordination.
To save our operations teams’ time, we send certain emails automatically using Rails ActionMailer. While the approach works great in general, we’ve had to overcome a few challenges related to email threading. This post outlines the problem and how we solved it.
One benefit of manual, conversation-based emails is that replies create threaded email conversations. Most email clients thread messages to make them easier to follow. Some Flexport customers reported that their inboxes were being filled by automated emails that weren’t being threaded properly. To make sure subsequent updates for a given request were not cluttering ourusers’ inboxes, we needed our automated emails to appear grouped together, just like a hand-typed email would.
To understand how to recreate this behavior programmatically, let’s look at how replies work in the mail client.
How Email Threading Works
Before an email client sends an email, it generates a uniqueheader called a Message-ID that is added to the message. Message-IDs are supposed to be unique, and have a specified format as defined in the Internet Message Format standards. A Message-ID is used to reference an individual email across the client.
When a user clicks ‘Reply’ to an email in Gmail, the reply has its own Message-ID generated, distinct from the original. A new header is also added to the reply, called References, and its value is the Message-ID from the original email. This is fairly self explanatory: this header tells the email client that this email “references” the one it is replying to.
Every additional reply will append another reference to this header, corresponding to all of the entries in the thread. However, even if References only contains the Message-ID of the original message, most email clients will thread all the messages with that References value together.
Additionally, email clients will add the “Re:” prefix to messages, to visually signal that they are “Regarding” a previous message. In Gmail, this subject only appears in the inbox view, and not in the email thread. This can be confirmed by clicking ‘More’ next to the ‘Reply’ button and clicking ‘Show Original’.
While Google’s Gmail “magic” may sometimes group emails together with one or neither of the above techniques, using both can guarantee that they will be visually threaded together.
Using ActionMailer
Based on this specification for Message-ID, it is actually fairly straightforward to send programmatic email replies:
- Set the References header of the reply to the Message-ID of the original.
- Change the subject of the reply to “Re: <original subject>”
Let’s take a look at how to accomplish this using Ruby’s ActionMailer. Below is a simple code example MoonLandingService that automatically sends emails from a lunar expedition back to earth:
Mailer Class
Mailer Service
The send_landing_updates method first creates and sends an email to start the thread. Then, the generated Message-ID header of the message is accessed using .message_id. This can be stored in email logs for further use, or as additional metadata inside a more complex model.
Then the user can create the reply and set the references header to the Message-ID of the first. It is important to note that it is surrounded by the < and > symbol: the .message-id call will return the Message-ID without these symbols, but they are needed to properly format the header.
And that’s it! Following this pattern will result in a string of messages like this:
Other Things to Consider
The above example highlights a simple usage of the References header for email threading. In reality, these threads can be complex, with multiple users replying to different emails in the thread. Additionally, replying to an email that was not initially sent programmatically requires retrieving the Message-ID of that email from the web server (or some other technique). Wes Morgan’s blog post Understanding Email Headers goes into more details about how these headers work.
I hope this helps anyone working on automating email infrastructure. Feedback or questions welcome!
Recommend
-
44
You probably found this tutorial because you want to send emails using Python. Perhaps you want to receive email reminders from your code, send a confirmation email to users when they create an account, or send emails to...
-
5
MFMailComposeViewController provides an easy way to present a view controller, that allows to write, edit and send an email. It’s presented modally, so the context of the app won’t be left. Hint: This post is using Swift 4 and iOS...
-
11
ActionMailer, Vlad and Rake Remote Task Issues Sep 5, 2014 We recently had a situation where adding a UserMailer to a new Rails 4 app (really a tiny one) led to conflicts with "domain" and Rake Remote Task in...
-
9
Monitoring your application can be a difficult task if you’re just getting started with it. You need to know which metrics matter most and understand which ones you can combine into meaningful graphs. In the (not just holiday ⛄️) spiri...
-
8
MFMailComposeViewController – sending emails from an iOS app MFMailComposeViewController provides an easy...
-
26
Sending Emails using PythonSending Emails using PythonPython is one of the most preferred programming languages to automate tasks and an interesting example of this is sending emails using Python language. Pyt...
-
16
Liam Cleary [MVP and MCT] Architecture, Development, Security, Hacking and anything that I deem as important...
-
14
A Python 3.6 feature that has flown under the radar is the revamped email package. In this post we will e...
-
5
Sending Reminder & Follow Up Emails For Events Using Real-time Marketing 2022-07-05 Categories:
-
11
Sending Emails from Your React App Using EmailJS
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK