Created
October 13, 2017 00:01
-
-
Save TheSirop/df1ea37aa53fd653bac6069034f6cd22 to your computer and use it in GitHub Desktop.
Nodemailer SendGrid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nodemailer = require('nodemailer'); | |
| // Run nodemailer | |
| export function sendEmail(req, res) { | |
| const output = ` | |
| <p>Вы получили заявку с сайта</p> | |
| <h3>Контактные данные:</h3> | |
| <ul> | |
| <li>Имя: ${req.body.name}</li> | |
| <li>Компания: ${req.body.company}</li> | |
| <li>Почта: ${req.body.email}</li> | |
| <li>Телефон: ${req.body.phone}</li> | |
| </ul> | |
| <h3>Сообщение:</h3> | |
| <p>${req.body.message}</p> | |
| `; | |
| // create reusable transporter object using the default SMTP transport | |
| const transporter = nodemailer.createTransport({ | |
| host: 'smtp.sendgrid.net', | |
| port: 465, | |
| secure: true, // use TLS | |
| auth: { | |
| user: 'login', | |
| pass: 'pass', | |
| }, | |
| tls: { | |
| // do not fail on invalid certs | |
| rejectUnauthorized: false, | |
| }, | |
| }); | |
| // setup email data with unicode symbols | |
| const mailOptions = { | |
| from: '"Компания" <noreply@gmail.com>', | |
| to: 'user@gmail.com', | |
| subject: 'Заявка с сайта', | |
| // text: 'Hello world?', | |
| html: output, | |
| }; | |
| // send mail with defined transport object | |
| transporter.sendMail(mailOptions, (error, info) => { | |
| if (error) { | |
| return global.console.log(error); | |
| } | |
| global.console.log('Message sent: %s', info.messageId); | |
| global.console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info)); | |
| // рендерим шаблон send | |
| return res.render('pages/send', { | |
| msg: 'Спасибо, Ваша заявка успешно отправлена', | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment