LibreChat/packages/api/src/utils/email.ts
mfish911 4e5ae28fa9
📡 feat: Support Unauthenticated SMTP Relays (#12322)
* allow smtp server that does not have authentication

* fix: align checkEmailConfig with optional SMTP credentials and add tests

Remove EMAIL_USERNAME/EMAIL_PASSWORD requirements from the hasSMTPConfig
predicate in checkEmailConfig() so the rest of the codebase (login,
startup checks, invite-user) correctly recognizes unauthenticated SMTP
as a valid email configuration.

Add a warning when only one of the two credential env vars is set,
in both sendEmail.js and checkEmailConfig(), to catch partial
misconfigurations early.

Add test coverage for both the transporter auth assembly in sendEmail.js
and the checkEmailConfig predicate in packages/api.

Document in .env.example that credentials are optional for
unauthenticated SMTP relays.

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2026-03-20 13:07:39 -04:00

25 lines
861 B
TypeScript

import { logger } from '@librechat/data-schemas';
/**
* Check if email configuration is set
* @returns Returns `true` if either Mailgun or SMTP is properly configured
*/
export function checkEmailConfig(): boolean {
const hasMailgunConfig =
!!process.env.MAILGUN_API_KEY && !!process.env.MAILGUN_DOMAIN && !!process.env.EMAIL_FROM;
const hasSMTPConfig =
(!!process.env.EMAIL_SERVICE || !!process.env.EMAIL_HOST) && !!process.env.EMAIL_FROM;
if (hasSMTPConfig) {
const hasUsername = !!process.env.EMAIL_USERNAME;
const hasPassword = !!process.env.EMAIL_PASSWORD;
if (hasUsername !== hasPassword) {
logger.warn(
'[checkEmailConfig] EMAIL_USERNAME and EMAIL_PASSWORD must both be set for authenticated SMTP, or both omitted for unauthenticated SMTP.',
);
}
}
return hasMailgunConfig || hasSMTPConfig;
}