249 lines
14 KiB
JavaScript
249 lines
14 KiB
JavaScript
const nodemailer = require('nodemailer');
|
|
|
|
/**
|
|
* Sends a premium custom email with the PDF attachment.
|
|
* @param {string} toEmail - The recipient's email address
|
|
* @param {Object} orderData - The Shopify order payload
|
|
* @param {Buffer} pdfBuffer - The PDF data buffer
|
|
*/
|
|
const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => {
|
|
const transporter = nodemailer.createTransport({
|
|
host: process.env.SMTP_HOST,
|
|
port: Number(process.env.SMTP_PORT) || 465,
|
|
secure: process.env.SMTP_SECURE === 'true',
|
|
auth: {
|
|
user: process.env.SMTP_USER,
|
|
pass: process.env.SMTP_PASS,
|
|
},
|
|
tls: {
|
|
rejectUnauthorized: false
|
|
}
|
|
});
|
|
|
|
const orderNumber = orderData.order_number || orderData.name;
|
|
const currencySymbol = orderData.currency === 'INR' ? 'Rs. ' : `${orderData.currency} `;
|
|
|
|
// --- 1. Generate Order Summary Rows HTML ---
|
|
let orderRowsHtml = '';
|
|
if (orderData.line_items && orderData.line_items.length > 0) {
|
|
orderData.line_items.forEach(item => {
|
|
const itemImageUrl = item.image || (item.featured_image && item.featured_image.url) || '';
|
|
const imageHtml = itemImageUrl
|
|
? `<td style="padding: 15px 0; width: 60px; vertical-align: middle;">
|
|
<img src="${itemImageUrl}" alt="${item.title}" style="width: 50px; height: 50px; border-radius: 6px; border: 1px solid #e2e8f0; object-fit: cover;" />
|
|
</td>`
|
|
: `<td style="padding: 15px 0; width: 60px; vertical-align: middle;">
|
|
<div style="width: 50px; height: 50px; border-radius: 6px; border: 1px solid #e2e8f0; background: #f8fafc;"></div>
|
|
</td>`;
|
|
|
|
const variantText = item.variant_title ? `<span style="font-size: 12px; color: #64748b; display: block; margin-top: 2px;">${item.variant_title}</span>` : '';
|
|
const itemTotal = (parseFloat(item.price) * item.quantity).toFixed(2);
|
|
|
|
orderRowsHtml += `
|
|
<tr style="border-bottom: 1px solid #f1f5f9;">
|
|
${imageHtml}
|
|
<td style="padding: 15px 10px; vertical-align: middle; font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #1e293b;">
|
|
<span style="font-weight: bold;">${item.title || item.name}</span> × ${item.quantity}
|
|
${variantText}
|
|
</td>
|
|
<td style="padding: 15px 0; text-align: right; vertical-align: middle; font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; color: #1e293b; width: 120px;">
|
|
${currencySymbol}${itemTotal}
|
|
</td>
|
|
</tr>
|
|
`;
|
|
});
|
|
}
|
|
|
|
// --- 2. Calculate Totals ---
|
|
const subtotal = parseFloat(orderData.subtotal_price).toFixed(2);
|
|
const shipping = parseFloat(orderData.total_shipping_price_set?.shop_money?.amount || 0).toFixed(2);
|
|
const taxes = parseFloat(orderData.total_tax || 0).toFixed(2);
|
|
const total = parseFloat(orderData.total_price).toFixed(2);
|
|
|
|
let taxRow = '';
|
|
if (parseFloat(taxes) > 0) {
|
|
taxRow = `
|
|
<tr>
|
|
<td style="padding: 4px 0; font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: #64748b;">Taxes</td>
|
|
<td style="padding: 4px 0; text-align: right; font-family: Helvetica, Arial, sans-serif; font-size: 13px; font-weight: bold; color: #1e293b;">${currencySymbol}${taxes}</td>
|
|
</tr>
|
|
`;
|
|
}
|
|
|
|
// --- 3. Billing Address Details HTML ---
|
|
const billing = orderData.billing_address || {};
|
|
let billingAddressHtml = '';
|
|
if (billing.first_name || billing.last_name) {
|
|
billingAddressHtml += `${billing.first_name || ''} ${billing.last_name || ''}<br/>`;
|
|
}
|
|
if (billing.address1) billingAddressHtml += `${billing.address1}<br/>`;
|
|
if (billing.address2) billingAddressHtml += `${billing.address2}<br/>`;
|
|
if (billing.city || billing.province || billing.zip) {
|
|
billingAddressHtml += `${billing.city || ''} ${billing.province || ''} ${billing.zip || ''}<br/>`;
|
|
}
|
|
if (billing.country) billingAddressHtml += `${billing.country}<br/>`;
|
|
if (billing.phone || orderData.phone) {
|
|
billingAddressHtml += `${billing.phone || orderData.phone}`;
|
|
}
|
|
|
|
const paymentMethod = orderData.gateway || 'Request Quote';
|
|
const shippingMethod = orderData.shipping_lines?.[0]?.title || 'Economy';
|
|
|
|
// --- 4. Beautiful Responsive HTML Template ---
|
|
const htmlTemplate = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Order Confirmation</title>
|
|
</head>
|
|
<body style="margin: 0; padding: 0; background-color: #f8fafc; -webkit-text-size-adjust: none; text-size-adjust: none;">
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="background-color: #f8fafc; padding: 20px 10px;">
|
|
<tr>
|
|
<td align="center">
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px; background-color: #ffffff; border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03); overflow: hidden; padding: 30px;">
|
|
|
|
<!-- Header Logo & Order number -->
|
|
<tr>
|
|
<td style="padding-bottom: 25px;">
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr>
|
|
<td style="font-family: Helvetica, Arial, sans-serif;">
|
|
<img src="https://cdn.shopify.com/s/files/1/0878/6791/2485/files/logo_150x.png" alt="rayaarishop" style="height: 60px; width: auto; display: block;" />
|
|
</td>
|
|
<td align="right" style="font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: #94a3b8; font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px;">
|
|
Order #${orderNumber}
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- GREETINGS -->
|
|
<tr>
|
|
<td style="font-family: Helvetica, Arial, sans-serif; padding-bottom: 20px;">
|
|
<h1 style="font-size: 22px; font-weight: bold; color: #1e293b; margin: 0 0 10px 0;">Thank you for your order!</h1>
|
|
<p style="font-size: 14px; line-height: 1.5; color: #475569; margin: 0;">
|
|
We're getting your order ready to be shipped. We have attached your custom PDF invoice directly to this email.
|
|
</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- ACTION BUTTON -->
|
|
${orderData.order_status_url ? `
|
|
<tr>
|
|
<td style="padding: 10px 0 30px 0;">
|
|
<a href="${orderData.order_status_url}" target="_blank" style="display: inline-block; background-color: #0284c7; color: #ffffff; font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; text-decoration: none; padding: 12px 24px; border-radius: 6px; box-shadow: 0 2px 4px rgba(2, 132, 199, 0.2);">
|
|
View your order
|
|
</a>
|
|
<span style="font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: #64748b; margin-left: 15px;">or <a href="https://rayaarishop.myshopify.com" target="_blank" style="color: #0284c7; text-decoration: none;">Visit our store</a></span>
|
|
</td>
|
|
</tr>
|
|
` : ''}
|
|
|
|
<!-- ORDER SUMMARY HEADER -->
|
|
<tr>
|
|
<td style="padding-bottom: 10px; border-bottom: 2px solid #e2e8f0;">
|
|
<h2 style="font-family: Helvetica, Arial, sans-serif; font-size: 15px; font-weight: bold; color: #1e293b; margin: 0; text-transform: uppercase; letter-spacing: 0.5px;">Order summary</h2>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- LINE ITEMS TABLE -->
|
|
<tr>
|
|
<td>
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
${orderRowsHtml}
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- TOTALS CALCULATION -->
|
|
<tr>
|
|
<td style="padding: 20px 0 30px 0;">
|
|
<table border="0" cellpadding="0" cellspacing="0" align="right" width="240" style="width: 240px;">
|
|
<tr>
|
|
<td style="padding: 4px 0; font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: #64748b;">Subtotal</td>
|
|
<td style="padding: 4px 0; text-align: right; font-family: Helvetica, Arial, sans-serif; font-size: 13px; font-weight: bold; color: #1e293b;">${currencySymbol}${subtotal}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 4px 0; font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: #64748b;">Shipping</td>
|
|
<td style="padding: 4px 0; text-align: right; font-family: Helvetica, Arial, sans-serif; font-size: 13px; font-weight: bold; color: #1e293b;">${currencySymbol}${shipping}</td>
|
|
</tr>
|
|
${taxRow}
|
|
<tr style="border-top: 1px solid #e2e8f0;">
|
|
<td style="padding: 15px 0 0 0; font-family: Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; color: #475569;">Total</td>
|
|
<td style="padding: 15px 0 0 0; text-align: right; font-family: Helvetica, Arial, sans-serif; font-size: 20px; font-weight: bold; color: #1e293b;">
|
|
${currencySymbol}${total} <span style="font-size: 12px; color: #64748b; font-weight: normal;">${orderData.currency}</span>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- CUSTOMER INFORMATION HEADER -->
|
|
<tr>
|
|
<td style="padding-top: 20px; padding-bottom: 10px; border-bottom: 2px solid #e2e8f0;">
|
|
<h2 style="font-family: Helvetica, Arial, sans-serif; font-size: 15px; font-weight: bold; color: #1e293b; margin: 0; text-transform: uppercase; letter-spacing: 0.5px;">Customer information</h2>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- CUSTOMER DETAILS BODY (2 COLUMNS) -->
|
|
<tr>
|
|
<td style="padding-top: 15px;">
|
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|
<tr>
|
|
<!-- Billing address column -->
|
|
<td valign="top" style="width: 50%; font-family: Helvetica, Arial, sans-serif; font-size: 13px; line-height: 1.5; color: #475569; padding-right: 10px;">
|
|
<strong style="color: #1e293b; display: block; margin-bottom: 5px;">Billing address</strong>
|
|
${billingAddressHtml}
|
|
</td>
|
|
|
|
<!-- Payment & Shipping info column -->
|
|
<td valign="top" style="width: 50%; font-family: Helvetica, Arial, sans-serif; font-size: 13px; line-height: 1.5; color: #475569; padding-left: 10px;">
|
|
<strong style="color: #1e293b; display: block; margin-bottom: 5px;">Payment method</strong>
|
|
<span style="display: block; margin-bottom: 15px;">${paymentMethod}</span>
|
|
|
|
<strong style="color: #1e293b; display: block; margin-bottom: 5px;">Shipping method</strong>
|
|
<span>${shippingMethod}</span>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
const mailOptions = {
|
|
from: `"${process.env.SHOP_NAME || 'Your Store'}" <${process.env.SMTP_FROM_EMAIL}>`,
|
|
to: toEmail,
|
|
subject: `Invoice for your order #${orderNumber}`,
|
|
text: `Thank you for your order! Attached is the invoice for order #${orderNumber}.`,
|
|
html: htmlTemplate,
|
|
attachments: [
|
|
{
|
|
filename: `Invoice_${orderNumber}.pdf`,
|
|
content: pdfBuffer,
|
|
contentType: 'application/pdf'
|
|
}
|
|
]
|
|
};
|
|
|
|
try {
|
|
const info = await transporter.sendMail(mailOptions);
|
|
console.log(`Email sent: ${info.messageId}`);
|
|
return info;
|
|
} catch (error) {
|
|
console.error('Error sending email:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
module.exports = { sendEmailWithAttachment };
|