Designing Piisend Templates with Merge Variables
Leverage Piisend's email templates and merge variables to send dynamic, personalized transactional emails. Learn best practices for template design, variable naming, and API integration for efficient email delivery.
Section 1
The Power of Piisend Email Templates and Merge Variables
Piisend's email templates provide a robust way to manage your transactional email designs, ensuring consistency and brand identity across all communications. By integrating merge variables, you can inject dynamic content into these reusable layouts, personalizing each email without altering the core template structure. This approach is crucial for developers building scalable applications that require sending personalized messages like order confirmations, password resets, or welcome emails.
Section 2
Crafting Your HTML Email Template with Placeholders
Designing effective HTML email templates involves creating a clean, responsive layout and strategically placing placeholders for your dynamic content. Piisend templates support a simple syntax for merge variables, typically using double curly braces (e.g., {{name}}). This separation of concerns allows your marketing or design team to manage the visual aspects of the email, while your development team focuses on providing the necessary data through the email API for developers.
Section 3
Sending Dynamic Emails with the Piisend API
Once your template is uploaded to Piisend, sending an email with dynamic content is straightforward. You'll reference the template by its ID and pass an object of key-value pairs for your merge variables in the API request body. The Piisend API will automatically populate the placeholders in your template with the provided data, generating a fully personalized email ready for delivery.
1curl -sS -X POST https://api.piisend.com/api/v1/emails \2 -H "Authorization: Bearer YOUR_API_KEY" \3 -H "Content-Type: application/json" \4 -d '{5 "to": ["recipient@example.com"],6 "template_id": "YOUR_TEMPLATE_OBJECT_ID",7 "template_vars": {8 "name": "Alice",9 "order_id": "PII-2023-12345",10 "item_count": 3,11 "total_amount": "49.99 USD"12 }13 }'Section 4
Best Practices for Variable Naming and Data Structure
To maintain clarity and prevent errors, adopt consistent naming conventions for your merge variables. Use descriptive, lowercase, snake_case names (e.g., user_name, order_total). For complex dynamic content, you can pass nested JSON objects as merge variables, allowing for more intricate data structures within your reusable layouts. This approach helps in separating layout logic from the actual content.
Section 5
Integrating Dynamic Emails with JavaScript
For applications built with JavaScript, sending emails with merge variables is equally simple. You can use any HTTP client library to make a POST request to the Piisend API, structuring your payload with the template ID and merge variables object. This allows for seamless integration of dynamic content into your web or backend applications, ensuring your users receive personalized and timely communications.
1const res = await fetch('https://api.piisend.com/api/v1/emails', {2 method: 'POST',3 headers: {4 Authorization: `Bearer ${process.env.PIISEND_API_KEY}`,5 'Content-Type': 'application/json',6 },7 body: JSON.stringify({8 to: ['recipient@example.com'],9 template_id: 'YOUR_TEMPLATE_OBJECT_ID',10 template_vars: {11 user_name: 'Bob',12 account_link: 'https://app.example.com/dashboard',13 },14 }),15});16 17if (!res.ok) {18 throw new Error(await res.text());19}20 21const data = await res.json();22console.log('Queued:', data.id);