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

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