smartsmtp/ts/index.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-08-12 14:13:20 +00:00
import * as plugins from './smartsmtp.plugins';
export interface ISmartSmtpOptions {
smtpServer: string;
smtpUser: string;
smtpPassword: string;
}
/**
* Use it to send mails via smtp
*/
export class Smartsmtp {
public nodemailerTransport: plugins.nodemailer.Transporter;
constructor(optionsArg: ISmartSmtpOptions) {
this.nodemailerTransport = plugins.nodemailer.createTransport({
host: optionsArg.smtpServer,
port: 465,
secure: true, // upgrade later with STARTTLS
auth: {
user: optionsArg.smtpUser,
2020-08-12 15:09:41 +00:00
pass: optionsArg.smtpPassword,
},
2020-08-12 14:13:20 +00:00
});
}
/**
* sends a SmartMail
*/
public async sendSmartMail(
smartmailArg: plugins.smartmail.Smartmail<any>,
toArg: string,
dataArg = {}
) {
2020-08-15 12:58:01 +00:00
const message: plugins.nodemailer.SendMailOptions = {
2020-08-12 14:13:20 +00:00
from: smartmailArg.options.from,
to: toArg,
subject: smartmailArg.getSubject(dataArg),
text: smartmailArg.getBody(dataArg),
2020-08-12 15:09:41 +00:00
html: smartmailArg.getBody(dataArg),
2020-08-15 12:58:54 +00:00
attachments: [],
2020-08-12 14:13:20 +00:00
};
2020-08-15 12:58:01 +00:00
// lets add attachments from smartmailArg
for (const attachment of smartmailArg.attachments) {
message.attachments.push({
filename: attachment.parsedPath.base,
2020-08-15 12:58:54 +00:00
content: attachment.contentBuffer,
2020-08-15 12:58:01 +00:00
});
}
2020-08-12 14:13:20 +00:00
const response = await this.nodemailerTransport.sendMail(message);
return response;
}
2020-08-12 15:09:41 +00:00
}