Guest User

Untitled

a guest
Oct 12th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import * as nm from 'nodemailer'
  2. import config from '../config/environment'
  3.  
  4. class Mailer {
  5.  
  6. private mailConfig: nm.SentMessageInfo
  7. private mailOptions: nm.SendMailOptions
  8. private isDev: boolean = false
  9. private devEnv: string[] = ['development', 'test', 'preproduction']
  10.  
  11. public constructor() {
  12. if (this.devEnv.indexOf(config.env) !== -1) {
  13. this.isDev = true
  14. }
  15. }
  16.  
  17. public send = (
  18. to: string[],
  19. subject: string,
  20. template: string
  21. ): void | string[] => {
  22. const dest: string = to.join(',')
  23. const text: string = template // TODO : create and find actual template
  24. if (this.isDev) {
  25. this.sendDev(dest, subject, text)
  26. } else {
  27. this.sendProd(dest, subject, text)
  28. }
  29. }
  30.  
  31. private sendDev = (
  32. dest: string,
  33. subject: string,
  34. text: string
  35. ): void | string[] => {
  36. nm.createTestAccount((err: Error, account: nm.TestAccount) => {
  37. this.mailConfig = this.setMailConfig(account)
  38. const transporter: nm.Transporter = nm.createTransport(this.mailConfig)
  39. this.mailOptions = this.setMailOptions(dest, subject, text)
  40. transporter.sendMail(
  41. this.mailOptions,
  42. (error: Error, info: nm.SentMessageInfo)
  43. => {
  44. // no empty block
  45. })
  46. })
  47. }
  48.  
  49. private sendProd = (
  50. dest: string,
  51. subject: string,
  52. text: string
  53. ): void | string[] => {
  54. this.mailConfig = this.setMailConfig()
  55. this.mailOptions = this.setMailOptions(dest, subject, text)
  56. const transporter: nm.Transporter = nm.createTransport(this.mailConfig)
  57. transporter.sendMail(
  58. this.mailOptions,
  59. (error: Error, info: nm.SentMessageInfo)
  60. => {
  61. // No empty block
  62. })
  63. }
  64.  
  65. private setMailConfig = (
  66. account: nm.TestAccount = null
  67. ): nm.SentMessageInfo => {
  68. const conf: nm.SentMessageInfo = {
  69. host: config.mail.host,
  70. port: 587,
  71. auth: {
  72. user: null,
  73. pass: null
  74. }
  75. }
  76. if (this.isDev) {
  77. conf.auth.user = account.user
  78. conf.auth.pass = account.pass
  79. } else {
  80. conf.auth.user = config.mail.username
  81. conf.auth.pass = config.mail.password
  82. }
  83. return conf
  84. }
  85.  
  86. private setMailOptions = (
  87. dest: string,
  88. subject: string,
  89. text: string
  90. ): nm.SendMailOptions => {
  91. return {
  92. from: '"Night Vision 👻" <contact@night-vision.com>',
  93. to: dest,
  94. subject: subject,
  95. html: text
  96. }
  97. }
  98. }
  99.  
  100. export default new Mailer()
  101.  
  102. {
  103. "compilerOptions": {
  104. "target": "es2017",
  105. "module": "commonjs",
  106. "sourceMap": true,
  107. "removeComments": true,
  108. "outDir": "./dist/",
  109. "pretty": true
  110. }
  111. }
  112.  
  113. {
  114. "comment-format": [
  115. true,
  116. "check-space"
  117. ],
  118. "extends": "tslint:latest",
  119. "rulesDirectory": [],
  120. "rules": {
  121. "no-implicit-dependencies": false,
  122. "arrow-parens": false,
  123. "class-name": true,
  124. "forin": true,
  125. "deprecation": true,
  126. "curly": true,
  127. "eofline": true,
  128. "indent": [
  129. true,
  130. "spaces",
  131. 4
  132. ],
  133. "no-console": [
  134. true,
  135. "debug",
  136. "info",
  137. "time",
  138. "timeEnd",
  139. "trace",
  140. "log"
  141. ],
  142. "only-arrow-functions": true,
  143. "max-line-length": [
  144. true,
  145. 100
  146. ],
  147. "member-ordering": [
  148. true,
  149. {
  150. "order": [
  151. "public-before-private",
  152. "static-before-instance",
  153. "variables-before-functions"
  154. ]
  155. }
  156. ],
  157. "typedef": [
  158. true,
  159. "call-signature",
  160. "arrow-call-signature",
  161. "parameter",
  162. "arrow-parameter",
  163. "property-declaration",
  164. "member-variable-declaration",
  165. "variable-declaration"
  166. ],
  167. "no-arg": true,
  168. "no-construct": true,
  169. "no-duplicate-variable": true,
  170. "no-duplicate-imports": true,
  171. "no-unused-variable": true,
  172. "no-non-null-assertion": true,
  173. "no-shadowed-variable": true,
  174. "no-string-literal": false,
  175. "no-string-throw": true,
  176. "no-switch-case-fall-through": true,
  177. "no-empty": true,
  178. "no-eval": true,
  179. "no-namespace": false,
  180. "no-trailing-whitespace": true,
  181. "no-unused-expression": true,
  182. "linebreak-style": [
  183. true,
  184. "LF"
  185. ],
  186. "one-line": [
  187. true,
  188. "check-open-brace",
  189. "check-catch",
  190. "check-else",
  191. "check-whitespace"
  192. ],
  193. "ordered-imports": false,
  194. "quotemark": [
  195. true,
  196. "single"
  197. ],
  198. "semicolon": [
  199. true,
  200. "never"
  201. ],
  202. "space-before-function-paren": false,
  203. "object-literal-shorthand": [
  204. true,
  205. "never"
  206. ],
  207. "trailing-comma": true,
  208. "triple-equals": true,
  209. "variable-name": [
  210. true,
  211. "ban-keywords"
  212. ],
  213. "object-literal-sort-keys": false,
  214. "cyclomatic-complexity": true,
  215. "whitespace": [
  216. true,
  217. "check-branch",
  218. "check-operator",
  219. "check-typecast"
  220. ]
  221. }
  222. }
Add Comment
Please, Sign In to add comment