-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask.js
163 lines (138 loc) · 5.34 KB
/
task.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
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
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
var dotenv = require('dotenv');
dotenv.load();
var e = module.exports;
e.ENV = process.env.NODE_ENV || 'development';
CONTEXTIO_KEY = process.env.CONTEXTIO_KEY;
CONTEXTIO_SECRET = process.env.CONTEXTIO_SECRET;
CONTEXTIO_ACCOUNT_ID = process.env.CONTEXTIO_ACCOUNT_ID;
LOB_KEY = process.env.LOB_KEY;
NAME = process.env.NAME;
EMAIL = process.env.EMAIL;
ADDRESS_LINE1 = process.env.ADDRESS_LINE1;
ADDRESS_LINE2 = process.env.ADDRESS_LINE2;
ADDRESS_CITY = process.env.ADDRESS_CITY;
ADDRESS_STATE = process.env.ADDRESS_STATE;
ADDRESS_ZIP = process.env.ADDRESS_ZIP;
ADDRESS_COUNTRY = process.env.ADDRESS_COUNTRY;
LIMIT = process.env.LIMIT || 100;
SENDGRID_USERNAME = process.env.SENDGRID_USERNAME;
SENDGRID_PASSWORD = process.env.SENDGRID_PASSWORD;
var wkhtmltopdf_path = process.env.PORT ? './bin/wkhtmltopdf-linux-amd64' : 'wkhtmltopdf';
var contextio = require('contextio');
var exec = require('child_process').exec;
var fs = require('fs');
var lob = require('lob');
lob = new lob(LOB_KEY);
var moment = require('moment');
var sendgrid = require('sendgrid')(SENDGRID_USERNAME, SENDGRID_PASSWORD);
function execute(command, callback){
exec(command, function(error, stdout, stderr){
callback(stdout);
});
};
var contextio_client = new contextio.Client({
key: CONTEXTIO_KEY,
secret: CONTEXTIO_SECRET
});
var runTask = function () {
var value = function () {
var filters = {
limit: LIMIT,
include_body: 1,
folder: "\\Inbox"
}
var html_paths = [];
contextio_client.accounts(CONTEXTIO_ACCOUNT_ID).messages().get(filters, function (err, response) {
if (err) throw err;
response.body.forEach(function(email) {
console.log(email);
var subject = email.subject;
var from = email.addresses.from.email;
var date = moment(parseInt(''+email.date+'000')).format("MMM Do");
var body = email.body;
var original_content = body[0].content;
var content = original_content.replace(new RegExp('\r?\n','g'), "<br/>");
var html_path = "/tmp/" + email.date + '-' + email.message_id + '.html';
html_paths.push(html_path);
var html = '<!DOCTYPE html>' +
'<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'+
'</head>'+
'<body>'+
'DATE: ' +date+'<br/>'+
'FROM: ' +from+'<br/>'+
'SUBJECT: ' +subject+'<br/>'+
'<br/>'+
content +
'</body>'+
'</html>';
fs.writeFile(html_path, html, function (err) {
if (err) throw err;
// I'd prefer to move to All Mail but not sure how yet. So instead, moving to trash for now.
//var params = {
// dst_folder: "",
// move: 1
//}
//contextio_client.accounts(CONTEXTIO_ACCOUNT_ID).messages(email.message_id).post(params, function (err, response) {
// if (err) throw err;
//});
contextio_client.accounts(CONTEXTIO_ACCOUNT_ID).messages(email.message_id).delete(function (err, response) {
if (err) throw err;
});
});
});
html_paths.sort();
var html_paths_as_string = html_paths.join(" ");
var unix_time = +new Date();
var output_path = "/tmp/" + unix_time + ".pdf";
var command = wkhtmltopdf_path + " --page-size letter --orientation portrait " + html_paths_as_string + " " + output_path;
execute(command, function(stdout) {
var email = new sendgrid.Email();
email.to = EMAIL;
email.from = EMAIL;
email.subject = '[retromail] '+unix_time;
email.text = 'Attached is your retromail from '+unix_time;
email.addFile({ path: output_path });
sendgrid.send(email, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});
lob.addresses.create({
name: NAME,
email: EMAIL,
address_line1: ADDRESS_LINE1,
address_line2: ADDRESS_LINE2,
address_city: ADDRESS_CITY,
address_state: ADDRESS_STATE,
address_zip: ADDRESS_ZIP,
address_country: ADDRESS_COUNTRY
}, function(err, res) {
var lob_address = res;
lob.objects.create({
name: output_path,
file: "@"+output_path,
setting_id: 100
}, function(err, res) {
var lob_object = res;
lob.jobs.create({
name: output_path,
from: lob_address.id,
to: lob_address.id,
object1: lob_object.id
}, function(err, res) {
console.log(err, res);
});
});
});
});
});
};
return function (name) {
var result;
result = value(name);
return result;
}
}();
runTask();