/
email.ts
150 lines (132 loc) · 3.87 KB
/
email.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
connect as imapConnect,
ImapSimple,
ImapSimpleOptions,
} from "imap-simple";
import lodash from "lodash";
import { simpleParser, Source as ParserSource, Attachment } from "mailparser";
import {
ITriggerClassType,
ITriggerContructorParams,
IHelpers,
AnyObject,
ITriggerOptions,
IBinaryData,
} from "actionsflow-core";
export default class Email implements ITriggerClassType {
options: ITriggerOptions = {};
helpers: IHelpers;
getItemKey(item: AnyObject): string {
// TODO adapt every cases
if (item.messageId)
return ((this.options.imap as AnyObject).host +
"__" +
item.messageId) as string;
if (item.id) return item.id as string;
return this.helpers.createContentDigest(item);
}
constructor({ helpers, options }: ITriggerContructorParams) {
this.options = options;
this.helpers = helpers;
}
async run(): Promise<AnyObject[]> {
const options = this.options;
let items: AnyObject[] = [];
const imap = {
port: 993,
tls: true,
authTimeout: 10000,
...(options.imap as AnyObject),
} as AnyObject;
if (!(imap && imap.host)) {
throw new Error("No credentials got returned!");
}
const mailbox = (options.mailbox as string) || "INBOX";
const postProcessAction =
(options.post_process_action as string) || "nothing";
const shouldDownloadAttachments =
(options.shouldDownloadAttachments as boolean) || false;
// Returns all the new unseen messages
const getNewEmails = async (
connection: ImapSimple
): Promise<AnyObject[]> => {
const searchCriteria = ["UNSEEN"];
let fetchOptions = {};
fetchOptions = {
bodies: [""],
markSeen: postProcessAction === "read",
struct: true,
};
const results = await connection.search(searchCriteria, fetchOptions);
const newEmails: AnyObject[] = [];
for (const message of results) {
const part = lodash.find(message.parts, { which: "" });
if (part === undefined) {
throw new Error("Email part could not be parsed.");
}
const parsedEmail = await parseRawEmail.call(
this,
part.body,
shouldDownloadAttachments
);
newEmails.push(parsedEmail);
}
return newEmails;
};
const config = ({
imap: imap,
} as unknown) as ImapSimpleOptions;
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
const imapConnection = await imapConnect(config);
await imapConnection.openBox(mailbox);
items = await getNewEmails(imapConnection);
await imapConnection.end();
// if need
return items;
}
}
export async function parseRawEmail(
this: Email,
messageEncoded: ParserSource,
shouldDownloadAttachments: boolean
): Promise<AnyObject> {
const responseData = ((await simpleParser(
messageEncoded
)) as unknown) as AnyObject;
// for save space, no header
// const headers: AnyObject = {};
// for (const header of responseData.headerLines as {
// key: string;
// line: AnyObject;
// }[]) {
// headers[header.key] = header.line;
// }
// responseData.headers = headers;
responseData.headerLines = undefined;
responseData.headers = undefined;
const content: IBinaryData[] = [];
if (shouldDownloadAttachments && responseData.attachments) {
for (
let i = 0;
i < ((responseData.attachments as unknown) as Attachment[]).length;
i++
) {
const attachment = ((responseData.attachments as unknown) as Attachment[])[
i
];
content[i] = await this.helpers.formatBinary(
attachment.content,
attachment.filename,
attachment.contentType
);
}
}
if (responseData.attachments) {
responseData.attachments = undefined;
}
return {
...responseData,
attachments: content,
};
}