SMAM (short for Send Me A Mail) is a free (as in freedom) contact form embedding software.

server.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict"
  2. var pug = require('pug');
  3. var nodemailer = require('nodemailer');
  4. var settings = require('./settings');
  5. // Web server
  6. var bodyParser = require('body-parser');
  7. var express = require('express');
  8. var app = express();
  9. // Logging
  10. var printit = require('printit');
  11. var log = printit({
  12. prefix: 'SMAM',
  13. date: true
  14. });
  15. var transporter = nodemailer.createTransport(settings.mailserver);
  16. // Serve static (JS + HTML) files
  17. app.use(express.static('front'));
  18. // Body parsing
  19. app.use(bodyParser.urlencoded({ extended: true }));
  20. app.use(bodyParser.json());
  21. // A request on /send with user input = mail to be sent
  22. app.post('/send', function(req, res, next) {
  23. // params will be used as:
  24. // - values for html generation from the pug template
  25. // - parameters for sending the mail(s)
  26. let params = {
  27. subject: req.body.subj,
  28. from: req.body.name + ' <' + req.body.addr + '>',
  29. html: req.body.text
  30. };
  31. // Replacing the mail's content with HTML from the pug template
  32. // Commenting the line below will bypass the generation and only user the
  33. // text entered by the user
  34. params.html = pug.renderFile('template.pug', params);
  35. log.info('Sending message from ' + content.from);
  36. // Send the email to all users
  37. sendMails(params, function(err, infos) {
  38. if(err) {
  39. log.error(err)
  40. }
  41. logStatus(infos);
  42. }, function() {
  43. res.header('Access-Control-Allow-Origin', '*');
  44. res.status(200).send();
  45. })
  46. });
  47. // Use either the default port or the one chosen by the user (PORT env variable)
  48. var port = process.env.PORT || 1970;
  49. // Start the server
  50. app.listen(port, function() {
  51. log.info("Server started on port " + port);
  52. });
  53. // Send mails to the recipients specified in the JSON settings file
  54. // content: object containing mail params
  55. // {
  56. // subject: String
  57. // from: String (following RFC 1036 (https://tools.ietf.org/html/rfc1036#section-2.1.1))
  58. // html: String
  59. // }
  60. // update(next, infos): Called each time a mail is sent with the infos provided
  61. // by nodemailer
  62. // done(): Called once each mail has been sent
  63. function sendMails(params, update, done) {
  64. let mails = settings.recipients.map((recipient) => {
  65. return new Promise((sent) => {
  66. params.to = recipient;
  67. transporter.sendMail(params, (err, infos) => {
  68. sent();
  69. if(err) {
  70. return next(err, recipient);
  71. }
  72. next(null, infos);
  73. });
  74. });
  75. });
  76. Promise.all(mails).then(done);
  77. }
  78. // Produces log from the infos provided by nodemailer
  79. // infos: infos provided by nodemailer
  80. // return: nothing
  81. function logStatus(infos) {
  82. if(infos.accepted.length) {
  83. log.info('Message sent to ' + status.accepted[0]);
  84. }
  85. if(infos.rejected.length) {
  86. log.info('Message failed to send to ' + status.rejected[0]);
  87. }
  88. }