fix(mail): harden mailer and fix missing notification recipients
Some checks failed
Build & Deploy / 🧪 QA (push) Failing after 1m24s
Build & Deploy / 🔍 Prepare (push) Successful in 16s
Build & Deploy / 🏗️ Build (push) Has been skipped
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 4s
Some checks failed
Build & Deploy / 🧪 QA (push) Failing after 1m24s
Build & Deploy / 🔍 Prepare (push) Successful in 16s
Build & Deploy / 🏗️ Build (push) Has been skipped
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 4s
This commit is contained in:
@@ -95,6 +95,7 @@ export async function sendContactFormAction(formData: FormData) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!isTestSubmission) {
|
if (!isTestSubmission) {
|
||||||
|
logger.info('Sending internal notification', { recipients: config.mail.recipients });
|
||||||
const notificationResult = await sendEmail({
|
const notificationResult = await sendEmail({
|
||||||
replyTo: email,
|
replyTo: email,
|
||||||
subject: notificationSubject,
|
subject: notificationSubject,
|
||||||
@@ -106,14 +107,18 @@ export async function sendContactFormAction(formData: FormData) {
|
|||||||
messageId: notificationResult.messageId,
|
messageId: notificationResult.messageId,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
logger.error('Notification email FAILED', {
|
logger.error('Notification email DELIVERY FAILED', {
|
||||||
error: notificationResult.error,
|
error: notificationResult.error,
|
||||||
subject: notificationSubject,
|
subject: notificationSubject,
|
||||||
email,
|
recipients: config.mail.recipients,
|
||||||
});
|
});
|
||||||
services.errors.captureException(
|
services.errors.captureException(
|
||||||
new Error(`Notification email failed: ${notificationResult.error}`),
|
new Error(`Notification email failed: ${notificationResult.error}`),
|
||||||
{ action: 'sendContactFormAction_notification', email },
|
{
|
||||||
|
action: 'sendContactFormAction_notification',
|
||||||
|
email,
|
||||||
|
recipients: config.mail.recipients
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -130,6 +135,7 @@ export async function sendContactFormAction(formData: FormData) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!isTestSubmission) {
|
if (!isTestSubmission) {
|
||||||
|
logger.info('Sending customer confirmation', { to: email });
|
||||||
const confirmationResult = await sendEmail({
|
const confirmationResult = await sendEmail({
|
||||||
to: email,
|
to: email,
|
||||||
subject: confirmationSubject,
|
subject: confirmationSubject,
|
||||||
@@ -141,7 +147,7 @@ export async function sendContactFormAction(formData: FormData) {
|
|||||||
messageId: confirmationResult.messageId,
|
messageId: confirmationResult.messageId,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
logger.error('Confirmation email FAILED', {
|
logger.error('Confirmation email DELIVERY FAILED', {
|
||||||
error: confirmationResult.error,
|
error: confirmationResult.error,
|
||||||
subject: confirmationSubject,
|
subject: confirmationSubject,
|
||||||
to: email,
|
to: email,
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ const envExtension = {
|
|||||||
MAIL_USERNAME: z.string().optional(),
|
MAIL_USERNAME: z.string().optional(),
|
||||||
MAIL_PASSWORD: z.string().optional(),
|
MAIL_PASSWORD: z.string().optional(),
|
||||||
MAIL_FROM: z.string().optional(),
|
MAIL_FROM: z.string().optional(),
|
||||||
MAIL_RECIPIENTS: z.string().optional(),
|
MAIL_RECIPIENTS: z.string().trim().optional(),
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -32,16 +32,27 @@ interface SendEmailOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function sendEmail({ to, replyTo, subject, html }: SendEmailOptions) {
|
export async function sendEmail({ to, replyTo, subject, html }: SendEmailOptions) {
|
||||||
const recipients = to || config.mail.recipients;
|
|
||||||
const logger = getServerAppServices().logger.child({ component: 'mailer' });
|
const logger = getServerAppServices().logger.child({ component: 'mailer' });
|
||||||
|
|
||||||
|
// Resolve recipients: priority to 'to' override, fallback to global MAIL_RECIPIENTS
|
||||||
|
const resolvedTo = to || config.mail.recipients;
|
||||||
|
|
||||||
|
// Normalize recipients (handle arrays or comma-strings)
|
||||||
|
const recipients = Array.isArray(resolvedTo)
|
||||||
|
? resolvedTo.join(', ')
|
||||||
|
: (resolvedTo?.toString() || '');
|
||||||
|
|
||||||
if (!recipients) {
|
if (!recipients || recipients.trim() === '') {
|
||||||
logger.error('No email recipients configured (MAIL_RECIPIENTS is empty and no "to" provided)', { subject });
|
logger.error('Email delivery ABORTED: No recipients configured', {
|
||||||
|
subject,
|
||||||
|
providedTo: to,
|
||||||
|
configRecipients: config.mail.recipients
|
||||||
|
});
|
||||||
return { success: false as const, error: 'No recipients configured' };
|
return { success: false as const, error: 'No recipients configured' };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.mail.from) {
|
if (!config.mail.from) {
|
||||||
logger.error('MAIL_FROM is not configured — cannot send email', { subject, recipients });
|
logger.error('Email delivery ABORTED: MAIL_FROM is missing', { subject, recipients });
|
||||||
return { success: false as const, error: 'MAIL_FROM is not configured' };
|
return { success: false as const, error: 'MAIL_FROM is not configured' };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,14 +64,36 @@ export async function sendEmail({ to, replyTo, subject, html }: SendEmailOptions
|
|||||||
html,
|
html,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const info = await getTransporter().sendMail(mailOptions);
|
const transporter = getTransporter();
|
||||||
logger.info('Email sent successfully', { messageId: info.messageId, subject, recipients });
|
logger.info('Attempting to send email via SMTP', {
|
||||||
|
host: config.mail.host,
|
||||||
|
subject,
|
||||||
|
recipients,
|
||||||
|
hasReplyTo: !!replyTo
|
||||||
|
});
|
||||||
|
|
||||||
|
const info = await transporter.sendMail(mailOptions);
|
||||||
|
|
||||||
|
logger.info('Email sent successfully', {
|
||||||
|
messageId: info.messageId,
|
||||||
|
subject,
|
||||||
|
recipients,
|
||||||
|
response: info.response
|
||||||
|
});
|
||||||
|
|
||||||
return { success: true, messageId: info.messageId };
|
return { success: true, messageId: info.messageId };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMsg = error instanceof Error ? error.message : String(error);
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
||||||
logger.error('Error sending email', { error: errorMsg, subject, recipients });
|
logger.error('SMTP Transport failed', {
|
||||||
|
error: errorMsg,
|
||||||
|
subject,
|
||||||
|
recipients,
|
||||||
|
config: {
|
||||||
|
host: config.mail.host,
|
||||||
|
user: config.mail.user ? '***' : 'not set'
|
||||||
|
}
|
||||||
|
});
|
||||||
return { success: false, error: errorMsg };
|
return { success: false, error: errorMsg };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,7 +160,7 @@
|
|||||||
"prepare": "husky",
|
"prepare": "husky",
|
||||||
"preinstall": "npx only-allow pnpm"
|
"preinstall": "npx only-allow pnpm"
|
||||||
},
|
},
|
||||||
"version": "2.3.11",
|
"version": "2.3.12",
|
||||||
"pnpm": {
|
"pnpm": {
|
||||||
"onlyBuiltDependencies": [
|
"onlyBuiltDependencies": [
|
||||||
"@parcel/watcher",
|
"@parcel/watcher",
|
||||||
|
|||||||
Reference in New Issue
Block a user