import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import SendGrid from '@sendgrid/mail'; import { User } from '@/users/entities/user.entity'; @Injectable() export class MailService { constructor(private configService: ConfigService) { SendGrid.setApiKey(this.configService.get('sendgrid.apiKey')); } async sendEmail(mail: SendGrid.MailDataRequired) { return SendGrid.send(mail); } async sendForgotPasswordEmail(user: User, resetToken: string): Promise { const appUrl = this.configService.get('app.url'); const url = `${appUrl}?modal=auth.reset&resetToken=${resetToken}`; const mailData: SendGrid.MailDataRequired = { from: { name: this.configService.get('sendgrid.fromName'), email: this.configService.get('sendgrid.fromEmail'), }, to: user.email, hideWarnings: true, dynamicTemplateData: { url }, templateId: this.configService.get('sendgrid.forgotPasswordTemplateId'), }; await SendGrid.send(mailData); return; } }