Guest User

Untitled

a guest
Sep 23rd, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. var nodemailer = require('nodemailer');
  2. var bodyParser = require('body-parser');
  3. var express = require('express');
  4. var app = express();
  5.  
  6. app.use(bodyParser.urlencoded({
  7. extended: true
  8. }));
  9.  
  10. app.post('/contact', function(req, res) {
  11. var mailOpts, smtpConfig;
  12. //Setup Nodemailer transport, I chose gmail. Create an application-specific password to avoid problems.
  13. smtpConfig = nodemailer.createTransport('SMTP', {
  14. service: 'Gmail',
  15. auth: {
  16. user: "<myUser>",
  17. pass: "<myPassword>"
  18. }
  19. });
  20. //Mail options
  21. mailOpts = {
  22. from: req.query.name + ' <' + req.query.email + '>',
  23. //grab form data from the request body object
  24. to: '<other user>',
  25. subject: 'Website contact form',
  26. text: req.query.message
  27. };
  28. smtpConfig.sendMail(mailOpts, function(error, response) {
  29. //Email not sent
  30. if (error) {
  31. res.end("Email send failed");
  32. //res.render('contact', { title: 'Raging Flame Laboratory - Contact', msg: 'Error occured, message not sent.', err: true, page: 'contact' })
  33. //console.log("error");
  34. }//Yay!! Email sent
  35. else {
  36. res.end("Email send successfully");
  37. //res.render('contact', { title: 'Raging Flame Laboratory - Contact', msg: 'Message sent! Thank you.', err: false, page: 'contact' })
  38. //console.log("success");
  39. }
  40. });
  41. });
  42.  
  43. app.listen(8081, function() {
  44. console.log('Server running at http://127.0.0.1:8081/');
  45. });
  46.  
  47. <form action="http://127.0.0.1:8087/contact" method="post">
  48. <b>send us a quote</b>
  49. </br>
  50. <input type="text" name="name" id="name" value="Name">
  51. </br>
  52. <!--input type="text" name="bname" id="bname" value="Business Name"></br>-->
  53. <input type="text" name="email" id="email" value="Email Address">
  54. </br>
  55. <textarea name="message" id="message" cols="30" rows="10">Enter detailed information here</textarea>
  56. </br>
  57. <input type="submit" name="Submit" id="Submit" value="send message">
  58. </form>
  59.  
  60. app.use(bodyParser.json());
  61.  
  62. app.use(bodyParser.urlencoded({ extended: true }));
  63.  
  64. var nodemailer = require('nodemailer');
  65.  
  66. // Create a SMTP transport object
  67. var transport = nodemailer.createTransport("SMTP", {
  68. service: 'Hotmail',
  69. auth: {
  70. user: "username",
  71. pass: "password"
  72. }
  73. });
  74.  
  75. console.log('SMTP Configured');
  76.  
  77. // Message object
  78. var message = {
  79.  
  80. // sender info
  81. from: from,
  82.  
  83. // Comma separated list of recipients
  84. to: req.query.to ,
  85.  
  86. // Subject of the message
  87. subject:req.query.subject //'Nodemailer is unicode friendly ✔',
  88.  
  89. // plaintext body
  90. text: req.query.text //'Hello to myself!',
  91.  
  92. // HTML body
  93. /* html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
  94. '<p>Here's a nyan cat for you as an embedded attachment:<br/></p>'*/
  95. };
  96.  
  97. console.log('Sending Mail');
  98. transport.sendMail(message, function(error){
  99. if(error){
  100. console.log('Error occured');
  101. console.log(error.message);
  102. return;
  103. }
  104. console.log('Message sent successfully!');
  105.  
  106. // if you don't want to use this transport object anymore,uncomment
  107. //transport.close(); // close the connection pool
  108. });
Add Comment
Please, Sign In to add comment