Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { parse } from "https://esm.town/v/stevekrouse/parseEmail?v=9";
const htmlEmailList = (es: { name: string; email: string }[]): string => {
return es
.map(
(t) => `<strong>${t.name}</strong> <span><<a href="mailto:${t.email}">${t.email}</a>></span>`,
)
.join(", ");
};
const textEmailList = (es: { name: string; email: string }[]): string => {
return es.map((t) => `${t.name} <${t.email}>`).join(", ");
};
// Sat, Aug 17, 2024 at 10:42
function formatDate(date) {
const options = {
weekday: "short",
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
timeZone: "America/New_York",
};
const formattedDate = date.toLocaleDateString("en-US", options); // Tue, Aug 27, 2024, 1:18 PM
const [weekday, monthDay, year, time] = formattedDate.split(", ");
return `${weekday}, ${monthDay}, ${year} at ${time}`;
}
export default function emailAddForwardedInfo(e: Email): Email {
const [to, cc, bcc, from] = [e.to, e.cc, e.cc, e.from].map(parse);
if (e.html) {
let html = `---------- Forwarded message ---------<br>From: ${htmlEmailList(from)}<br>Date: ${
formatDate(new Date())
}<br>Subject: ${e.subject}<br>To: ${htmlEmailList(to)}`;
if (cc.length > 0) {
html += `<br>Cc: ${htmlEmailList(cc)}`;
}
if (bcc.length > 0) {
html += `<br>Bcc: ${htmlEmailList(cc)}`;
}
html += `<br><br>${e.html}`;
e.html = html;
}
if (e.text) {
let text = `---------- Forwarded message ---------\nFrom: ${textEmailList(from)}\nDate: ${
formatDate(new Date())
}\nSubject: ${e.subject}\nTo: ${textEmailList(to)}`;
if (cc.length > 0) {
text += `\nCc: ${textEmailList(cc)}`;
}
if (bcc.length > 0) {
text += `\nBcc: ${textEmailList(cc)}`;
}
text += `\n\n${e.text}`;
e.text = text;
}
return { ...e };
}
export const runTest = () => {
console.log(formatDate(new Date()));
let e = emailAddForwardedInfo({
from: "Steve Krouse <steve@val.town>, Max McDonnell <max@val.town>",
to: "Steve Krouse <steve@val.town>, Max McDonnell <max@val.town>",
cc: "Steve Krouse <steve@val.town>, Max McDonnell <max@val.town>",
bcc: "Steve Krouse <steve@val.town>, Max McDonnell <max@val.town>",
subject: "steve@val.town",
text: "hello hello",
html: "<b>hello</b>",
attachments: [],
});
console.log(e);
// Add some assertions here?
};
// If this is an in-use library, run this from a fork or another val
// runTest();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
September 10, 2024
We're getting some weird
undefined
results sometimesI kinda prefer the old way this forwarder used to work. What do you think?