mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
* 🚚 Move schema to standalone file * ⚡ Add assertions to string literal arrays * ✨ Infer typings for convict schema * 🔥 Remove unneeded assertions * 🔨 Fix errors surfaced by typings * ⚡ Type nodes.include/exclude per docs * ⚡ Account for types for exception paths * ⚡ Set method alias to flag incorrect paths * ⚡ Replace original with alias * ⚡ Make allowance for nodes.include * ⚡ Adjust leftover calls * 🔀 Fix conflicts * 🔥 Remove unneeded castings * 📘 Simplify exception path type * 📦 Update package-lock.json * 🔥 Remove unneeded imports * 🔥 Remove unrelated file * ⚡ Update schema * ⚡ Update interface * 📦 Update package-lock.json * 📦 Update package-lock.json * 🔥 Remove leftover assertions Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
import { createTransport, Transporter } from 'nodemailer';
|
|
import { LoggerProxy as Logger } from 'n8n-workflow';
|
|
import config = require('../../../config');
|
|
import { MailData, SendEmailResult, UserManagementMailerImplementation } from './Interfaces';
|
|
|
|
export class NodeMailer implements UserManagementMailerImplementation {
|
|
private transport: Transporter;
|
|
|
|
constructor() {
|
|
this.transport = createTransport({
|
|
host: config.getEnv('userManagement.emails.smtp.host'),
|
|
port: config.getEnv('userManagement.emails.smtp.port'),
|
|
secure: config.getEnv('userManagement.emails.smtp.secure'),
|
|
auth: {
|
|
user: config.getEnv('userManagement.emails.smtp.auth.user'),
|
|
pass: config.getEnv('userManagement.emails.smtp.auth.pass'),
|
|
},
|
|
});
|
|
}
|
|
|
|
async verifyConnection(): Promise<void> {
|
|
const host = config.getEnv('userManagement.emails.smtp.host');
|
|
const user = config.getEnv('userManagement.emails.smtp.auth.user');
|
|
const pass = config.getEnv('userManagement.emails.smtp.auth.pass');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
this.transport.verify((error: Error) => {
|
|
if (!error) {
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
const message = [];
|
|
|
|
if (!host) message.push('SMTP host not defined (N8N_SMTP_HOST).');
|
|
if (!user) message.push('SMTP user not defined (N8N_SMTP_USER).');
|
|
if (!pass) message.push('SMTP pass not defined (N8N_SMTP_PASS).');
|
|
|
|
reject(new Error(message.length ? message.join(' ') : error.message));
|
|
});
|
|
});
|
|
}
|
|
|
|
async sendMail(mailData: MailData): Promise<SendEmailResult> {
|
|
let sender = config.getEnv('userManagement.emails.smtp.sender');
|
|
const user = config.getEnv('userManagement.emails.smtp.auth.user');
|
|
|
|
if (!sender && user.includes('@')) {
|
|
sender = user;
|
|
}
|
|
|
|
try {
|
|
await this.transport.sendMail({
|
|
from: sender,
|
|
to: mailData.emailRecipients,
|
|
subject: mailData.subject,
|
|
text: mailData.textOnly,
|
|
html: mailData.body,
|
|
});
|
|
Logger.verbose(
|
|
`Email sent successfully to the following recipients: ${mailData.emailRecipients.toString()}`,
|
|
);
|
|
} catch (error) {
|
|
Logger.error('Failed to send email', { recipients: mailData.emailRecipients, error });
|
|
return {
|
|
success: false,
|
|
error,
|
|
};
|
|
}
|
|
|
|
return { success: true };
|
|
}
|
|
}
|