Advertisement
Guest User

Untitled

a guest
Sep 14th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1.  
  2. var express = require('express')
  3. var bodyParser = require('body-parser')
  4. var request = require('request')
  5. var app = express()
  6. // npm install nodemailer nodemailer-smtp-pool
  7. var nodemailer = require('nodemailer');
  8. var smtpPool = require('nodemailer-smtp-pool');
  9.  
  10. app.set('port', (process.env.PORT || 3000))
  11.  
  12. // Process application/x-www-form-urlencoded
  13. app.use(bodyParser.urlencoded({extended: false}))
  14.  
  15. // Process application/json
  16. app.use(bodyParser.json())
  17.  
  18. // Index route
  19. app.get('/', function (req, res) {
  20. res.send('Hello world, I am a send mail')
  21. })
  22. // send email
  23. app.get('/sendmail', function(req, res, next){
  24. // Create a SMTP transporter object
  25. var transporter = nodemailer.createTransport(smtpPool({
  26. host: 'smtp.gmail.com',
  27. port: 465,
  28. secure: true,
  29. auth: {
  30. user: 'username', // change your username here
  31. pass: 'password' // change your password here
  32. },
  33. // use up to 5 parallel connections, default is 5
  34. maxConnections: 5,
  35. // do not send more than 10 messages per connection, default is 100
  36. maxMessages: 10,
  37. // no not send more than 5 messages in a second, default is no limit
  38. rateLimit: 5
  39. }));
  40.  
  41. console.log('SMTP Configured');
  42.  
  43. // Message object
  44. var message = {
  45.  
  46. // sender info
  47. from: 'Sender Name <sumn2u@gmail.com>',
  48.  
  49. // Comma separated list of recipients
  50. to: '"Receiver Name" <sumn2u@hotmail.com>',
  51.  
  52. // Subject of the message
  53. subject: 'Nodemailer is unicode friendly ✔', //
  54.  
  55. headers: {
  56. 'X-Laziness-level': 1000
  57. },
  58.  
  59. // plaintext body
  60. text: 'Hello to myself!',
  61.  
  62. // HTML body
  63. html: '<p><b>Hello</b> to myself <img src="cid:note@example.com"/></p>' +
  64. '<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:nyan@example.com"/></p>',
  65. };
  66.  
  67. console.log('Sending Mail');
  68. transporter.sendMail(message, function(error, info) {
  69. if (error) {
  70. console.log('Error occurred');
  71. res.send(error.message);
  72. console.log(error.message);
  73. return;
  74. }
  75. res.send({"message sent"});
  76. console.log('Message sent successfully!');
  77. console.log('Server responded with "%s"', info.response);
  78. });
  79.  
  80. });
  81.  
  82. // Spin up the server
  83. app.listen(app.get('port'), function() {
  84. console.log('running on port', app.get('port'))
  85. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement