· 6 years ago · Dec 17, 2019, 11:20 PM
1app.post("/send", (req, res) => {
2 const output = `
3 <p>You have a new contact request</p>
4 <h3>Contact Details</h3>
5 <ul>
6 <li>Name: ${req.body.name}</li>
7 <li>Email: ${req.body.email}</li>
8 <li>Website: ${req.body.website}</li>
9 </ul>
10 <h3>Message</h3>
11 <p>${req.body.message}</p>
12 `;
13
14 // async..await is not allowed in global scope, must use a wrapper
15 async function main() {
16
17 // create reusable transporter object using the default SMTP transport
18 let transporter = nodemailer.createTransport({
19 host: "mail.privateemail.com",
20 port: 587,
21 secure: false, // true for 465, false for other ports
22 auth: {
23 user: "@.com", // generated ethereal user
24 pass: "" // generated ethereal password
25 },
26 // //for testing on localhost
27 // tls:{
28 // rejectUnauthorized: false
29 // }
30 });
31
32 // send mail with defined transport object
33 let info = await transporter.sendMail({
34 from: '"Nodemailer Contact" <@.com>', // sender address
35 to: "gmail.com", // list of receivers
36 subject: "Node Contact Request", // Subject line
37 text: "Hello world?", // plain text body
38 html: output // html body
39 });
40
41 console.log("Message sent: %s", info.messageId);
42 console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
43
44// res.render('contact', { success: "Email sent successfully!"});
45req.flash("success", 'Thanks for the message!')
46 res.redirect('/send')
47
48
49}