Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. 'use strict'
  2.  
  3. function UserDAO(model) {
  4. this.model = model;
  5. }
  6.  
  7. UserDAO.prototype.email = function(callback) {
  8.  
  9. //TESTE de email
  10. var filePath = 'services/content/test.html';
  11. var fs = require('fs');
  12. var config = require('config');
  13. var mail = require('../services/email.service');
  14.  
  15. //TESTE de email
  16. fs.readFile(filePath, function (err, content)
  17. {
  18. if (err) {
  19. console.log('Failed to read email file template html, err = ' + err);
  20. callback('Failed to read email file template html, err = ' + err);
  21. }
  22. else {
  23. var url = config.get('smtpConfig.redirectUrl') + '/#/novo-usuario?token=' + '123456'
  24. var rendered = content.toString().replace('#link#', 'teste.com.br')
  25. .replace('#usuario#', 'eduardo uchida');
  26. mail.send('eduardo.massami@yahoo.com.br',
  27. 'Bem-vindo ao e-solutions da PAN Seguros',
  28. 'Acesse o link: ',
  29. rendered, function (err, info)
  30. {
  31. if (err) {
  32. console.log(err);
  33. callback(err)
  34. }
  35. else {
  36. console.log('E-Mail enviado: ' + info);
  37.  
  38. var retorno = { message: 'Usuário criado com sucesso!' };
  39. callback(null, retorno);
  40. }
  41. });
  42. }
  43. });
  44. //FIM TESTE email
  45. }
  46.  
  47. module.exports = function(mongoose) {
  48. var User = mongoose.model('User', {
  49. name: {type: String, required: true},
  50. username: {type: String, required: true},
  51. password: {type: String, required: true},
  52. email: {type: String, required: true},
  53. ra: {type: Number, required: false},
  54. tipo: {type: String, required: true}
  55. });
  56. return new UserDAO(User);
  57. }
  58.  
  59. var nodemailer = require("nodemailer"); //para enviar emails
  60. var smtpTransport = require('nodemailer-smtp-transport');
  61. var config = require('config');
  62.  
  63. function enviarEmail (to, subject, text, html, callback) {
  64.  
  65. //console.log("entrou no email.service.js");
  66.  
  67. var transporter = nodemailer.createTransport(smtpTransport({
  68. host: config.get('smtpConfig.host'),
  69. port: config.get('smtpConfig.port'),
  70. secure: config.get('smtpConfig.secure'),
  71. auth: {
  72. user: config.get('smtpConfig.auth.user'),
  73. pass: config.get('smtpConfig.auth.pass')
  74. }
  75. }));
  76.  
  77. // setup e-mail data with unicode symbols
  78. var mailOptions = {
  79. from: config.get('smtpConfig.from'),
  80. to: to,
  81. subject: subject,
  82. text: text,
  83. html: html
  84. };
  85.  
  86. //verify connection configuration
  87. transporter.verify(function(error, success) {
  88. if (error) {
  89. console.log(error);
  90. transporter.close();
  91. } else {
  92. console.log('Server is ready to take our messages');
  93. //send mail with defined transport object
  94. transporter.sendMail(mailOptions, function (error, info)
  95. {
  96. if (error) {
  97. return callback(error);
  98. }
  99. callback(null, info);
  100. });
  101. }
  102. });
  103. }
  104.  
  105. exports.send = function (to, subject, text, html, callback) {
  106. return enviarEmail(to, subject, text, html, callback);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement