瀏覽代碼

Merge pull request #17 from babolivier/fix/default-template

Add default template and fallback to it if no template found
Brendan Abolivier 6 年之前
父節點
當前提交
cda4aaff1f
沒有帳戶連結到提交者的電子郵件
共有 2 個文件被更改,包括 61 次插入26 次删除
  1. 3
    1
      README.md
  2. 58
    25
      server.js

+ 3
- 1
README.md 查看文件

@@ -145,7 +145,7 @@ A custom field is defined in the `customFields` section of your settings file, a
145 145
 
146 146
 ## Templating
147 147
 
148
-Each e-mail sent by the form follows a template described in `template.pug` (it's [Pug](pugjs.org/)). If you want to change the way the e-mails you receive are displayed in your mailbox, just edit it! You don't even need to restart the server aftewards :smile:
148
+Each e-mail sent by the form follows a template described in `template.example.pug` (it's [Pug](pugjs.org/)). If you want to change the way the e-mails you receive are displayed in your mailbox, just edit it! You don't even need to restart the server aftewards :smile:
149 149
 
150 150
 The template also features custom fields, iterating over the `custom` object, containing the field's label and user-input value:
151 151
 
@@ -156,6 +156,8 @@ The template also features custom fields, iterating over the `custom` object, co
156 156
 }
157 157
 ```
158 158
 
159
+The template needs to be named `template.pug`. If no template could be found under this name, SMAM will use a default one, which features both default and custom fields, and should be sufficient for non-advanced usage.
160
+
159 161
 ## Personnalising
160 162
 
161 163
 As you might have already seen, the contact form is generated without any form of style except your browser's default one. But that doesn't meen that you have to add an ugly form to your site to receive contact e-mails, as every element has a specific id (beginning with the `form_` prefix), allowing you to use your own style on your contact form.

+ 58
- 25
server.js 查看文件

@@ -1,17 +1,18 @@
1
-var pug			= require('pug');
2
-var nodemailer  = require('nodemailer');
3
-var crypto		= require('crypto');
4
-var settings	= require('./settings');
1
+var pug = require('pug');
2
+var nodemailer = require('nodemailer');
3
+var crypto = require('crypto');
4
+var fs = require('fs');
5
+var settings = require('./settings');
5 6
 
6 7
 // Translation
7
-var locale		= require('./locales/' + settings.language);
8
-var lang		= locale.server;
8
+var locale = require('./locales/' + settings.language);
9
+var lang = locale.server;
9 10
 
10 11
 // Web server
11
-var bodyParser	= require('body-parser');
12
-var cors		= require('cors');
13
-var express		= require('express');
14
-var app			= express();
12
+var bodyParser = require('body-parser');
13
+var cors = require('cors');
14
+var express = require('express');
15
+var app = express();
15 16
 
16 17
 // Logging
17 18
 var printit = require('printit');
@@ -28,6 +29,28 @@ var transporter = nodemailer.createTransport(settings.mailserver);
28 29
 // Verification tokens
29 30
 var tokens = {};
30 31
 
32
+// Default template
33
+// JavaScript has no native way to handle multi-line strings, so we put our template
34
+// in a comment inside a function fro which we generate a string.
35
+// cf: https://tomasz.janczuk.org/2013/05/multi-line-strings-in-javascript-and.html
36
+var defaultTemplate = (function() {/*
37
+html
38
+	body
39
+		p.subj
40
+			span(style="font-weight:bold") Subject: 
41
+			span= subject
42
+		p.from
43
+			span(style="font-weight:bold") Sent from: 
44
+			span= replyTo
45
+		each field in custom
46
+			p.custom
47
+				span(style="font-weight:bold")= field.label + ': '
48
+				span= field.value
49
+		p.message
50
+			span(style="font-weight:bold") Message: 
51
+		
52
+		p= html
53
+*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
31 54
 
32 55
 // Serve static (JS + HTML) files
33 56
 app.use(express.static('front'));
@@ -104,23 +127,33 @@ app.post('/send', function(req, res, next) {
104 127
 	// Replacing the mail's content with HTML from the pug template
105 128
 	// Commenting the line below will bypass the generation and only user the
106 129
 	// text entered by the user
107
-	params.html = pug.renderFile('template.pug', params);
108
-
109
-	log.info(lang.log_sending, params.replyTo);
110
-
111
-	// Send the email to all users
112
-	sendMails(params, function(err, infos) {
130
+	fs.access('template.pug', function(err) {
131
+		// Checking if the template exists.
132
+		// If not, fallback to the default template.
133
+		// TODO: Parameterise the template file name.
113 134
 		if(err) {
114
-			log.error(err);
115
-		}
116
-		logStatus(infos);
117
-	}, function() {
118
-		if(status.failed === status.total) {
119
-			res.status(500).send();
135
+			params.html = pug.render(defaultTemplate, params);
120 136
 		} else {
121
-			res.status(200).send();
137
+			params.html = pug.renderFile('template.pug', params);
122 138
 		}
123
-	})
139
+
140
+		log.info(lang.log_sending, params.replyTo);
141
+
142
+		// Send the email to all users
143
+		sendMails(params, function(err, infos) {
144
+			if(err) {
145
+				log.error(err);
146
+			}
147
+			logStatus(infos);
148
+		}, function() {
149
+			if(status.failed === status.total) {
150
+				res.status(500).send();
151
+			} else {
152
+				res.status(200).send();
153
+			}
154
+		});
155
+	});
156
+
124 157
 });
125 158
 
126 159
 
@@ -334,4 +367,4 @@ function processCustom(custom) {
334 367
 	}
335 368
 
336 369
 	return fields;
337
-}
370
+}