Email Templates
Define email patterns with placeholders to verify structure and extract dynamic data from your application's emails. Templates make integration tests more reliable and maintainable.
What are Email Templates?
{{UserName}}
to mark dynamic content. When you verify an email against a template, DevInbox automatically
extracts these values for assertion in your tests.
Verify Structure
Ensure emails follow the expected format and contain all required elements in the correct order.
Extract Data
Pull out dynamic values like verification codes, user names, or order numbers for validation.
Maintainable Tests
Write cleaner tests that focus on the data that matters, not fragile string matching.
How Templates Work
Template Syntax
Templates use double curly braces {{}}
to define placeholders for dynamic content. The parser extracts values that match these placeholders
when verifying an email.
Subject: Welcome to {{CompanyName}}, {{UserName}}! Body: Hello {{UserName}}, Welcome to {{CompanyName}}! Your verification code is: {{VerificationCode}} Best regards, The {{CompanyName}} Team
Matching Process
When you verify an email against a template, DevInbox's template parser:
- Converts the template pattern into a regular expression
- Matches the email content against this pattern
- Extracts values from the placeholders
- Returns extracted data as key-value pairs
Example: Before and After
Template Pattern
Welcome {{UserName}}! Code: {{Code}}Actual Email
Welcome John Doe! Code: ABC123
Extracted:
UserName = "John Doe"
Code = "ABC123"
Sample Templates
User Welcome Email
A common template for welcome emails sent during user registration.
Template Name: user-welcome Subject Pattern: Welcome to {{CompanyName}}, {{UserName}}! Body Pattern: Hello {{UserName}}, Welcome to {{CompanyName}}! Your account has been created successfully. Verification Code: {{VerificationCode}} Best regards, The {{CompanyName}} Team
Password Reset Email
Template for password reset requests with time-sensitive links.
Template Name: password-reset Subject Pattern: Password Reset Request for {{UserName}} Body Pattern: Hello {{UserName}}, You requested a password reset. Click the link below: {{ResetLink}} This link expires in {{ExpiryHours}} hours.
Order Confirmation Email
Template for e-commerce order confirmations with order details.
Template Name: order-confirmation Subject Pattern: Order #{{OrderNumber}} Confirmation Body Pattern: Thank you for your order, {{CustomerName}}! Order Number: {{OrderNumber}} Total: ${{Total}} We'll send you tracking information once shipped.
Using Templates in Integration Tests
Template API Endpoints
Use the endpoint /messages/{'{key}'}/{'{template}'}/single
to retrieve and parse the most recent email matching your template. The API returns extracted values
ready for assertions in your tests.
import requests def test_welcome_email_with_template(): API_KEY = "your_api_key" headers = {"X-Api-Key": API_KEY} # Create mailbox r = requests.post( "https://api.devinbox.io/mailboxes", headers=headers, json={}) mailbox = r.json() test_email = f"{mailbox['key']}@devinbox.io" # Trigger registration user_service.register( email=test_email, name="Jane Smith", company="Acme Corp") # Get parsed email using template r = requests.get( f"https://api.devinbox.io/messages/" f"{mailbox['key']}/user-welcome/single", headers=headers) parsed = r.json() # Extract and verify template values assert parsed['subject']['UserName'] == "Jane Smith" assert parsed['subject']['CompanyName'] == "Acme Corp" assert parsed['body']['VerificationCode'] is not None assert len(parsed['body']['VerificationCode']) == 6
Best Practices
Keep Templates Simple
Start with basic templates that capture essential fields. Avoid overly complex patterns that might break with minor formatting changes in your emails.
Use Descriptive Variable Names
Choose clear variable names that match your application's domain. Use
{{VerificationCode}} instead of
{{Code}} for clarity.
Test Template Matching First
Before using templates in your tests, verify they correctly match real emails from your application using the DevInbox dashboard or API.
Handle Whitespace Carefully
The template parser normalizes line endings but respects spacing. Be mindful of whitespace in your patterns, especially around placeholders.
Store Templates in Version Control
Keep your template definitions in your repository alongside your tests. This ensures templates evolve with your application and are accessible to all team members.
Advanced Tips
Multi-Line Content
Templates support multi-line patterns. The parser normalizes line endings, so your templates work consistently across different email formats.
Hello {{Name}},\n\nYour code is {{Code}}Non-Greedy Matching
The template parser uses non-greedy matching for placeholders. If you have multiple placeholders in sequence, each captures the minimum text needed to satisfy the pattern.
Template Versioning
When your email format changes, create new template versions rather than updating existing ones. This prevents old tests from breaking unexpectedly.