-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.service.ts
63 lines (58 loc) · 1.77 KB
/
mailer.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Project } from '../database/project/project.entity';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { createTransport } from 'nodemailer';
import { AppLogger } from 'src/utils/app-logger.util';
/**
* Dynamic configuration from G Suite
*/
const mailConf = require("../../mail.conf.json");
@Injectable()
export class MailerService implements OnModuleInit {
private readonly _transporter = createTransport({
host: "smtp.gmail.com",
port: mailConf?.port,
auth: {
type: 'OAuth2',
user: mailConf?.mail,
serviceClient: mailConf?.client_id,
privateKey: mailConf?.private_key
},
});
constructor(private readonly _logger: AppLogger) {}
public async onModuleInit() {
try {
this._logger.log("Checking mail server configuration...");
if (!mailConf)
throw new Error("Mail configuration not found");
await this._transporter.verify();
this._logger.log("Mail server configuration OK");
} catch(e) {
this._logger.error("Mail error during verification", e);
}
return this;
}
/**
* Send a mail to all project collaborators
* @param project The project to send the mail to
* @param message The message to send
*/
public async sendMailToProject(project: Project, message: string) {
try {
await this._transporter.sendMail({
from: mailConf.mail,
to: project.collaborators.map(c => c.user.mail).join(","),
subject: `[${project.name}] Notification Herogu`,
html: `
<h1 style='text-align: center'>Notification Herogu</h1>
<p>${message}</p>
<br>
<br>
<p>Cordialement,</p>
<p>Garage</p>
`
});
} catch (e) {
this._logger.error("Error sending mail !", e);
}
}
}