62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { singleton } from 'decorators/singleton'
|
|
import { createTransport, Transporter } from 'nodemailer'
|
|
import Mail from 'nodemailer/lib/mailer'
|
|
import FormData from 'form-data'
|
|
import Mailgun, { InputFormData } from 'mailgun.js'
|
|
import { ZError } from 'common/ZError'
|
|
import * as aws from '@aws-sdk/client-ses'
|
|
|
|
@singleton
|
|
export class MailService {
|
|
private transporter: Transporter
|
|
private mailClient: any
|
|
private awsClient: any
|
|
constructor() {
|
|
// const options = {
|
|
// host: process.env.MAIL_SMTP_HOST,
|
|
// secure: true,
|
|
// auth: {
|
|
// user: process.env.MAIL_SMTP_USER,
|
|
// pass: process.env.MAIL_SMTP_PASS,
|
|
// },
|
|
// logger: true,
|
|
// debug: false,
|
|
// }
|
|
// // @ts-ignore
|
|
// this.transporter = createTransport(options, {})
|
|
|
|
const mailgun = new Mailgun(FormData)
|
|
this.mailClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY })
|
|
const ses = new aws.SES({
|
|
region: 'ap-southeast-1', // Your region will need to be updated
|
|
credentials: {
|
|
accessKeyId: process.env.AWS_ACCESS_KEY,
|
|
secretAccessKey: process.env.AWS_SECRET_KEY,
|
|
},
|
|
})
|
|
this.transporter = createTransport({
|
|
SES: { ses, aws },
|
|
})
|
|
}
|
|
|
|
public async send(message: Mail.Options) {
|
|
await this.transporter.verify()
|
|
return this.transporter.sendMail(message)
|
|
}
|
|
|
|
public async sendMailgun(message: Mail.Options) {
|
|
const domain = 'counterfire.games'
|
|
const sendResult = await this.mailClient.messages.create(domain, {
|
|
from: message.from,
|
|
to: message.to,
|
|
subject: message.subject,
|
|
html: message.html,
|
|
text: message.text,
|
|
})
|
|
if (sendResult.status !== 200) {
|
|
throw new ZError(20, sendResult.message)
|
|
}
|
|
return { messageId: sendResult.id }
|
|
}
|
|
}
|