mirror of
https://github.com/nodemailer/wildduck.git
synced 2025-03-01 02:15:15 +08:00
Allow linking images to initial email HTML
This commit is contained in:
parent
d7741e2c0c
commit
2f516612ea
4 changed files with 39 additions and 1 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -4,4 +4,8 @@ npm-debug.log
|
|||
.npmrc
|
||||
config/production.*
|
||||
config/development.*
|
||||
|
||||
emails/*
|
||||
!emails/README.md
|
||||
!emails/example.*
|
||||
emails/example.json
|
||||
|
|
|
@ -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
|
||||
<img src="/path/to/duck.png"> <!-- BAD, path is not allowed -->
|
||||
<img src="duck.png"> <!-- GOOD, canonical attachment name -->
|
||||
<img src="example.duck.png"> <!-- GOOD, actual filename in folder -->
|
||||
<img src="./duck.png"> <!-- Not GOOD but works as leading ./ is removed from the filename -->
|
||||
```
|
||||
|
|
|
@ -92,7 +92,7 @@ body {
|
|||
</tr>
|
||||
<tr style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
|
||||
<td class="content-block" style="font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
|
||||
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!
|
||||
<img src="duck.png" />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!
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
|
23
lib/tools.js
23
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,
|
||||
|
|
Loading…
Reference in a new issue