Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express')
- var bodyParser = require('body-parser')
- var request = require('request')
- var app = express()
- // npm install nodemailer nodemailer-smtp-pool
- var nodemailer = require('nodemailer');
- var smtpPool = require('nodemailer-smtp-pool');
- app.set('port', (process.env.PORT || 3000))
- // Process application/x-www-form-urlencoded
- app.use(bodyParser.urlencoded({extended: false}))
- // Process application/json
- app.use(bodyParser.json())
- // Index route
- app.get('/', function (req, res) {
- res.send('Hello world, I am a send mail')
- })
- // send email
- app.get('/sendmail', function(req, res, next){
- // Create a SMTP transporter object
- var transporter = nodemailer.createTransport(smtpPool({
- host: 'smtp.gmail.com',
- port: 465,
- secure: true,
- auth: {
- user: 'username', // change your username here
- pass: 'password' // change your password here
- },
- // use up to 5 parallel connections, default is 5
- maxConnections: 5,
- // do not send more than 10 messages per connection, default is 100
- maxMessages: 10,
- // no not send more than 5 messages in a second, default is no limit
- rateLimit: 5
- }));
- console.log('SMTP Configured');
- // Message object
- var message = {
- // sender info
- from: 'Sender Name <sumn2u@gmail.com>',
- // Comma separated list of recipients
- to: '"Receiver Name" <sumn2u@hotmail.com>',
- // Subject of the message
- subject: 'Nodemailer is unicode friendly ✔', //
- headers: {
- 'X-Laziness-level': 1000
- },
- // plaintext body
- text: 'Hello to myself!',
- // HTML body
- html: '<p><b>Hello</b> to myself <img src="cid:note@example.com"/></p>' +
- '<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:nyan@example.com"/></p>',
- };
- console.log('Sending Mail');
- transporter.sendMail(message, function(error, info) {
- if (error) {
- console.log('Error occurred');
- res.send(error.message);
- console.log(error.message);
- return;
- }
- res.send({"message sent"});
- console.log('Message sent successfully!');
- console.log('Server responded with "%s"', info.response);
- });
- });
- // Spin up the server
- app.listen(app.get('port'), function() {
- console.log('running on port', app.get('port'))
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement