Created
March 27, 2024 21:05
-
-
Save antonkalik/9b265c54eaa2e1d30ec18aa51dde6243 to your computer and use it in GitHub Desktop.
Email Service sendPasswordResetEmail method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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