-
Notifications
You must be signed in to change notification settings - Fork 7
/
smtp.js
99 lines (79 loc) · 2.6 KB
/
smtp.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
var net = require( 'net' ),
fs = require( 'fs' ),
ip = '74.207.234.151',
name = 'Send to Dropbox',
port = 25,
smtp = (function( ip, name ) {
var eol = "\r\n",
commands = {
'OPEN' : '220 ' + ip + ' ESMTP ' + name,
'EHLO' : [
'250-' + ip + ' OH HAI <var>',
'250-SIZE 35651584',
'250-PIPELINING',
'250-ENHANCEDSTATUSCODES',
'250 8BITMIME'
].join( eol ),
'HELO' : '250 OH HAI <var>',
'MAIL' : '250 Ok',
'RCPT' : '250 Ok',
'DATA' : '354 End data with <CR><LF>.<CR><LF>',
'.' : '250 OK id=1778te-0009TT-00',
'QUIT' : '221 Peace Out'
};
function sendResponse( socket, command, arg ) {
var response = commands[ command ];
if ( arg ) {
response = response.replace( '<var>', arg );
}
console.log( 'S: ' + response );
socket.write( response + eol );
};
return {
sendResponse : sendResponse,
commands : commands
};
})( ip ),
server = function( socket ) {
var email = "",
timeout;
// Set encoding
socket.setEncoding( 'utf8' );
// New Connections
socket.addListener( 'connect', function() {
console.log( 'Incoming email!\n' );
smtp.sendResponse( socket, 'OPEN' );
});
// Incoming Data
socket.addListener( 'data', function( data ) {
var parts = data.split(/\s|\\r|\\n/),
command = parts[0];
console.log('C: ' + parts.join(' ').trim());
// Check for a command
if ( smtp.commands[ command ] ) {
smtp.sendResponse( socket, command, parts[1] );
// Check for end of email
} else if ( data.substr(-5) == "\r\n.\r\n" ) {
email += data.substring(0, data.length - 5);
smtp.sendResponse( socket, '.' );
// Build email
} else {
email += data;
}
clearTimeout( timeout );
timeout = setTimeout(function(){
smtp.sendResponse(socket, 'MAIL');
}, 10000);
});
// Finished
socket.addListener( 'close', function() {
clearTimeout( timeout );
// Do something with the email here
});
},
smtpServer = net.createServer( server );
console.log( 'Starting email server' );
// SMTP Convo will log to console.
// C: are client commands
// S: are server commands.
smtpServer.listen( port, ip );