-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.js
120 lines (99 loc) · 2.82 KB
/
email.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
const util = require('util')
const nodemailer = require('nodemailer')
const config = require('./config/config')
const EMAIL_FROM = 'Kleinhafen <[email protected]>'
const EMAIL_TO = '[email protected], [email protected]'
const DEBUG_EMAIL_TO = '[email protected]'
function makeEmailBodyForFormMessage(msg) {
const grd = (d) => d.length > 0 ? d : 'Not known'
const formTypeLine = {
flexDesk: `<p>Someone is asking about a flex desk.</p>`,
fixedDesk: '<p>Someone is asking about a fixed desk.</p>',
meetingRoom: '<p>Someone is asking about a meeting room.</p>',
storefront: '<p>Someone is asking about the storefront.</p>',
}[msg.formType]
const detailsLine = `
<tr><td>Name</td><td>${grd(msg.name)}</td></tr>
<tr><td>Email</td><td>${grd(msg.email)}</td></tr>
<tr><td>Phone number</td><td>${grd(msg.phone)}</td></tr>
`
const companyLine = (msg.company.length == 0 ? '' : `
<tr><td>Company</td><td>${msg.company}</td></tr>
`)
const peopleLine = (msg.nPeople.length == 0 ? '' : `
<tr><td>Number of people</td><td>${msg.nPeople}</td></tr>
`)
const datesLine = (msg.dates.length == 0 ? '' : `
<tr><td>Date(s) needed</td><td>${msg.dates}</td></tr>
`)
const tourDatesLine = (msg.tourDates.length == 0 ? '' : `
<tr><td>Tour dates</td><td>${msg.tourDates}</td></tr>
`)
const descriptionLine = `<tr><td>Description</td><td>${msg.description}</td></tr>`
return `
<p>Ahoi!</p>
${formTypeLine}
<table>
${detailsLine}
${companyLine}
${peopleLine}
${datesLine}
${tourDatesLine}
${descriptionLine}
</table>
<p>
Best,<br>
The Kleinhafen postman
</p>
`
}
function sendFormEmail(emailBody, done) {
const transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: config.email.user,
pass: config.email.password,
}
})
const msg = {
from: EMAIL_FROM,
to: EMAIL_TO,
subject: 'New message from the website',
text: "There's a new message from the website! Please use an email client that supports HTML to see it.",
html: emailBody,
}
transporter.sendMail(msg, (err, info) => {
console.log('[email#sendFormEmail]', err, info)
done(err, info)
})
}
function sendDebugEmail(info, done) {
const transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: config.email.user,
pass: config.email.password,
}
})
const msg = {
from: EMAIL_FROM,
to: DEBUG_EMAIL_TO,
subject: 'kleinhafen.ch debug email',
text: util.inspect(info, false, null),
}
transporter.sendMail(msg, (err, info) => {
console.log('[email#sendDebugEmail]', err, info)
if (done) {
done(err, info)
}
})
}
module.exports = {
makeEmailBodyForFormMessage,
sendFormEmail,
sendDebugEmail,
}