Tutorial•3 min read

Building Order Confirmation Emails with the Piisend API

Learn how to send robust order confirmation emails using the Piisend API, leveraging templates for dynamic content, attaching invoices, and monitoring delivery with webhooks. Ensure your ecommerce transactional email flow is reliable and informative for your customers.

Building Order Confirmation Emails with the Piisend API

Section 1

Sending Your First Order Confirmation Email

Order confirmation emails are crucial for ecommerce, providing immediate reassurance to customers after a purchase. With Piisend's transactional email API, you can quickly integrate this functionality into your application. This initial example demonstrates how to send a basic order confirmation email using a direct HTML payload.

bash
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": ["customer@example.com"],
6 "subject": "Your Order #12345 Has Been Confirmed!",
7 "html": "<p>Dear Customer,</p><p>Thank you for your order! Your order #12345 has been successfully placed and will be shipped soon.</p><p>View your order details here: <a href=\"https://yourstore.com/orders/12345\">Order Details</a></p>"
8 }'

Section 2

Leveraging Templates for Dynamic Order Details

For consistent branding and dynamic content, using email templates is highly recommended for your order confirmation email. Piisend allows you to define templates with merge variables, making it easy to personalize each receipt email API call with specific order data like items, total, and shipping information. This approach simplifies maintenance and ensures a professional look.

python
1import requests
2import json
3
4API_KEY = "YOUR_API_KEY"
5PIISEND_API_URL = "https://api.piisend.com/api/v1/emails"
6
7headers = {
8 "Authorization": f"Bearer {API_KEY}",
9 "Content-Type": "application/json"
10}
11
12payload = {
13 "to": ["customer@example.com"],
14 "template_id": "order_confirmation_template_id",
15 "template_vars": {
16 "customer_name": "Jane Doe",
17 "order_id": "67890",
18 "order_date": "2024-07-20",
19 "total_amount": "$129.99",
20 "items": [
21 {"name": "Product A", "quantity": 1, "price": "$79.99"},
22 {"name": "Product B", "quantity": 1, "price": "$50.00"}
23 ],
24 "order_link": "https://yourstore.com/orders/67890"
25 }
26}
27
28response = requests.post(PIISEND_API_URL, headers=headers, data=json.dumps(payload))
29
30print(f"Status Code: {response.status_code}")
31print(f"Response: {response.json()}")

Section 3

Attaching Invoices or Receipts as PDFs

Many businesses need to include a detailed invoice email or receipt as a PDF attachment with their order confirmations. Piisend's API supports attachments by encoding the file content in base64. This ensures that customers receive all necessary documentation directly in their inbox, enhancing the overall customer experience.

javascript
1const fetch = require('node-fetch');
2const fs = require('fs');
3
4const API_KEY = 'YOUR_API_KEY';
5const PIISEND_API_URL = 'https://api.piisend.com/api/v1/emails';
6
7// Read PDF file and encode to base64
8const pdfPath = './invoice_12345.pdf';
9const pdfContent = fs.readFileSync(pdfPath).toString('base64');
10
11const payload = {
12 to: ['customer@example.com'],
13 subject: 'Your Invoice for Order #12345',
14 html: '<p>Please find your invoice attached.</p>',
15 attachments: [
16 {
17 filename: 'invoice_12345.pdf',
18 content: pdfContent,
19 content_type: 'application/pdf'
20 }
21 ]
22};
23
24fetch(PIISEND_API_URL, {
25 method: 'POST',
26 headers: {
27 'Authorization': `Bearer ${API_KEY}`,
28 'Content-Type': 'application/json'
29 },
30 body: JSON.stringify(payload)
31})
32.then(response => response.json())
33.then(data => console.log(data))
34.catch(error => console.error('Error:', error));

Section 4

Monitoring Delivery and Handling Failures with Webhooks

Understanding the delivery status of your order confirmation emails is critical for customer support and maintaining a reliable ecommerce transactional email system. Piisend's webhooks provide real-time notifications for events like delivered, opened, clicked, or bounced emails. This allows you to react promptly to delivery failures and update your internal systems. Remember to verify webhook signatures using the X-Webhook-Signature header and X-Webhook-Timestamp for security.

json
1{
2 "event": "delivered",
3 "message_id": "msg_abcdef1234567890",
4 "recipient": "customer@example.com",
5 "timestamp": 1678886400,
6 "payload": {
7 "to": "customer@example.com",
8 "subject": "Your Order #12345 Has Been Confirmed!",
9 "metadata": {
10 "order_id": "12345"
11 }
12 }
13}

Section 5

Ensuring Reliability with Idempotency and Delivery Logs

To prevent duplicate order confirmation emails, especially in distributed systems or during retries, Piisend supports idempotency. By including an Idempotency-Key header with a unique value for each send attempt, you ensure that the API processes the request only once. Additionally, Piisend's delivery logs offer a comprehensive record of all email activity, invaluable for debugging and customer service inquiries about missing invoice emails.

bash
1curl -X POST https://api.piisend.com/api/v1/emails \
2 -H "Authorization: Bearer YOUR_API_KEY" \
3 -H "Content-Type: application/json" \
4 -H "Idempotency-Key: order-12345-confirmation-v1" \
5 -d '{
6 "to": ["customer@example.com"],
7 "subject": "Your Order #12345 Has Been Confirmed!",
8 "html": "<p>This email is idempotent.</p>"
9 }'

Start sending

Ship transactional email in minutes

Create an API key, verify a domain, and send your first message with Piisend.