-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (70 loc) · 2.67 KB
/
index.js
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
const headerParse = require('header-parse');
/**
* Convert header names to lower case and normalize value to string.
* @param {Object} headers - Key/value object of headers
* @return {Object} - Normalized headers
*/
const normalizedHeaders = (headers) => {
var normalized = {};
Object.keys(headers).forEach((key) => {
normalized[key.toLowerCase()] = '' + [headers[key]];
});
return normalized;
}
/**
* Check if email is autoreply according to headers.
* @param {Object} headers - Email headers as key/value object
* @return {Boolean} - True if autoreply, false otherwise
*/
module.exports = (headers) => {
if (!headers) {
return false;
}
if(typeof headers === 'string' || Buffer.isBuffer(headers)) {
headers = headerParse.parseHeaders(headers);
}
headers = normalizedHeaders(headers);
// Detections according to <https://github.com/jpmckinney/multi_mail/wiki/Detecting-autoresponders>
if ('auto-submitted' in headers && headers['auto-submitted'].toLowerCase() !== 'no') {
return true;
}
if ('return-path' in headers && headers['return-path'] === '<>') {
return true;
}
if ('preference' in headers && headers['preference'].toLowerCase() === 'auto_reply') {
return true;
}
if ('x-precedence' in headers && headers['x-precedence'].toLowerCase() === 'auto_reply') {
return true;
}
if ('x-autorespond' in headers) {
return true;
}
if ('x-autogenerated' in headers && ['forward', 'group', 'letter', 'mirror', 'redirect', 'reply'].includes(headers['x-autogenerated'].toLowerCase())) {
return true;
}
if ('x-mail-autoreply' in headers || 'x-autoreply-from' in headers) {
return true;
}
if ('x-fc-machinegenerated' in headers && headers['x-fc-machinegenerated'].toLowerCase() === 'true') {
return true;
}
if ('precedence' in headers && headers['precedence'].toLowerCase() === 'bulk') {
return true;
}
if ('x-autoreply' in headers && headers['x-autoreply'].toLowerCase() === 'yes') {
return true;
}
if ('x-post-messageclass' in headers && headers['x-post-messageclass'].toLowerCase() === '9; autoresponder') {
return true;
}
if ('delivered-to' in headers && headers['delivered-to'].toLowerCase() === 'autoresponder') {
return true;
}
// Detecting MS Exchange/Outlook according to <https://msdn.microsoft.com/en-us/library/ee219609(v=exchg.80).aspx>
if ('x-auto-response-suppress' in headers && headers['x-auto-response-suppress'].toLowerCase() !== 'none') {
return true;
}
// By default email is not autoreply
return false;
};