BREAKING CHANGE(smartsmtp): now supports sending mails with sendmail
This commit is contained in:
55
ts/index.ts
55
ts/index.ts
@ -1,54 +1 @@
|
||||
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,
|
||||
pass: optionsArg.smtpPassword,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a SmartMail
|
||||
*/
|
||||
public async sendSmartMail(
|
||||
smartmailArg: plugins.smartmail.Smartmail<any>,
|
||||
toArg: string,
|
||||
dataArg = {}
|
||||
) {
|
||||
const message: plugins.nodemailer.SendMailOptions = {
|
||||
from: smartmailArg.options.from,
|
||||
to: toArg,
|
||||
subject: smartmailArg.getSubject(dataArg),
|
||||
text: smartmailArg.getBody(dataArg),
|
||||
html: smartmailArg.getBody(dataArg),
|
||||
attachments: [],
|
||||
};
|
||||
|
||||
// lets add attachments from smartmailArg
|
||||
for (const attachment of smartmailArg.attachments) {
|
||||
message.attachments.push({
|
||||
filename: attachment.parsedPath.base,
|
||||
content: attachment.contentBuffer,
|
||||
});
|
||||
}
|
||||
|
||||
const response = await this.nodemailerTransport.sendMail(message);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
export * from './smartsmtp.classes.smartsmtp';
|
69
ts/smartsmtp.classes.smartsmtp.ts
Normal file
69
ts/smartsmtp.classes.smartsmtp.ts
Normal file
@ -0,0 +1,69 @@
|
||||
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 static async createSmartsmtpWithRelay(optionsArg: ISmartSmtpOptions) {
|
||||
const nodemailerTransport = plugins.nodemailer.createTransport({
|
||||
host: optionsArg.smtpServer,
|
||||
port: 465,
|
||||
secure: true, // upgrade later with STARTTLS
|
||||
auth: {
|
||||
user: optionsArg.smtpUser,
|
||||
pass: optionsArg.smtpPassword,
|
||||
},
|
||||
});
|
||||
return new Smartsmtp(nodemailerTransport);
|
||||
}
|
||||
|
||||
public static async createSmartsmtpSendmail() {
|
||||
const nodemailerTransport = plugins.nodemailer.createTransport({
|
||||
sendmail: true,
|
||||
newline: 'unix'
|
||||
});
|
||||
return new Smartsmtp(nodemailerTransport);
|
||||
}
|
||||
|
||||
public nodemailerTransport: plugins.nodemailer.Transporter;
|
||||
constructor(transporterArg: plugins.nodemailer.Transporter) {
|
||||
this.nodemailerTransport = transporterArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a SmartMail
|
||||
*/
|
||||
public async sendSmartMail(
|
||||
smartmailArg: plugins.smartmail.Smartmail<any>,
|
||||
toArg: string,
|
||||
dataArg = {}
|
||||
) {
|
||||
const message: plugins.nodemailer.SendMailOptions = {
|
||||
from: smartmailArg.options.from,
|
||||
to: toArg,
|
||||
subject: smartmailArg.getSubject(dataArg),
|
||||
text: smartmailArg.getBody(dataArg),
|
||||
html: smartmailArg.getBody(dataArg),
|
||||
attachments: [],
|
||||
};
|
||||
|
||||
// lets add attachments from smartmailArg
|
||||
for (const attachment of smartmailArg.attachments) {
|
||||
message.attachments.push({
|
||||
filename: attachment.parsedPath.base,
|
||||
content: attachment.contentBuffer,
|
||||
});
|
||||
}
|
||||
|
||||
const response = await this.nodemailerTransport.sendMail(message).catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
return response;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user