Proper way to quote previous email content in ActionMailer auto-replies via Mailgun

I’m working on an email system that uses Mailgun to receive incoming messages, and I want to send automatic replies back to senders. Right now I have the basic threading working by setting the In-Reply-To and References headers in my response emails, which helps most email clients group the messages together properly.

However, I’m running into issues with some mobile email apps like the default iPhone Mail client. These apps don’t always handle email threading very well, so users can’t easily see the conversation flow. I want to add the original message content below my auto-reply, properly formatted with the typical email quoting style (like with > symbols at the start of each line).

What’s the best approach to format and include the original email content in my ActionMailer responses? I need to make sure it displays correctly across different email clients and follows standard email reply formatting conventions.

I’ve run into this threading mess with different mail clients too. You need to handle both HTML and plain text properly. For HTML, wrap quoted content in <blockquote> tags with decent styling. For plain text, just prefix each line with > like you mentioned. What really helped me was adding a header line before the quoted stuff - “On [date], [sender] wrote:” - makes things way clearer on mobile where threading breaks. Watch out for line wrapping though. Some email content has crazy long lines that you need to wrap before adding quote prefixes, or you’ll get wonky formatting in Outlook and other clients.

Nested quote levels destroyed me. When someone forwards an email that’s already quoted, you get multiple > prefixes that don’t line up. Some lines end up with > > > while others are completely off. I had to build logic that counts prefixes on each line and normalizes them before adding the new quote level.

Signature blocks are another pain. Most clients add signatures you don’t want to quote in full. I parse for separators like -- or Sent from my iPhone and either cut there or add [signature trimmed] to keep things clean.

Here’s what I learned dealing with this stuff - multipart emails are tricky. Mailgun sends emails with both HTML and plain text parts, so you’ve got to handle each one separately when adding quotes. For plain text, strip out any existing quote markers first (I use gsub(/^>\s*/, '')) then add your own. Watch out for encoding issues too - different character sets can mess up your display with weird characters. Another gotcha: inline images and attachments will break your quoting logic, so sanitize everything first. Test with Gmail, Outlook, and Apple Mail since they all handle quoted content differently.

I found that mail.text_part.decoded works way better than .body.to_s - it keeps the original formatting and handles charset conversion properly. Just heads up, quoted-printable encoding will screw up your line breaks when you add quote markers.

try using mail.body.to_s.gsub(/^/, '> ') to add the quotes. it works well in my Rails setup with ActionMailer. just make sure to strip HTML tags first if you’re handling HTML emails, otherwise it becomes a bit messy.

Email formatting is one of those headaches that gets worse the more you try to handle edge cases manually. I wasted so much time debugging quote formatting and threading issues across different clients.

The real problem isn’t just formatting - it’s maintaining logic for different email types, handling encodings, managing threading headers, and keeping everything synced. Every time Mailgun changes something or a new client drops, you’re debugging again.

Automating the entire email processing pipeline solved this for me. Instead of writing custom ActionMailer code for every scenario, I built a workflow that handles incoming emails, extracts content properly, formats quotes automatically, and sends responses with correct headers.

The workflow handles HTML and plain text, strips problematic content, wraps long lines, and adds proper quote formatting - no separate code paths to maintain. When new edge cases show up, I just tweak the automation rules instead of diving back into Rails.

This makes it easy to add features like delayed responses, sentiment analysis on incoming emails, or routing to different templates based on content.

Latenode makes building these email workflows straightforward since it connects directly with Mailgun and handles formatting logic visually.

That threading header approach is spot on. What saved me hours of debugging? Adding a content sanitization layer before the quote prefixes. Mailgun’s raw email content is a mess - mixed line endings and random whitespace that breaks quote formatting. I run content.gsub(/\r\n|\r/, "\n").strip first, then handle quoting. Also watch out for reply separators. Email clients add those “— Original Message —” dividers, and you want to keep them in your quoted content. Don’t strip everything or you’ll lose the context markers that help users follow conversations.