· 6 years ago · Jun 28, 2019, 05:30 PM
1const express = require('express');
2const nodemailer = require('nodemailer');
3const app = express();
4const port = 3001;
5
6app.get('/askForCookiesRecipe', (req, res) => {
7 // Création de la méthode de transport de l'email
8 const smtpTransport = nodemailer.createTransport({
9 host: 'smtp.mailtrap.io',
10 port: 587,
11 auth: {
12 user: 'Usernameofmailtrap.io',
13 pass: 'Passwordofmailtrap.io'
14 }
15 });
16
17 smtpTransport.sendMail({
18 from: '******************************@gmail.com', // Expediteur
19 to: 'supergrandma@yopmail.com', // Destinataires
20 subject: 'Cookie Recipe !', // Sujet
21 text: "Hello GrandMa, I would like to eat cookies but I don't know the recipe.. :/ Can you send it to me ?", // plaintext body
22 html: '<b>Hello GrandMa !</b>' // html body
23 }),
24 (error, res) => {
25 if (error) {
26 console.log(error);
27 } else {
28 console.log('Message envoyé : ' + res.message);
29 }
30 };
31 res.send('Mail send')
32});
33
34app.listen(port, err => {
35 if (err) {
36 throw new Error('Something bad happened...');
37 }
38 console.log(`Server is listening on ${port}`);
39});