implements web verification

This commit is contained in:
Matthieu Jolimaitre 2024-02-02 16:45:08 +01:00
parent a7799eb3b1
commit 440e8bf324
9 changed files with 303 additions and 28 deletions

View file

@ -1,4 +1,6 @@
import { SmtpClient } from "https://deno.land/x/smtp@v0.7.0/mod.ts";
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;
@ -11,21 +13,61 @@ export type EmailerConfig = {
* Wraps emailing process.
*/
export class Emailer {
private client;
private config;
private sender_email;
private sender_address;
private client_options;
public constructor(config: EmailerConfig, sender_email: string) {
this.config = config;
this.sender_email = sender_email;
this.client = new SmtpClient();
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(to_email: string, subject: string, content: string) {
const from = this.sender_email;
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 this.client.connectTLS(this.config);
await this.client.send({ from, to, subject, content });
await this.client.close();
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 à é effectuée automatiquement.
Vous pouvez contacter le développeur de se service à l'email matthieu at imagevo dot fr.
</pre>
</body>
</html>
`;
}