Last active
September 22, 2018 14:21
-
-
Save tosipaulo/774aef122efd04c6e191fdab4fb45579 to your computer and use it in GitHub Desktop.
nodemailer
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
require('dotenv').load(); | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const cors = require('cors'); | |
const nodemailer = require('nodemailer'); | |
const hbs = require('nodemailer-express-handlebars'); | |
const app = express(); | |
const port = process.env.PORT || 4000; | |
const uri = process.env.SMTP; | |
const transporter = nodemailer.createTransport(uri); | |
transporter.use('compile', hbs({ | |
viewPath: "views", | |
extName: ".hbs" | |
})) | |
const mailOptions = { | |
from: 'BUUUU 👻 <[email protected]>', | |
to: '[email protected]', | |
subject: 'BUUUU 👻', | |
template: 'main' | |
} | |
app.use(bodyParser.urlencoded({ extended: false })) | |
app.use(cors()); | |
app.get('/', (req, res) => { | |
res.status(200).json({msg: 'ok'}) | |
}) | |
app.post('/email', (req, res) => { | |
const { nome, telefone, email } = req.body | |
const mensagem = { ...mailOptions, ...{context: {nome, telefone, email}}} | |
transporter.sendMail(mensagem, (error, info) => { | |
if (error) { | |
return console.log(error); | |
} | |
console.log('Message sent: ' + info.response); | |
}) | |
res.status(200).json({ mensagem: "E-mail enviado com sucesso" }) | |
}) | |
app.listen(port, () => { | |
console.log('running ' + port) | |
}) |
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
{ | |
"name": "nodemailer", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"dependencies": { | |
"body-parser": "^1.18.3", | |
"cors": "^2.8.4", | |
"dotenv": "^6.0.0", | |
"express": "^4.16.3", | |
"nodemailer": "^4.6.8", | |
"nodemailer-express-handlebars": "^3.0.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"start": "node index.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "Paulo Tosi <[email protected]> (http://www.paulotosi.com.br/)", | |
"license": "MIT" | |
} |
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
web: node index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment