Skip to content

Commit 4c98baf

Browse files
Add email service with gmail and nodemailer
1 parent fa239ac commit 4c98baf

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

app/api/user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var mongoose = require('mongoose');
2+
let mail = require('../services/mail.js');
3+
let transporter = mail().getMail();
24
var api = {};
35

46
var model = mongoose.model('User');
@@ -16,6 +18,24 @@ api.list = function (req, res){
1618
api.create = function(req, res){
1719
model
1820
.create(req.body).then(function(dados){
21+
let mailOptions = {
22+
from: '"Node API" <rest-api-node@gmail.com>',
23+
to: req.body.email,
24+
subject: 'Welcome to Node API',
25+
html: '<p>Welcome to Node API!</p>'
26+
+'<p>This API is developed to provide Front End developers with a server and database configurations for connection testing and to provide a visualization of the data streamlining for the development process. The API provides HTTP methods via AJAX requests to collect, insert and update the given data.</p>'
27+
+'Github: <a href="https://github.com/renanlopescoder/rest-api-node">rest-api-node</a>'
28+
+'<br><br>Happy Hack!,'
29+
+'<br>Node API Team 🤖'
30+
+'<br><img src="http://pluspng.com/img-png/github-octocat-logo-vector-png--896.jpg" alt="Github Logo" height="82" width="82">'
31+
+'<img src="http://mean.io/wp-content/themes/twentysixteen-child/images/nodejs.png" alt="Node Logo" height="52" width="217">'
32+
};
33+
transporter.sendMail(mailOptions, (error, info) => {
34+
if (error) {
35+
return console.log(error);
36+
}
37+
console.log('Message %s sent: %s', info.messageId, info.response);
38+
});
1939
res.json(dados);
2040
}, function(error){
2141
console.log(error);

app/services/mail.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const nodemailer = require('nodemailer');
2+
3+
module.exports = (app) => {
4+
5+
let api = {};
6+
7+
api.getMail = () => {
8+
9+
let mailConfig = nodemailer.createTransport({
10+
host: 'smtp.gmail.com',
11+
auth: {
12+
user: 'rest.api.node@gmail.com',
13+
pass: 'pnzkxchwwqddzhve'
14+
}
15+
});
16+
return mailConfig;
17+
}
18+
19+
return api;
20+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"express": "^4.14.0",
1616
"express-load": "^1.1.16",
1717
"jsonwebtoken": "^7.2.1",
18-
"mongoose": "^4.7.1"
18+
"mongoose": "^4.7.1",
19+
"nodemailer": "^4.2.0"
1920
},
2021
"engines": {
2122
"node": "7.10.1"

0 commit comments

Comments
 (0)