-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (85 loc) · 2.77 KB
/
app.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
const express = require('express')
const app = express()
const cors = require('cors')
const port = 3000
require('dotenv').config()
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: process.env.TRANSPORTER_HOST,
port: process.env.TRANSPORTER_PORT,
secure: process.env.TRANSPORTER_SECURE,
auth: {
user: process.env.TRANSPORTER_AUTH_USER,
pass: process.env.TRANSPORTER_AUTH_PASSWORD,
},
});
app.use(express.json())
app.use(cors({
origin: process.env.CORS_ORIGIN,
}))
app.post('/register', async (req, res) => {
mailOptions.text = JSON.stringify(req.body);
sendEmail(req.body)
.then(() => res.send({ok: true}))
.catch((error) => {
console.log(error);
res.send({ok: false})
});
})
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
const mailOptions = {
from: process.env.EMAIL_FROM,
to: process.env.EMAIL_TO,
subject: 'Nouvelle inscription au mariage',
};
async function sendEmail(data) {
const confirmationMailOptions = {
from: process.env.EMAIL_FROM,
to: data.email,
subject: data.locale === 'de-DE' ? getGermanSubject() : getFrenchSubject(),
text: data.locale === 'de-DE' ? getGermanText(data) : getFrenchText(data)
};
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
reject(error);
}
transporter.sendMail(confirmationMailOptions, function (error, info) {
if (error) {
reject(error);
console.log(error);
}
resolve();
})
});
})
}
const getGermanSubject = () => {
return 'Anmeldebestätigung';
}
const getGermanText = (data) => {
return `Vielen Dank für die Anmeldung, Wir sind sehr glücklich, diesen Tag mit dir zu verbringen.
Hier nochmals die Zusammenfassung:
- Essgewohnheiten : ${data.diet}
- Allergien : ${data.allergies ? data.allergies : 'aucune'}
- Übernachtung : ${data.lodging}
Falls du etwas ändern möchtest, schreib uns bitte eine Nachricht.
Bisous ❤️
Corin & Phippu`
}
const getFrenchSubject = () => {
return 'Confirmation';
}
const getFrenchText = (data) => {
return `Merci beaucoup pour l’inscription. On est très heureux de passer cette journée avec toi.
Voici le récapitulatif :
- Habitudes alimentaires : ${data.diet}
- Alergies : ${data.allergies ? data.allergies : 'aucune'}
- Logement : ${data.lodging}
Si tu souhaites changer quelque chose, envoie-nous un message
Bisous ❤️
Corin & Philipp️`
}