Skip to content

Instantly share code, notes, and snippets.

@antonkalik
Created March 27, 2024 21:05
Show Gist options
  • Save antonkalik/9b265c54eaa2e1d30ec18aa51dde6243 to your computer and use it in GitHub Desktop.
Save antonkalik/9b265c54eaa2e1d30ec18aa51dde6243 to your computer and use it in GitHub Desktop.
Email Service sendPasswordResetEmail method
import process from 'process';
import * as nodemailer from 'nodemailer';
import * as dotenv from 'dotenv';
import { generateAttachments } from 'src/helpers/generateAttachments';
import { generateTemplate } from 'src/helpers/generateTemplate';
import { getHost } from 'src/helpers/getHost';
dotenv.config();
export class EmailService {
// ...rest code
public static async sendPasswordResetEmail(email: string, token: string) {
try {
const host = getHost();
const template = generateTemplate<{
token: string;
host: string;
}>('passwordResetTemplate', { token, host });
const attachments = generateAttachments([{ name: 'email_logo' }]);
const info = await EmailService.transporter.sendMail({
from: this.env.USER,
to: email,
subject: 'Password Reset',
html: template,
attachments,
});
console.log('Message sent: %s', info.messageId);
} catch (error) {
console.error('Error sending email: ', error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment