Files
ipal-kit/dist/modules/email/sendEmail.js
T

70 lines
2.1 KiB
JavaScript

import 'server-only';
import nodemailer from 'nodemailer';
import { getSiteIntegrations } from '../payload/index.js';
/**
* Sends an email using SMTP settings from the SiteIntegrations global.
*
* Reads config at call time (not at boot) so editors can change SMTP in the
* admin panel without a restart — consistent with the plugin's panel-managed
* model. `server-only` keeps the SMTP password out of any client bundle.
*
* Returns a result object rather than throwing, so callers (e.g. form
* submission) can handle failure without a try/catch and never leak SMTP
* details to the client.
*/ export async function sendEmail({ from, html, payload, replyTo, subject, text, to }) {
const smtp = await getSiteIntegrations(payload);
const host = smtp.smtpHost;
const port = smtp.smtpPort ?? 587;
const user = smtp.smtpUser;
const pass = smtp.smtpPassword;
if (!host || !user || !pass) {
return {
error: 'SMTP is not configured in Site Integrations.',
sent: false
};
}
const fromAddress = from ?? smtp.smtpFromAddress;
if (!fromAddress) {
return {
error: 'No from-address configured.',
sent: false
};
}
const fromName = smtp.smtpFromName;
const fromHeader = fromName ? `${fromName} <${fromAddress}>` : fromAddress;
const transporter = nodemailer.createTransport({
auth: {
pass,
user
},
host,
port,
secure: port === 465
});
try {
const info = await transporter.sendMail({
from: fromHeader,
html,
subject,
to,
...text ? {
text
} : {},
...replyTo ? {
replyTo
} : {}
});
return {
messageId: info.messageId,
sent: true
};
} catch (err) {
payload.logger.error(`[ipal] Email send failed: ${err.message}`);
return {
error: 'Failed to send email.',
sent: false
};
}
}
//# sourceMappingURL=sendEmail.js.map