Skip to content

Commit c3f75db

Browse files
committed
feat: added improved styling with tabs, added download original raw source button
1 parent 14f1267 commit c3f75db

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ const previewEmail = async (message, options) => {
4444
debug('message', message, 'options', options);
4545

4646
let raw;
47+
let base64;
4748
if (Buffer.isBuffer(message)) {
4849
raw = message;
50+
base64 = message.toString('base64');
4951
} else if (typeof message === 'string') {
5052
raw = message;
53+
base64 = Buffer.from(message).toString('base64');
5154
} else if (typeof message === 'object') {
5255
const response = await transport.sendMail(message);
5356
raw = response.message;
57+
base64 = Buffer.from(response.message).toString('base64');
5458
} else {
5559
throw new TypeError('Message argument is required');
5660
}
5761

5862
const parsed = await simpleParser(raw, options.simpleParser);
63+
parsed.base64 = base64;
5964

6065
const html = await renderFilePromise(
6166
options.template,

template.pug

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,108 @@ html
1414
min-height: 800px;
1515
max-width: 600px;
1616
display: block;
17+
}
18+
.preview-email-tabs {
19+
display: flex;
20+
flex-wrap: wrap;
21+
max-width: 600px;
1722
border: 1px solid black;
23+
background: #e2e2e2;
24+
}
25+
.preview-email-tabs input[type="radio"] {
26+
display: none;
27+
}
28+
.preview-email-tabs label {
29+
padding: 1rem;
30+
background: #e2e2e2;
31+
font-weight: bold;
32+
cursor: pointer;
33+
}
34+
.preview-email-tabs .preview-email-tab {
35+
width: 100%;
36+
background: #fff;
37+
order: 1;
38+
display: none;
39+
}
40+
.preview-email-tabs input[type='radio']:checked + label + .preview-email-tab {
41+
display: block;
42+
}
43+
.preview-email-tabs input[type="radio"]:checked + label {
44+
background: #fff;
1845
}
19-
2046
body
2147
table
48+
if base64
49+
tr
50+
td(colspan=2, style='text-align:right;'): button(type='button', onclick='downloadRawEmail()') Download Original
51+
script.
52+
function downloadRawEmail() {
53+
var link = document.createElement('a');
54+
link.href = "data:message/rfc822;base64,#{base64}";
55+
link.download = "#{messageId ? messageId.replace('<', '').replace('>', '').split('@')[0] : Date.now()}.eml";
56+
link.click();
57+
}
2258
each headerLine, i in headerLines
2359
- const index = headerLine.line.indexOf(': ')
60+
- const value = headerLine.line.slice(index + 1)
61+
- const header = headers.get(headerLine.key)
2462
tr
2563
td
26-
strong: code= headerLine.line.slice(0, index)
64+
strong= headerLine.line.slice(0, index)
2765
td
28-
code= headerLine.line.slice(index + 1)
66+
if header
67+
case headerLine.key
68+
when 'content-type'
69+
when 'content-disposition'
70+
when 'dkim-signature'
71+
= header.value || header || value
72+
//- TODO: header.params[key]
73+
when 'subject'
74+
when 'references'
75+
when 'message-id'
76+
when 'in-reply-to'
77+
when 'priority'
78+
when 'x-priority'
79+
when 'x-msmail-priority'
80+
when 'importance'
81+
= header.value || header || value
82+
when 'from'
83+
when 'to'
84+
when 'cc'
85+
when 'bcc'
86+
when 'sender'
87+
when 'reply-to'
88+
when 'delivered-to'
89+
when 'return-path'
90+
if header.html
91+
!= header.html
92+
else
93+
= header.value || header || value
94+
default
95+
//- when 'date'
96+
= header.value || header || value
97+
else
98+
= value
2999
if attachments && attachments.length > 0
30100
tr
31-
td: strong: code Attachments
101+
td: strong Attachments
32102
td
33103
ul
34104
each a in attachments
35105
li
36106
a(href=`data:${a.contentType};base64,${a.content.toString('base64')}`, download=a.filename, target='_blank')
37107
if a.filename
38-
code= `${a.filename}`
108+
code= a.filename
39109
else
40110
code= 'Unnamed file'
111+
.preview-email-tabs
41112
if html
42-
tr: td(colspan=2): strong: code HTML Version:
43-
tr: td(colspan=2): iframe(sandbox, referrerpolicy='no-referrer', seamless='seamless', srcdoc=`<base target='_top'>${html}`)#html
113+
input(type='radio', name='preview_email', checked)#tab-html
114+
label(for='tab-html') HTML
115+
.preview-email-tab
116+
iframe(sandbox, referrerpolicy='no-referrer', seamless='seamless', srcdoc=`<base target='_top'>${html}`)#html
44117
if text
45-
tr: td(colspan=2): strong: code Text Version:
46-
tr: td(colspan=2): iframe(sandbox, referrerpolicy='no-referrer', seamless='seamless', srcdoc=`<pre>${text}</pre>`)#text
118+
input(type='radio', name='preview_email', checked=!html)#tab-text
119+
label(for='tab-text') Plain text
120+
.preview-email-tab
121+
iframe(sandbox, referrerpolicy='no-referrer', seamless='seamless', srcdoc=`<pre>${text}</pre>`)#text

0 commit comments

Comments
 (0)