Custom parsing rules for emails with inconsistent formats work when you write one small rule set per layout, anchor each rule on text that rarely changes, and send anything that matches no layout to a review list instead of a blank cell. This guide shows how to build those rules for the Gmail emails an online store or a bookkeeping practice receives every day, and how to keep them working when a sender changes its template.
Why do the same emails break parsing rules?
Rules break because a sender’s wording, currency format or HTML layout changes while your rule still expects the old one. One developer’s write-up of parsing order emails with regex shows the pattern: a rule for “Total: $29” worked on the first email, failed on “Amount paid: $29”, failed again on “Total price: USD 29”, and then met an HTML email built from nested tables.
Those three failures are the whole problem in miniature:
- Label drift: the same value sits behind “Total”, “Amount paid” or “Order total”.
- Format drift: “$1,249.00”, “USD 1249” and “1.249,00 EUR” are the same number.
- Layout drift: a sender redesigns its template, or two systems send the same kind of email (a store’s own confirmation and a marketplace’s).
A single catch-all rule that tries to cover everything gets longer with each exception and still misses the next one. Several narrow rules, each tied to one layout, are easier to read and easier to fix.
What is the best way to create email parsing rules?
Start from a sample of real emails, group them by layout, and write one rule set for each group. Do the grouping by hand before you touch any tool.
- Collect 20 to 30 recent emails of the type you want to parse, across every sender that produces it. Include the odd ones: refunds, partial payments, emails with a discount line.
- Group them by layout. Two emails belong together when the fields you need appear next to the same labels in the same order. A store’s “Order confirmed” email and a marketplace’s “You made a sale” email are two groups even though both carry an order number.
- List the fields you need per group. Keep the list short. Every field is a rule you maintain, and plans cap how many fields one saved search can extract (see the limits below).
- For each field, pick the most stable anchor. The anchor is the text around the value that is least likely to change: a label such as “Order number”, a currency symbol, a line that always follows the value.
- Write the rule, test it on every email in the group, and note the misses. A rule that works on 18 of 20 emails has 2 emails that tell you something about the layout.
Text-before and text-after rules
A text-before/text-after rule takes whatever sits between two fixed pieces of text, which makes it the right first choice for labelled values. To extract an order number from a line like “Order #10482 confirmed”, you set the text before to “Order #” and the text after to ” confirmed”. It is easy to read months later, and when it breaks you can see why.
It stops working when the value has no stable text on one side, or when the label changes by sender. That is where a regex filter earns its place.
Regex filters for values that vary in shape
A regex filter describes the shape of a value, so it can cope with label and format differences that a fixed before/after pair cannot. For a total that appears as “Total”, “Amount paid”, “Order total” or “Total price”, with or without a currency marker, a pattern like this covers all three:
(?:Total price|Order total|Amount paid|Total)\s*:?\s*(?:\$|USD)?\s*([\d,]+(?:\.\d{2})?)
Keep the alternatives in order from the longest label to the shortest, so “Total price” is tried before “Total”. Anchor on the label rather than on the number alone, otherwise the rule will happily grab a shipping fee, a tax line or a phone number. Write the rule as narrowly as the sample allows, then widen it only when a real email forces you to.
Multiple parsing contexts for different layouts
When a sender or a template really does differ, give each layout its own context instead of stretching one rule across both. In xtractor, multiple parsing contexts let one search extract data from emails whose format varies, so a single saved search can hold the store’s layout and the marketplace’s layout side by side. The step-by-step multi-context setup walks through real templates.
Worked example: an online store with two order layouts
Here is the setup for a store owner who receives about 150 order and payment emails a day in Gmail, from two sources with different layouts, and wants one Google Sheet for daily cashflow.
Layout A is the store’s own order confirmation: “Order #10482 confirmed” and “Total: $129.00”. Layout B is a payment processor receipt: “Amount paid: USD 129.00” and “Fee: USD 3.99”. Both layouts carry a date, and B carries the fee.
The fields are four: order number, date, total and fee. In a Google Sheet:
| Column | Layout A rule | Layout B rule |
|---|---|---|
| Order number | Text after “Order #”, before ” confirmed” | Text after “Reference: “ |
| Date | Text after “Date: “ | Text after “Paid on: “ |
| Total | Text after “Total: $” | Regex on “Amount paid” with optional “USD” |
| Fee | Leave empty | Text after “Fee: USD “ |
Each layout is one parsing context, so the two sets of anchors never compete. Four fields are more than Starter’s limit of 2 fields per saved search, so this setup needs Pro, which allows 5. At 150 emails a day, the daily volume sits under Pro’s 500 a day, and the month (about 4,500) sits under its 5,000 a month.
Run it once over the last 3 days as a bulk import (about 450 emails, under Pro’s 500 a day), check the rows against the Gmail search, and then switch the import to a daily schedule. A 90-day backfill at this volume is about 13,500 emails, which is more than Pro’s 5,000 a month, so it would need Premium (20,000 a month, 2,000 a day) and about a week of daily limits. Compare the total row count with the number of emails the Gmail search returns, because a gap tells you that some emails matched no layout.
How do you handle emails that match no rule?
Decide in advance what an unmatched email looks like in the sheet, so a missed email shows up as a row you can see instead of a gap you never notice. Three habits do most of the work:
- Count emails against rows. After each import, compare the number of imported emails with the rows you expected. The difference is your unmatched list.
- Make required fields visible when empty. Add a conditional format that highlights a blank order number or total, or a column with
=IF(OR(A2="",C2=""),"CHECK","OK")in the sheet the data lands in. - Add a context for each new layout, not a patch to an old one. When the same unmatched layout appears three times, it is a new sender or a redesigned template. Give it its own context and leave the working ones alone.
xtractor can also use AI to parse emails alongside the filters and contexts you define. Keep your rules for the layouts you know, use AI parsing for the long tail, and check what it returns against your sample before you trust it for money.
How do you keep rules working when senders change templates?
Keep a small test set, re-run it when numbers look wrong, and change one rule at a time. A saved search keeps the rules together so the next import uses the same setup, which is why reusing one saved search beats rebuilding it for each project.
- Save 3 sample emails per layout (the plain case, the odd case, the refund) and re-test them whenever a total looks off.
- Watch the first import after a sender announces a redesign. Check the five newest rows by hand.
- Date your rules. A note in the sheet such as “Layout B rule updated 2 November” tells the next person which version produced older rows.
- Search narrowly. The search that selects the emails should use subject, sender and date, so a rule only ever sees emails of the layout it was written for. The filtering guide covers that side.
What does this cost at your volume?
Plans are priced per user, and what matters here is the cap on fields and saved searches, not only the email count. Prices from xtractor’s plans:
| Plan | Monthly billing | Annual billing | Imports | Saved searches and fields per search |
|---|---|---|---|---|
| Starter | $24 | $5 | 1,000 a month | 2 and 2 |
| Pro | $45 | $10 | 5,000 a month | 5 and 5 |
| Premium | $75 | $20 | 20,000 a month | 10 and 10 |
| Business | $129 | $40 | 100,000 a month | 20 and 20 |
The free plan allows 10 emails a day and 50 a month, with 1 saved search and 1 field per search and no scheduled imports, after a 100-email trial. That is enough to test one rule on one layout but not the example above. The four-field store example needs Pro or higher, because Starter stops at 2 fields per saved search. Scheduled imports run daily on Starter and Pro, every 4 hours on Premium and hourly on Business.
xtractor works with Gmail only and does not parse attachments such as PDFs, so a receipt that only exists as an attached PDF needs another route. Extracted data stays in your own Google Sheet, and you download Excel or CSV from there.
Frequently asked questions
Can one rule parse every email layout?
Rarely. A rule that covers several layouts tends to grow until nobody can read it. One rule set per layout is easier to test and fix.
When should I use regex instead of text before and after?
Use text before and after for labelled values with stable surrounding text. Use regex when the label, currency marker or number format varies.
How many sample emails do I need?
Twenty to thirty is enough to see the main layouts, plus a few edge cases such as refunds.
What happens when a sender changes its template?
A rule tied to the old layout stops matching, and its field may come out empty or wrong, so check the rows. Re-test your saved samples, then add a new context for the new layout.
Can I export the parsed rows to Excel or CSV?
Yes. The data lands in Google Sheets, and you download it as Excel or CSV from there.
Where to go next
If you are still choosing a tool, the comparison of free email parsers shows what each one covers. For the basics, read what email parsing is, and for a ready example on real order emails, see extracting order numbers and amounts into Sheets.
