Sending Test Emails Before You Go Live on Piisend
Ensure your transactional emails are perfect before hitting production. Learn how to send test emails, preview content, and verify webhook delivery using Piisend's staging email API features.
Section 1
Why Testing Your Transactional Emails is Crucial
Before deploying any email-sending functionality to production, thorough testing is essential. This prevents unexpected issues like incorrect content, broken links, or delivery failures from reaching your users. A robust staging email API setup allows developers to preview transactional email content, confirm template rendering, and ensure webhook events are processed correctly without impacting live systems. This proactive approach saves time and maintains a positive user experience.
Section 2
Sending a Basic Test Email to a Staging Recipient
Piisend makes it straightforward to send test emails. You can direct emails to a specific test recipient email address or use a temporary email service during development. This allows you to inspect the email's appearance and content in a real inbox. Remember to use your staging API key for these tests to keep production data clean.
1curl -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": ["test-user@example.com"],6 "subject": "Test Email from Piisend Staging",7 "html": "<p>This is a <strong>test</strong> email from your staging environment.</p>",8 "text": "This is a test email from your staging environment."9 }'Section 3
Previewing HTML Content and Template Variables
When using Piisend's email templates, it's vital to ensure that merge variables are correctly populated and the HTML renders as expected. You can send a preview transactional email by specifying a template_id and providing template_vars. This helps catch any formatting issues or missing data before your users see it.
1import requests2 3api_key = "YOUR_API_KEY"4api_url = "https://api.piisend.com/api/v1/emails"5 6headers = {7 "Authorization": f"Bearer {api_key}",8 "Content-Type": "application/json"9}10 11payload = {12 "to": ["preview@example.com"],13 "subject": "Order Confirmation Preview",14 "template_id": "tpl_your_order_confirmation_template_id",15 "template_vars": {16 "customer_name": "Jane Doe",17 "order_id": "#12345",18 "total_amount": "$99.99"19 }20}21 22response = requests.post(api_url, headers=headers, json=payload)23 24print(response.status_code)25print(response.json())Section 4
Confirming Webhook Delivery and Event Data
If your application relies on webhook delivery events, testing these in a staging environment is critical. Configure your Piisend webhooks to point to your staging endpoint. This allows you to verify that your application receives the events, processes the payload correctly, and that the X-Webhook-Signature and X-Webhook-Timestamp headers are validated. This ensures your system reacts appropriately to bounces, deliveries, and opens.
1{2 "event": "email.delivered",3 "message_id": "msg_1234567890abcdef",4 "recipient": "user@example.com",5 "timestamp": 1678886400,6 "data": {7 "subject": "Your order is confirmed!",8 "tags": ["transactional", "order-confirmation"]9 }10}Section 5
Implementing Idempotency for Reliable Testing
During testing, especially with automated scripts or retries, you might accidentally send the same email multiple times. Piisend's Idempotency-Key header helps prevent this. By including a unique key with each request, Piisend ensures that even if the request is sent multiple times, the email is processed only once. This is invaluable for testing retry logic without sending duplicate emails.
1const fetch = require('node-fetch');2 3async function sendIdempotentTestEmail() {4 const apiKey = 'YOUR_API_KEY';5 const idempotencyKey = 'my-unique-test-key-123'; // Use a unique key for each logical send6 7 const response = await fetch('https://api.piisend.com/api/v1/emails', {8 method: 'POST',9 headers: {10 'Authorization': `Bearer ${apiKey}`,11 'Content-Type': 'application/json',12 'Idempotency-Key': idempotencyKey13 },14 body: JSON.stringify({15 to: ['idempotency-test@example.com'],16 subject: 'Idempotent Test Email',17 html: '<p>This email should only be sent once.</p>'18 })19 });20 21 const data = await response.json();22 console.log(response.status, data);23}24 25sendIdempotentTestEmail();