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

server.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var pug = require('pug');
  2. var nodemailer = require('nodemailer');
  3. var settings = require('./settings');
  4. // Web server
  5. var bodyParser = require('body-parser');
  6. var express = require('express');
  7. var app = express();
  8. // Logging
  9. var printit = require('printit');
  10. var log = printit({
  11. prefix: 'SMAM',
  12. date: true
  13. });
  14. // nodemailer initial configuration
  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. // Promise for each recipient to send each mail asynchronously
  66. return new Promise((sent) => {
  67. params.to = recipient;
  68. // Send the email
  69. transporter.sendMail(params, (err, infos) => {
  70. // Promise callback
  71. sent();
  72. if(err) {
  73. return next(err, recipient);
  74. }
  75. next(null, infos);
  76. });
  77. });
  78. });
  79. // Run all the promises (= send all the mails)
  80. Promise.all(mails).then(done);
  81. }
  82. // Produces log from the infos provided by nodemailer
  83. // infos: infos provided by nodemailer
  84. // return: nothing
  85. function logStatus(infos) {
  86. if(infos.accepted.length) {
  87. log.info('Message sent to ' + status.accepted[0]);
  88. }
  89. if(infos.rejected.length) {
  90. log.info('Message failed to send to ' + status.rejected[0]);
  91. }
  92. }