Skip to content

Instantly share code, notes, and snippets.

@Wanuja97
Created May 8, 2023 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wanuja97/e769d4d03aec34390205c6d337380fd1 to your computer and use it in GitHub Desktop.
Save Wanuja97/e769d4d03aec34390205c6d337380fd1 to your computer and use it in GitHub Desktop.
How to send emails in nodejs + express application - Part 2
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: 'wanuja18@gmail.com', // 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