diff --git a/mailer.js b/mailer.js index ef5c5e9..2ac9039 100644 --- a/mailer.js +++ b/mailer.js @@ -25,14 +25,14 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => { const orderNumber = orderData.order_number || orderData.name; const currencySymbol = orderData.currency === 'INR' ? 'Rs. ' : `${orderData.currency} `; - // Load logo as base64 so it always displays in Gmail (no external URL blocking) + // Check if local logo exists for CID inline attachment const localLogoPath = path.join(__dirname, 'logo.png'); - let logoBase64 = ''; - let logoMimeType = 'image/png'; - if (fs.existsSync(localLogoPath)) { - logoBase64 = fs.readFileSync(localLogoPath).toString('base64'); - } - + const logoExists = fs.existsSync(localLogoPath); + // Use cid:store_logo in HTML - Gmail renders CID attachments correctly + const logoImgTag = logoExists + ? `rayaarishop` + : `rayaarishop`; + // --- 1. Generate Order Summary Rows HTML --- let orderRowsHtml = ''; if (orderData.line_items && orderData.line_items.length > 0) { @@ -61,7 +61,7 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => { 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 = ` @@ -112,10 +112,7 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => { @@ -219,19 +216,30 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => { `; + // Build attachments array - always include PDF, add logo as CID inline if it exists + const attachments = [ + { + filename: `Invoice_${orderNumber}.pdf`, + content: pdfBuffer, + contentType: 'application/pdf' + } + ]; + + if (logoExists) { + attachments.push({ + filename: 'logo.png', + path: localLogoPath, + cid: 'store_logo' // Referenced in HTML as src="cid:store_logo" + }); + } + 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' - } - ] + attachments }; try {
- ${logoBase64 - ? `rayaarishop` - : `rayaarishop` - } + ${logoImgTag}

New Order: #${orderNumber}

@@ -133,10 +130,10 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => { ${orderData.billing_address ? `${orderData.billing_address.first_name || ''} ${orderData.billing_address.last_name || ''}`.trim() : (orderData.contact_email || 'a customer')}:

- ${orderData.order_status_url - ? `[Order #${orderNumber}]` - : `[Order #${orderNumber}]` - } + ${orderData.order_status_url + ? `[Order #${orderNumber}]` + : `[Order #${orderNumber}]` + } (${new Date(orderData.created_at || Date.now()).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })})