Template Helper Functions
Postchi provides powerful template helper functions for encoding, hashing, and transforming data within your email templates. These helpers enable secure personalization, GDPR-compliant tracking, and seamless third-party integrations.
Table of Contents
Basic Variables
Before diving into helpers, remember that Postchi supports simple variable replacement:
Available contact variables:
{{email}}or{{contact.email}}- Contact email address{{firstName}}or{{contact.firstName}}- Contact first name{{lastName}}or{{contact.lastName}}- Contact last name{{customFieldName}}or{{contact.customFieldName}}- Any custom field
Helper Functions
Hash
Generate cryptographic hashes for secure, anonymized identifiers.
Supported Algorithms: md5, sha1, sha256, sha512
Syntax:
Examples:
Use Cases:
- GDPR-compliant analytics tracking
- Anonymized user identifiers
- Privacy-preserving third-party integrations
HMAC
Generate Hash-based Message Authentication Codes (HMAC) for secure tokens requiring authentication.
Supported Algorithms: sha1, sha256, sha512
Syntax:
Examples:
Use Cases:
- Secure authentication tokens
- Signed unsubscribe links
- Webhook payload verification
- API request signing
Security Note: HMAC is more secure than plain hashing because it requires a secret key, preventing rainbow table attacks.
Base64
Encode and decode data using Base64 encoding.
Encode Syntax:
Decode Syntax:
Examples:
Use Cases:
- Encoding data in URL parameters
- Obfuscating email addresses in links
- Binary data transmission
URL Encoding
Encode and decode strings for safe use in URLs.
Encode Syntax:
Decode Syntax:
Examples:
Use Cases:
- Passing data in URL query parameters
- Email tracking links
- Redirect URLs with parameters
JSON
Convert variables to JSON format, optionally with URL encoding.
JSON Syntax:
URL-Encoded JSON Syntax:
Examples:
Use Cases:
- Passing structured data to webhooks
- Embedding contact data in redirect URLs
- JavaScript data initialization
Real-World Use Cases
1. Privacy-Preserving Analytics Tracking
Hash email addresses before sending to analytics services:
<!-- Google Analytics tracking pixel with hashed user ID -->
<img src="https://www.google-analytics.com/collect?v=1&tid=UA-XXXXX&cid={{#hash "sha256"}}{{contact.email}}{{/hash}}&t=event" />
<!-- Custom analytics with anonymized identifier -->
<a href="https://yoursite.com/page?user_id={{#hash "sha256"}}{{contact.email}}{{/hash}}">
Visit Our Site
</a>
2. Secure Unsubscribe Links
Create authenticated unsubscribe links:
<!-- HMAC-secured unsubscribe link -->
<a href="https://yoursite.com/unsubscribe?email={{#base64}}{{contact.email}}{{/base64}}&token={{#hmac "sha256" "unsubscribe-secret"}}{{contact.email}}{{/hmac}}">
Unsubscribe
</a>
3. Third-Party Integration with Contact Data
Pass contact information to external services:
<!-- Webhook callback with contact data -->
<a href="https://partner.com/callback?data={{#toUrlEncodedJson}}{{contact}}{{/toUrlEncodedJson}}">
Complete Registration
</a>
<!-- CRM sync with Base64-encoded email -->
<img src="https://crm.example.com/sync?id={{#base64}}{{contact.email}}{{/base64}}" />
4. Personalized Tracking Links
Combine multiple helpers for complex use cases:
<!-- Multi-factor tracking link -->
<a href="https://yoursite.com/click?campaign=summer2024&user={{#hash "md5"}}{{contact.email}}{{/hash}}&ref={{#urlEncode}}{{campaign.name}}{{/urlEncode}}">
Click Here
</a>
5. GDPR-Compliant Facebook Pixel
Hash PII before sending to Facebook:
<script>
fbq('track', 'PageView', {
em: '{{#hash "sha256"}}{{contact.email}}{{/hash}}',
fn: '{{#hash "sha256"}}{{contact.firstName}}{{/hash}}',
ln: '{{#hash "sha256"}}{{contact.lastName}}{{/hash}}'
});
</script>
6. Nested Helpers for Complex Transformations
Chain helpers for advanced use cases:
Security Best Practices
1. Choose the Right Hash Algorithm
- SHA-256: Best balance of security and performance (recommended)
- SHA-512: Maximum security for sensitive data
- MD5: Fast but cryptographically weak (only use for non-security purposes)
- SHA-1: Deprecated, avoid for security-critical applications
2. Use HMAC for Authentication
When you need to verify data authenticity:
3. Protect Secret Keys
- Never hardcode secrets in templates shared with customers
- Rotate HMAC secrets periodically
- Use different secrets for different purposes (unsubscribe, webhooks, etc.)
4. GDPR Compliance
When sending data to third parties:
5. URL Encoding for Special Characters
Always URL encode when passing data in URLs:
Template Testing
You can test templates with helper functions using the "Test Email" feature in the Postchi dashboard:
- Navigate to Templates → Your Template → Test Email
- Enter test contact data (email, firstName, lastName, custom fields)
- Send test email to verify helper outputs
Example Test Variables:
{
"email": "test@example.com",
"firstName": "John",
"lastName": "Doe"
}
API Integration
When sending emails via the API, variables are automatically processed with helpers:
curl -X POST https://api.postchi.io/v1/emails/send \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "recipient@example.com",
"templateId": "your-template-id",
"variables": {
"firstName": "John",
"campaign": {
"name": "Summer Sale 2024"
}
}
}'
The template content will automatically process all helper functions with the provided variables.
Performance Considerations
Hash Performance
From fastest to slowest:
- MD5 - ~10x faster than SHA-256 (but less secure)
- SHA-256 - Good balance (recommended)
- SHA-512 - Slower but more secure
Caching Strategy
Postchi automatically caches processed templates. Helper functions are re-evaluated for each email with contact-specific data.
Best Practices
- Use simpler hashes (MD5) for non-security purposes
- Minimize nested helpers when possible
- Cache helper results in your application if calling API frequently
Support
For questions or issues with template helpers:
- Documentation: https://docs.postchi.io
- Support: support@postchi.io
- GitHub Issues: https://github.com/studioaccolade/postchi/issues
Changelog
v1.0.0 - Initial release
- Hash helpers (md5, sha1, sha256, sha512)
- HMAC helpers (sha1, sha256, sha512)
- Base64 encoding/decoding
- URL encoding/decoding
- JSON conversion
- URL-encoded JSON conversion