37 lines
1018 B
TypeScript
37 lines
1018 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import Mailgun from 'mailgun.js';
|
|
import * as formdata from 'form-data';
|
|
import { IMailgunClient } from 'mailgun.js/Interfaces';
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
@Injectable()
|
|
export class EmailService {
|
|
private readonly mailgun: IMailgunClient | undefined;
|
|
private static readonly emailFromDomain = 'hello.hooli.co';
|
|
|
|
constructor(private readonly configService: ConfigService) {
|
|
const mailgun = new Mailgun(formdata);
|
|
const hooliKey = this.configService.get<string>('mailgun.hooliKey');
|
|
if (!hooliKey) {
|
|
return;
|
|
}
|
|
this.mailgun = mailgun.client({
|
|
username: 'api',
|
|
key: hooliKey,
|
|
});
|
|
}
|
|
|
|
async sendEmail(to: string[], subject: string, text: string) {
|
|
if (!this.mailgun) {
|
|
return;
|
|
}
|
|
return this.mailgun.messages.create(EmailService.emailFromDomain, {
|
|
from: `HooliMail <hooli-mail@${EmailService.emailFromDomain}>`,
|
|
to,
|
|
subject,
|
|
text,
|
|
html: text,
|
|
});
|
|
}
|
|
}
|