73 lines
2 KiB
TypeScript
73 lines
2 KiB
TypeScript
import { ClientOptions, SMTPClient } from "https://deno.land/x/denomailer@1.6.0/mod.ts";
|
|
import { log_from } from "./utils.ts";
|
|
const log = (...args: unknown[]) => log_from(import.meta.url, ...args);
|
|
|
|
export type EmailerConfig = {
|
|
hostname: string;
|
|
port: number;
|
|
username: string;
|
|
password: string;
|
|
};
|
|
|
|
/**
|
|
* Wraps emailing process.
|
|
*/
|
|
export class Emailer {
|
|
private sender_address;
|
|
private client_options;
|
|
|
|
public constructor(config: EmailerConfig) {
|
|
const client_options: ClientOptions = {
|
|
connection: {
|
|
hostname: config.hostname,
|
|
auth: {
|
|
username: config.username,
|
|
password: config.password,
|
|
},
|
|
port: config.port,
|
|
tls: true,
|
|
},
|
|
};
|
|
this.client_options = client_options;
|
|
this.sender_address = config.username;
|
|
}
|
|
|
|
public async send_confirmation_mail(discord_username: string, cri_email: string, link: string) {
|
|
await this.send(cri_email, CONFIRMATION_EMAIL_SUBJECT, confirmation_email_body(discord_username, link));
|
|
}
|
|
|
|
private async send(to_email: string, subject: string, content: string) {
|
|
const client = new SMTPClient(this.client_options);
|
|
const from = this.sender_address;
|
|
const to = to_email;
|
|
await client.send({ from, to, subject, html: content });
|
|
await client.close();
|
|
log(`Sent an email to '${to_email}'.`);
|
|
}
|
|
}
|
|
|
|
const CONFIRMATION_EMAIL_SUBJECT = "Confirmation d'association à un compte discord";
|
|
function confirmation_email_body(discord_username: string, link: string) {
|
|
return `<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head> <meta charset="UTF-8"> </head>
|
|
<body>
|
|
<pre>
|
|
Bonjour,
|
|
Ceci est un message automatique de confirmation pour l'association
|
|
du compte discord '${discord_username}' à cet email.
|
|
|
|
Pour terminer l'association, veuillez suivre sur le lien suivant :
|
|
<a href="${link}">${link}</a>
|
|
|
|
Si vous n'êtes pas à l'origine de cette demande, veuillez ignorer ce message.
|
|
|
|
|
|
---
|
|
Je suis un robot et cette action à été effectuée automatiquement.
|
|
Vous pouvez contacter le développeur de se service à l'email matthieu at imagevo dot fr.
|
|
</pre>
|
|
</body>
|
|
</html>
|
|
`;
|
|
}
|