Created
May 8, 2023 10:34
-
-
Save Wanuja97/e769d4d03aec34390205c6d337380fd1 to your computer and use it in GitHub Desktop.
How to send emails in nodejs + express application - Part 2
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 express = require('express'); | |
const nodemailer = require('nodemailer'); | |
const Mailgen = require('mailgen'); | |
require('dotenv').config(); | |
const app = express(); | |
const port = 3001; | |
app.use(express.json()); | |
app.post('/email',(req, res)=>{ | |
let config = { | |
service: 'gmail', // your email domain | |
auth: { | |
user: process.env.NODEJS_GMAIL_APP_USER, // your email address | |
pass: process.env.NODEJS_GMAIL_APP_PASSWORD // your password | |
} | |
} | |
let transporter = nodemailer.createTransport(config); | |
let message = { | |
from: '[email protected]', // sender address | |
to: req.body.email, // list of receivers | |
subject: 'Welcome to ABC Website!', // Subject line | |
html: "<b>Hello world?</b>", // html body | |
attachments: [ // use URL as an attachment | |
{ | |
filename: 'receipt_test.pdf', | |
path: 'receipt_test.pdf', | |
cid: 'uniqreceipt_test.pdf' | |
} | |
] | |
}; | |
transporter.sendMail(message).then((info) => { | |
return res.status(201).json( | |
{ | |
msg: "Email sent", | |
info: info.messageId, | |
preview: nodemailer.getTestMessageUrl(info) | |
} | |
) | |
}).catch((err) => { | |
return res.status(500).json({ msg: err }); | |
} | |
); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment