diff --git a/.gitignore b/.gitignore
index 44be061b..943173e1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,8 @@ npm-debug.log
.npmrc
config/production.*
config/development.*
+
+emails/*
+!emails/README.md
+!emails/example.*
emails/example.json
diff --git a/emails/README.md b/emails/README.md
index 10393cfd..012805b8 100644
--- a/emails/README.md
+++ b/emails/README.md
@@ -35,3 +35,14 @@ You can include some resources as external files by using the same name prefix a
- **name.html** or **name.htm** is the HTML content of the message. If this file exists then it sets or overrides the `html` property in message json structure
- **name.text** or **name.txt** is the plaintext content of the message. If this file exists then it sets or overrides the `text` property in message json structure
- **name.filename.ext** is included in the message as an attachment
+
+### Embedded images
+
+You can link the attachment files to HTML as images. For this either use the canonical name of the attachment (eg. "duck.png") or the filename of the attachment in the emails folder (eg "example.duck.png"). Make sure that the URL used in HTML does not use full path, it must point to the current folder.
+
+```html
+
+
+
+
+```
diff --git a/emails/example.html b/emails/example.html
index 7f83cfb7..6e4dd30b 100644
--- a/emails/example.html
+++ b/emails/example.html
@@ -92,7 +92,7 @@ body {
- If you are seeing this message then it means you have reached the inbox of your new email address [EMAIL]. Be aware though that the service is in a constant change, so this address might disappear during the next database schema update. Don't start using it as your main email address!
+ If you are seeing this message then it means you have reached the inbox of your new email address [EMAIL]. Be aware though that the service is in a constant change, so this address might disappear during the next database schema update. Don't start using it as your main email address!
|
diff --git a/lib/tools.js b/lib/tools.js
index 5b7cc25a..dcbe15a7 100644
--- a/lib/tools.js
+++ b/lib/tools.js
@@ -6,6 +6,7 @@ const consts = require('./consts');
const fs = require('fs');
const he = require('he');
const pathlib = require('path');
+const crypto = require('crypto');
let templates = false;
@@ -219,6 +220,7 @@ function getEmailTemplates(tags, callback) {
if (pos >= files.length) {
let newTemplates = Array.from(filesMap)
.map(entry => {
+ let name = escapeRegexStr(entry[0]);
entry = entry[1];
if (!entry.message) {
return false;
@@ -234,6 +236,22 @@ function getEmailTemplates(tags, callback) {
if (entry.attachments) {
entry.message.attachments = [].concat(entry.message.attachments || []).concat(entry.attachments);
+
+ if (entry.message.html) {
+ entry.message.attachments.forEach(attachment => {
+ if (entry.message.html.indexOf(attachment.filename) >= 0) {
+ // replace html image link with a link to the attachment
+ let fname = escapeRegexStr(attachment.filename);
+ entry.message.html = entry.message.html.replace(
+ new RegExp('(["\'])(?:.\\/)?(?:' + name + '.)?' + fname + '(?=["\'])', 'g'),
+ (m, p) => {
+ attachment.cid = attachment.cid || crypto.randomBytes(8).toString('hex') + '@wildduck.email';
+ return p + 'cid:' + attachment.cid;
+ }
+ );
+ }
+ });
+ }
}
if (entry.text) {
@@ -317,6 +335,11 @@ function getEmailTemplates(tags, callback) {
});
}
+function escapeRegexStr(string) {
+ let specials = ['-', '[', ']', '/', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'];
+ return string.replace(RegExp('[' + specials.join('\\') + ']', 'g'), '\\$&');
+}
+
module.exports = {
normalizeAddress,
redisConfig,