forked from eleith/emailjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
195 lines (165 loc) · 5.07 KB
/
client.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var smtp = require('./smtp');
var smtpError = require('./error');
var message = require('./message');
var address = require('./address');
var Client = function(server)
{
this.smtp = new smtp.SMTP(server);
//this.smtp.debug(1);
this.queue = [];
this.timer = null;
this.sending = false;
this.ready = false;
};
Client.prototype =
{
_poll: function()
{
var self = this;
clearTimeout(self.timer);
if(self.queue.length)
{
if(self.smtp.state() == smtp.state.NOTCONNECTED)
self._connect(self.queue[0]);
else if(self.smtp.state() == smtp.state.CONNECTED && !self.sending && self.ready)
self._sendmail(self.queue.shift());
}
// wait around 1 seconds in case something does come in, otherwise close out SMTP connection if still open
else if(self.smtp.state() == smtp.state.CONNECTED)
self.timer = setTimeout(function() { self.smtp.quit(); }, 1000);
},
_connect: function(stack)
{
var self = this,
connect = function(err)
{
if(!err)
{
var begin = function(err)
{
if(!err)
{
self.ready = true;
self._poll();
}
else
stack.callback(err, stack.message);
};
if(!self.smtp.authorized())
self.smtp.login(begin);
else
self.smtp.ehlo_or_helo_if_needed(begin);
}
else
stack.callback(err, stack.message);
};
self.ready = false;
self.smtp.connect(connect);
},
send: function(msg, callback)
{
var self = this;
if(!(msg instanceof message.Message)
&& msg.from
&& (msg.to || msg.cc || msg.bcc)
&& (msg.text || this._containsInlinedHtml(msg.attachment)))
msg = message.create(msg);
if(msg instanceof message.Message)
{
msg.valid(function(valid, why)
{
if(valid)
{
var stack =
{
message: msg,
to: address.parse(msg.header.to),
from: address.parse(msg.header.from)[0].address,
callback: callback || function() {}
};
if(msg.header.cc)
stack.to = stack.to.concat(address.parse(msg.header.cc));
if(msg.header.bcc)
stack.to = stack.to.concat(address.parse(msg.header.bcc));
self.queue.push(stack);
self._poll();
}
else
callback(new Error(why), msg);
});
}
else
callback(new Error("message is not a valid Message instance"), msg);
},
_containsInlinedHtml: function(attachment) {
if (Array.isArray(attachment)) {
return attachment.some((function(ctx) {
return function(att) {
return ctx._isAttachmentInlinedHtml(att);
};
})(this));
} else {
return this._isAttachmentInlinedHtml(attachment);
}
},
_isAttachmentInlinedHtml: function(attachment) {
return attachment &&
(attachment.data || attachment.path) &&
attachment.alternative === true;
},
_sendsmtp: function(stack, next)
{
var self = this;
var check= function(err)
{
if(!err && next)
{
next.apply(self, [stack]);
}
else
{
// if we snag on SMTP commands, call done, passing the error
// but first reset SMTP state so queue can continue polling
self.smtp.rset(function() { self._senddone(err, stack); });
}
};
return check;
},
_sendmail: function(stack)
{
var self = this;
self.sending = true;
self.smtp.mail(self._sendsmtp(stack, self._sendrcpt), '<' + stack.from + '>');
},
_sendrcpt: function(stack)
{
var self = this, to = stack.to.shift().address;
self.smtp.rcpt(self._sendsmtp(stack, stack.to.length ? self._sendrcpt : self._senddata), '<'+ to +'>');
},
_senddata: function(stack)
{
var self = this;
self.smtp.data(self._sendsmtp(stack, self._sendmessage));
},
_sendmessage: function(stack)
{
var self = this, stream = stack.message.stream();
stream.on('data', function(data) { self.smtp.message(data); });
stream.on('end', function() { self.smtp.data_end(self._sendsmtp(stack, function() { self._senddone(null, stack) })); });
// there is no way to cancel a message while in the DATA portion, so we have to close the socket to prevent
// a bad email from going out
stream.on('error', function(err) { self.smtp.close(); self._senddone(err, stack); });
},
_senddone: function(err, stack)
{
var self = this;
self.sending = false;
stack.callback(err, stack.message);
self._poll();
}
};
exports.Client = Client;
exports.connect = function(server)
{
return new Client(server);
};