Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as nm from 'nodemailer'
- import config from '../config/environment'
- class Mailer {
- private mailConfig: nm.SentMessageInfo
- private mailOptions: nm.SendMailOptions
- private isDev: boolean = false
- private devEnv: string[] = ['development', 'test', 'preproduction']
- public constructor() {
- if (this.devEnv.indexOf(config.env) !== -1) {
- this.isDev = true
- }
- }
- public send = (
- to: string[],
- subject: string,
- template: string
- ): void | string[] => {
- const dest: string = to.join(',')
- const text: string = template // TODO : create and find actual template
- if (this.isDev) {
- this.sendDev(dest, subject, text)
- } else {
- this.sendProd(dest, subject, text)
- }
- }
- private sendDev = (
- dest: string,
- subject: string,
- text: string
- ): void | string[] => {
- nm.createTestAccount((err: Error, account: nm.TestAccount) => {
- this.mailConfig = this.setMailConfig(account)
- const transporter: nm.Transporter = nm.createTransport(this.mailConfig)
- this.mailOptions = this.setMailOptions(dest, subject, text)
- transporter.sendMail(
- this.mailOptions,
- (error: Error, info: nm.SentMessageInfo)
- => {
- // no empty block
- })
- })
- }
- private sendProd = (
- dest: string,
- subject: string,
- text: string
- ): void | string[] => {
- this.mailConfig = this.setMailConfig()
- this.mailOptions = this.setMailOptions(dest, subject, text)
- const transporter: nm.Transporter = nm.createTransport(this.mailConfig)
- transporter.sendMail(
- this.mailOptions,
- (error: Error, info: nm.SentMessageInfo)
- => {
- // No empty block
- })
- }
- private setMailConfig = (
- account: nm.TestAccount = null
- ): nm.SentMessageInfo => {
- const conf: nm.SentMessageInfo = {
- host: config.mail.host,
- port: 587,
- auth: {
- user: null,
- pass: null
- }
- }
- if (this.isDev) {
- conf.auth.user = account.user
- conf.auth.pass = account.pass
- } else {
- conf.auth.user = config.mail.username
- conf.auth.pass = config.mail.password
- }
- return conf
- }
- private setMailOptions = (
- dest: string,
- subject: string,
- text: string
- ): nm.SendMailOptions => {
- return {
- from: '"Night Vision 👻" <contact@night-vision.com>',
- to: dest,
- subject: subject,
- html: text
- }
- }
- }
- export default new Mailer()
- {
- "compilerOptions": {
- "target": "es2017",
- "module": "commonjs",
- "sourceMap": true,
- "removeComments": true,
- "outDir": "./dist/",
- "pretty": true
- }
- }
- {
- "comment-format": [
- true,
- "check-space"
- ],
- "extends": "tslint:latest",
- "rulesDirectory": [],
- "rules": {
- "no-implicit-dependencies": false,
- "arrow-parens": false,
- "class-name": true,
- "forin": true,
- "deprecation": true,
- "curly": true,
- "eofline": true,
- "indent": [
- true,
- "spaces",
- 4
- ],
- "no-console": [
- true,
- "debug",
- "info",
- "time",
- "timeEnd",
- "trace",
- "log"
- ],
- "only-arrow-functions": true,
- "max-line-length": [
- true,
- 100
- ],
- "member-ordering": [
- true,
- {
- "order": [
- "public-before-private",
- "static-before-instance",
- "variables-before-functions"
- ]
- }
- ],
- "typedef": [
- true,
- "call-signature",
- "arrow-call-signature",
- "parameter",
- "arrow-parameter",
- "property-declaration",
- "member-variable-declaration",
- "variable-declaration"
- ],
- "no-arg": true,
- "no-construct": true,
- "no-duplicate-variable": true,
- "no-duplicate-imports": true,
- "no-unused-variable": true,
- "no-non-null-assertion": true,
- "no-shadowed-variable": true,
- "no-string-literal": false,
- "no-string-throw": true,
- "no-switch-case-fall-through": true,
- "no-empty": true,
- "no-eval": true,
- "no-namespace": false,
- "no-trailing-whitespace": true,
- "no-unused-expression": true,
- "linebreak-style": [
- true,
- "LF"
- ],
- "one-line": [
- true,
- "check-open-brace",
- "check-catch",
- "check-else",
- "check-whitespace"
- ],
- "ordered-imports": false,
- "quotemark": [
- true,
- "single"
- ],
- "semicolon": [
- true,
- "never"
- ],
- "space-before-function-paren": false,
- "object-literal-shorthand": [
- true,
- "never"
- ],
- "trailing-comma": true,
- "triple-equals": true,
- "variable-name": [
- true,
- "ban-keywords"
- ],
- "object-literal-sort-keys": false,
- "cyclomatic-complexity": true,
- "whitespace": [
- true,
- "check-branch",
- "check-operator",
- "check-typecast"
- ]
- }
- }
Add Comment
Please, Sign In to add comment