This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dad4ebe
commit aa47728
Showing
8 changed files
with
489 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Address } from 'nodemailer/lib/mailer'; | ||
import SMTPTransport from 'nodemailer/lib/smtp-transport'; | ||
|
||
export interface NodemailerConfig { | ||
transport?: SMTPTransport | SMTPTransport.Options | string, | ||
defaults?: SMTPTransport.Options, | ||
sender?: string | Address, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Constant, Injectable } from "@tsed/di"; | ||
import { NodemailerConfig } from "./Nodemailer.config"; | ||
import * as nodemailer from 'nodemailer'; | ||
import SMTPTransport from "nodemailer/lib/smtp-transport"; | ||
import Mail from "nodemailer/lib/mailer"; | ||
import moment from "moment"; | ||
|
||
@Injectable() | ||
export class NodemailerService { | ||
@Constant("nodemailer") | ||
config: NodemailerConfig; | ||
|
||
private _transporter: nodemailer.Transporter<SMTPTransport.SentMessageInfo>; | ||
|
||
private _testAccount: nodemailer.TestAccount; | ||
|
||
|
||
constructor() { | ||
if (!this.config?.transport) { | ||
// Generate test SMTP service account from ethereal.email | ||
// Only needed if you don't have a real mail account for testing | ||
nodemailer.createTestAccount((err, testAccount) => { | ||
if (err) { | ||
return console.error(err); | ||
} | ||
this._testAccount = testAccount; | ||
console.log(testAccount); | ||
// create reusable transporter object using the default SMTP transport | ||
this._transporter = nodemailer.createTransport({ | ||
host: testAccount.smtp.host, | ||
port: testAccount.smtp.port, | ||
secure: testAccount.smtp.secure, | ||
auth: { | ||
user: testAccount.user, // generated ethereal user | ||
pass: testAccount.pass, // generated ethereal password | ||
}, | ||
}); | ||
this.transporterVerify(); | ||
this.sendTestMail(); | ||
}); | ||
} else { | ||
this._transporter = nodemailer.createTransport( | ||
this.config.transport, | ||
this.config.defaults | ||
); | ||
this.transporterVerify(); | ||
} | ||
} | ||
|
||
private transporterVerify() { | ||
this._transporter.verify((error) => { | ||
if (error) { | ||
return console.error(error); | ||
} | ||
console.log(`${moment().format('YYYY-MM-DD HH:mm:ss')} Nodemailer: Server is ready to take our messages`); | ||
}) | ||
} | ||
|
||
public async sendTestMail() { | ||
// send mail with defined transport object | ||
let info = await this._transporter.sendMail({ | ||
from: `"Fred Foo 👻" <${this._testAccount.user}>`, // sender address | ||
to: "matuska.lukas@lukasmatuska.cz", // list of receivers | ||
subject: "Hello ✔", // Subject line | ||
text: "Hello world?", // plain text body | ||
html: "<b>Hello world?</b>", // html body | ||
}); | ||
|
||
console.log("Message sent: %s", info.messageId); | ||
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com> | ||
|
||
// Preview only available when sending through an Ethereal account | ||
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); | ||
} | ||
|
||
public async send(id: string, callback: Function) { | ||
// send mail with defined transport object | ||
let info = await this._transporter.sendMail({ | ||
from: this.config.sender, // sender address | ||
to: "matuska.lukas@lukasmatuska.cz", // list of receivers | ||
subject: "Hello ✔", // Subject line | ||
text: "Hello world?", // plain text body | ||
html: "<b>Hello world?</b>", // html body | ||
}); | ||
|
||
console.log("Message sent: %s", info.messageId); | ||
|
||
// Preview only available when sending through an Ethereal account | ||
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); | ||
} | ||
|
||
/*public send(mailOptions: Mail.Options) { | ||
this._transporter.sendMail(mailOptions); | ||
}*/ | ||
|
||
} |