Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

direct emails support

Blue Mouse0bc9aaa82b29
diff --git a/backend/apps/community/src/app.module.ts b/backend/apps/community/src/app.module.ts
index 2a35a979d..d0231ead0 100644
--- a/backend/apps/community/src/app.module.ts
+++ b/backend/apps/community/src/app.module.ts
@@ -23,20 +23,41 @@ const modules = [
expandVariables: true,
isGlobal: true,
}),
- NodeMailerModule.forRoot({
- transport: {
- sendingRate: 14,
- // pool: true, // if true - set up pooled connections against a SMTP server
- host: process.env.SMTP_HOST,
- port: Number(process.env.SMTP_PORT),
- secure: true, // if false - upgrade later with STARTTLS
- auth: {
- user: process.env.SMTP_USER,
- pass: process.env.SMTP_PASSWORD,
- },
- },
- defaults: {
- from: `"Swetrix Community Edition" <${process.env.FROM_EMAIL || '[email protected]'}>`, // outgoing email ID
+ NodeMailerModule.forRootAsync({
+ useFactory: () => {
+ const hasSmtpConfig =
+ !!process.env.SMTP_HOST &&
+ !!process.env.SMTP_PORT &&
+ !!process.env.SMTP_USER &&
+ !!process.env.SMTP_PASSWORD
+
+ const defaults = {
+ from: `"Swetrix Community Edition" <${process.env.FROM_EMAIL || '[email protected]'}>`,
+ }
+
+ if (hasSmtpConfig) {
+ return {
+ transport: {
+ secure: true,
+ host: process.env.SMTP_HOST,
+ port: Number(process.env.SMTP_PORT),
+ auth: {
+ user: process.env.SMTP_USER,
+ pass: process.env.SMTP_PASSWORD,
+ },
+ },
+ defaults,
+ }
+ }
+
+ // Fallback transport so the module initializes without SMTP; actual sending
+ // will be handled directly in the MailerService via MX lookups
+ return {
+ transport: {
+ streamTransport: true,
+ },
+ defaults,
+ }
},
}),
I18nModule.forRootAsync(getI18nConfig()),
diff --git a/backend/apps/community/src/mailer/mailer.service.ts b/backend/apps/community/src/mailer/mailer.service.ts
index c8a00d229..dd4d0ffe1 100644
--- a/backend/apps/community/src/mailer/mailer.service.ts
+++ b/backend/apps/community/src/mailer/mailer.service.ts
@@ -3,6 +3,8 @@ import path from 'path'
import { MailerService as NodeMailerService } from '@nestjs-modules/mailer'
import { Injectable } from '@nestjs/common'
import handlebars from 'handlebars'
+import nodemailer from 'nodemailer'
+import dns from 'dns/promises'
import { LetterTemplate } from './letter'
import { AppLoggerService } from '../logger/logger.service'
@@ -64,6 +66,90 @@ export class MailerService {
private readonly nodeMailerService: NodeMailerService,
) {}
+ private hasSmtpConfig(): boolean {
+ return (
+ !!process.env.SMTP_HOST &&
+ !!process.env.SMTP_PORT &&
+ !!process.env.SMTP_USER &&
+ !!process.env.SMTP_PASSWORD
+ )
+ }
+
+ private async sendDirect(message: {
+ from: string
+ to: string
+ subject: string
+ html: string
+ }): Promise<boolean> {
+ try {
+ const to = message.to
+ const domain = (to.split('@')[1] || '').trim().toLowerCase()
+ if (!domain) {
+ this.logger.error(
+ 'Invalid recipient e-mail address',
+ 'sendDirect',
+ true,
+ )
+ return false
+ }
+
+ let mxRecords: Array<{ exchange: string; priority: number }> = []
+ try {
+ mxRecords = await dns.resolveMx(domain)
+ } catch (err) {
+ this.logger.error(err, 'resolveMx', true)
+ }
+
+ // Sort by priority ascending
+ mxRecords.sort((a, b) => a.priority - b.priority)
+
+ const targets = mxRecords.length
+ ? mxRecords.map(mx => mx.exchange)
+ : [domain]
+
+ for (const host of targets) {
+ try {
+ const transporter = nodemailer.createTransport({
+ host,
+ port: 25,
+ secure: false,
+ name: process.env.SMTP_HELO_NAME || 'swetrix-ce',
+ connectionTimeout: 10_000,
+ greetingTimeout: 10_000,
+ socketTimeout: 15_000,
+ tls: {
+ rejectUnauthorized: false,
+ },
+ })
+
+ await transporter.sendMail({
+ ...message,
+ envelope: {
+ from: message.from,
+ to: [to],
+ },
+ disableFileAccess: true,
+ disableUrlAccess: true,
+ })
+
+ return true
+ } catch (reason) {
+ this.logger.error(
+ { host, err: (reason as Error)?.message || reason },
+ 'sendDirectAttempt',
+ true,
+ )
+ continue
+ }
+ }
+
+ return false
+ } catch (reason) {
+ this.logger.error(reason, 'sendDirect', true)
+ return false
+ }
+ }
+
async sendEmail(
email: string,
templateName: LetterTemplate,
@@ -92,8 +178,13 @@ export class MailerService {
'sendEmail',
true,
)
- } else {
+ } else if (this.hasSmtpConfig()) {
await this.nodeMailerService.sendMail(message)
+ } else {
+ const ok = await this.sendDirect(message)
+ if (!ok) {
+ throw new Error('All direct delivery attempts failed')
+ }
}
} catch (reason) {
this.logger.error(reason, 'sendEmail', true)