forked from nodemailer/nodemailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-proxy.js
78 lines (66 loc) · 2 KB
/
custom-proxy.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
/* eslint no-console: 0 */
'use strict';
/*
This example demonstrates how to use proxies using a custom proxy connector
*/
// create SOCKS5 proxy with
// ssh -N -D 0.0.0.0:1080 [email protected]
var Socks = require('socks');
var nodemailer = require('../../lib/nodemailer');
var proxy = {
ipaddress: 'localhost',
port: 1080,
type: 5
};
// Create a SMTP transporter object
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'Nodemailer123'
},
logger: true, // log to console
debug: true // include SMTP traffic in the logs
});
// This method handles socket creating
transporter.getSocket = function (options, callback) {
console.log('Socket requested to %s:%s', options.host, options.port);
var proxyOptions = {
proxy: proxy,
target: {
host: options.host,
port: options.port
},
command: 'connect'
};
console.log(proxyOptions);
Socks.createConnection(proxyOptions, function(err, socket){
callback(err, {
// everything we pass here will be appended to the options of smtp-connection
// see possible options here:
// https://github.com/nodemailer/smtp-connection#create-smtpconnection-instance
connection: socket,
tls: {
rejectUnauthorized: true
}
});
});
};
console.log('SMTP Configured');
var message = {
from: 'Sender Name <[email protected]>',
to: '"Receiver Name" <[email protected]>',
subject: 'Nodemailer is unicode friendly ✔', //
text: 'Hello to myself!',
html: '<p><b>Hello</b> world!</p>'
};
console.log('Sending Mail');
transporter.sendMail(message, function (error, info) {
if (error) {
console.log('Error occurred');
console.log(error.message);
return;
}
console.log('Message sent successfully!');
console.log('Server responded with "%s"', info.response);
});