How to detect email responses in Gmail with Google Apps Script

I’m working on a project where I need to monitor email replies automatically. I have a Google Sheets document that I use to keep track of outgoing emails and whether people have responded to them. I want to know if there’s a method using Google Apps Script to check when someone sends a reply to my original message. This would help me update my spreadsheet automatically instead of checking manually. Has anyone done something similar before? I’m looking for a way to identify reply messages and match them to the original emails I sent out.

I built something like this last year for tracking client emails. Here’s what worked for me: Use GmailApp.getThreadById() with thread message counts. When you send an email, save the thread ID in your spreadsheet next to the message details. Set up a time trigger to check each stored thread regularly. If thread.getMessageCount() is over 1, you’ve got replies. You can use thread.getMessages() to grab all thread messages and filter by sender - that way you’ll catch actual replies instead of your own follow-ups. Watch out for forwarded emails though. They spawn new threads so you might miss those. Also, use Gmail labels to mark processed threads. Your script will run way faster that way.

After months of struggling with this, I figured out the trick: use Gmail’s Reference and Message-ID headers instead of counting threads. Here’s what works: when you send the original email, grab both the Message-ID from getMessage().getId() and the thread ID, then store them in your sheet. To catch replies, check the In-Reply-To header with getMessage().getHeader('In-Reply-To') - it’ll match your original Message-ID for real replies. This beats message counting, especially with auto-replies or forwarded emails. Pro tip: filter out messages where the sender is your own email so you don’t count your responses. The Reference header helps track longer conversations too.

gmail search queries with GmailApp.search() work best for this. Search for "re: your subject line" from:[email protected] to catch most replies without messing with headers or thread counting. gr8 for basic tracking, but u’ll miss replies where pple change the subject line.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.