fix: Use CID inline attachment for logo instead of blocked base64 data URI

This commit is contained in:
Alaguraj0361 2026-05-19 11:00:04 +05:30
parent f0026f65dd
commit 30dec851e9

View File

@ -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
? `<img src="cid:store_logo" alt="rayaarishop" style="height: 55px; width: auto; display: block;" />`
: `<span style="font-family: Helvetica, Arial, sans-serif; font-size: 20px; font-weight: bold; color: #ffffff;">rayaarishop</span>`;
// --- 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) => {
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="vertical-align: middle;">
${logoBase64
? `<img src="data:${logoMimeType};base64,${logoBase64}" alt="rayaarishop" style="height: 55px; width: auto; display: block;" />`
: `<span style="font-family: Helvetica, Arial, sans-serif; font-size: 20px; font-weight: bold; color: #ffffff;">rayaarishop</span>`
}
${logoImgTag}
</td>
<td align="right" style="vertical-align: middle;">
<h1 style="font-family: Helvetica, Arial, sans-serif; font-size: 22px; font-weight: bold; color: #ffffff; margin: 0;">New Order: #${orderNumber}</h1>
@ -133,10 +130,10 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => {
<strong style="color: #1e293b;">${orderData.billing_address ? `${orderData.billing_address.first_name || ''} ${orderData.billing_address.last_name || ''}`.trim() : (orderData.contact_email || 'a customer')}</strong>:
</p>
<p style="font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #475569; margin: 0;">
${orderData.order_status_url
? `<a href="${orderData.order_status_url}" target="_blank" style="color: #1d4ed8; font-weight: bold; text-decoration: none;">[Order #${orderNumber}]</a>`
: `<strong>[Order #${orderNumber}]</strong>`
}
${orderData.order_status_url
? `<a href="${orderData.order_status_url}" target="_blank" style="color: #1d4ed8; font-weight: bold; text-decoration: none;">[Order #${orderNumber}]</a>`
: `<strong>[Order #${orderNumber}]</strong>`
}
<strong> (${new Date(orderData.created_at || Date.now()).toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' })})</strong>
</p>
</td>
@ -219,19 +216,30 @@ const sendEmailWithAttachment = async (toEmail, orderData, pdfBuffer) => {
</html>
`;
// 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 {