feat: implement MailModule with email services and tests
Added MailModule with services for email validation and password reset functionalities. Includes configuration via `@nestjs-modules/mailer` and comprehensive unit tests.
This commit is contained in:
62
backend/src/mail/mail.service.ts
Normal file
62
backend/src/mail/mail.service.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { MailerService } from "@nestjs-modules/mailer";
|
||||
|
||||
@Injectable()
|
||||
export class MailService {
|
||||
private readonly logger = new Logger(MailService.name);
|
||||
private readonly domain: string;
|
||||
|
||||
constructor(
|
||||
private mailerService: MailerService,
|
||||
private configService: ConfigService,
|
||||
) {
|
||||
this.domain = this.configService.get<string>("DOMAIN_NAME", "memegoat.io");
|
||||
}
|
||||
|
||||
async sendEmailValidation(email: string, token: string) {
|
||||
const url = `https://${this.domain}/api/auth/verify-email?token=${token}`;
|
||||
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: email,
|
||||
subject: "Validation de votre adresse email",
|
||||
html: `
|
||||
<p>Bonjour,</p>
|
||||
<p>Veuillez cliquer sur le lien ci-dessous pour valider votre adresse email :</p>
|
||||
<p><a href="${url}">${url}</a></p>
|
||||
<p>Si vous n'avez pas demandé cette validation, vous pouvez ignorer cet email.</p>
|
||||
`,
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to send validation email to ${email}: ${error.message}`,
|
||||
error.stack,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async sendPasswordReset(email: string, token: string) {
|
||||
const url = `https://${this.domain}/api/auth/reset-password?token=${token}`;
|
||||
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: email,
|
||||
subject: "Réinitialisation de votre mot de passe",
|
||||
html: `
|
||||
<p>Bonjour,</p>
|
||||
<p>Veuillez cliquer sur le lien ci-dessous pour réinitialiser votre mot de passe :</p>
|
||||
<p><a href="${url}">${url}</a></p>
|
||||
<p>Si vous n'avez pas demandé de réinitialisation, vous pouvez ignorer cet email.</p>
|
||||
`,
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to send password reset email to ${email}: ${error.message}`,
|
||||
error.stack,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user