-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsendEmail.js
67 lines (59 loc) · 1.61 KB
/
sendEmail.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
const config = require('./config');
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const landlordEmails = require('./landlordEmails');
const sendEmail = function(apartment) {
const info = apartment['resultlist.realEstate'];
if (!info) {
console.log('UNABLE TO SEND EMAIL FOR ', JSON.stringify(apartment));
return;
}
const email = landlordEmails[info.contactDetails.company];
if(!email){
console.log('EMAIL ADDRESS NOT FOUND, FOR ', JSON.stringify(apartment));
return;
}
const salutation =
info.contactDetails.salutation === 'FEMALE' ? 'Frau' : 'Herr';
let intro =
info.contactDetails.lastname && info.contactDetails.salutation
? `${salutation} ${info.contactDetails.lastname}`
: 'Damen und Herren';
intro = `Sehr geehrte ${intro}`;
const mailOptions = {
from: config.email,
to: email,
subject: info.title,
html: `
${intro},
<br>
<br/>
${config.emailContentHTML}
<br>
<br/>
`
};
var transporter = nodemailer.createTransport(
smtpTransport({
service: 'gmail',
port: 25,
auth: {
user: config.email,
pass: config.emailPassword
}
})
);
return new Promise(function(resolve, reject) {
transporter.sendMail(mailOptions, function(err, info) {
console.log(`SENDING EMAIL for ${JSON.stringify(apartment)}`);
if (err) {
console.error('ERROR SENDING EMAIL');
reject(err);
} else {
console.error('EMAIL SEND');
resolve(info);
}
});
});
};
module.exports = sendEmail;