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,13 +25,13 @@ 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 = '';
@ -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>
@ -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 {