-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.js
146 lines (126 loc) · 4.69 KB
/
mailer.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
// 'use strict';
// const nodemailer = require('nodemailer');
// const account = {
// user: 'anglo.yhc',
// pass: 'yanghc102030'
// }
// // Generate test SMTP service account from ethereal.email
// // Only needed if you don't have a real mail account for testing
// nodemailer.createTestAccount((err, account) => {
// // create reusable transporter object using the default SMTP transport
// let transporter = nodemailer.createTransport({
// host: 'localhost',
// port: 587,
// secure: false, // true for 465, false for other ports
// auth: {
// user: account.user, // generated ethereal user
// pass: account.pass // generated ethereal password
// }
// });
// // setup email data with unicode symbols
// let mailOptions = {
// from: '"Fred Foo 👻" <[email protected]', // sender address
// to: '[email protected], [email protected]', // list of receivers
// subject: 'Hello! nodemailer test.', // Subject line
// text: 'Hello world?', // plain text body
// html: '<b>Hello world?</b>' // html body
// };
// // send mail with defined transport object
// transporter.sendMail(mailOptions, (error, info) => {
// if (error) {
// return console.log(error);
// }
// console.log('Message sent: %s', info.messageId);
// // Preview only available when sending through an Ethereal account
// console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// // Message sent: <[email protected]>
// // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
// });
// });
'use strict';
const nodemailer = require('nodemailer');
const account = {
smtp: {
host: 'localhost',
port: '12345'
},
user: 'anglo.yhc',
pass: 'yanghc102030'
}
// Generate SMTP service account from ethereal.email
nodemailer.createTestAccount((err, account) => {
if (err) {
console.error('Failed to create a testing account');
console.error(err);
return process.exit(1);
}
console.log('Credentials obtained, sending message...');
// NB! Store the account object values somewhere if you want
// to re-use the same account for future mail deliveries
// Create a SMTP transporter object
let transporter = nodemailer.createTransport(
{
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass
},
logger: false,
debug: false // include SMTP traffic in the logs
},
{
// default message fields
// sender info
from: '110 <[email protected]>',
headers: {
'X-Laziness-level': 1000 // just an example header, no need to use this
}
}
);
// Message object
let message = {
// Comma separated list of recipients
to: 'Andris Reinman <[email protected]>',
// Subject of the message
subject: 'Nodemailer is unicode friendly ✔',
// plaintext body
text: 'Hello to myself!',
// HTML body
html:
'<p><b>Hello</b> to myself <img src="cid:[email protected]"/></p>' +
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:[email protected]"/></p>',
// An array of attachments
attachments: [
// String attachment
{
filename: 'notes.txt',
content: 'Some notes about this e-mail',
contentType: 'text/plain' // optional, would be detected from the filename
},
// Binary Buffer attachment
{
filename: 'image.png',
content: new Buffer(
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
'base64'
),
cid: '[email protected]' // should be as unique as possible
}
]
};
transporter.sendMail(message, (error, info) => {
if (error) {
console.log('Error occurred');
console.log(error.message);
return process.exit(1);
}
console.log('Message sent successfully!');
console.log(nodemailer.getTestMessageUrl(info));
// only needed when using pooled connections
transporter.close();
});
});