2026-05-18 22:14:41 +05:30

51 lines
1.8 KiB
JavaScript

const nodemailer = require('nodemailer');
/**
* Sends an email with the PDF attachment.
* @param {string} toEmail - The recipient's email address
* @param {string|number} orderNumber - The Shopify order number
* @param {Buffer} pdfBuffer - The PDF data buffer
*/
const sendEmailWithAttachment = async (toEmail, orderNumber, pdfBuffer) => {
// Create a transporter using your email service credentials
// You can configure SMTP, SendGrid, Amazon SES, etc.
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT) || 465,
secure: process.env.SMTP_SECURE === 'true', // true for 465, false for other ports
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
tls: {
rejectUnauthorized: false
}
});
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: `<p>Thank you for your order!</p><p>Attached is the invoice for order <b>#${orderNumber}</b>.</p>`,
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 };