Skip to content

Instantly share code, notes, and snippets.

@Wanuja97
Created May 8, 2023 11:36
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/c277485aac2e1317ba216200879097bb to your computer and use it in GitHub Desktop.
Save Wanuja97/c277485aac2e1317ba216200879097bb to your computer and use it in GitHub Desktop.
How to send emails in nodejs + express application - Part 03
// Creating an endpoint for sending customized emails
app.post('/customized-email',(req, res)=>{
let config = {
service: 'gmail',
auth: {
user: process.env.NODEJS_GMAIL_APP_USER,
pass: process.env.NODEJS_GMAIL_APP_PASSWORD
}
}
let transporter = nodemailer.createTransport(config);
let MailGenerator = new Mailgen({
theme: 'default',
product: {
name: 'YOUR_PRODUCT_NAME',
link: 'https://mailgen.js/'
}
});
let response = {
body: {
name: 'Name',
intro: 'Welcome to ABC Company! We\'re very excited to have you on board.',
action: {
instructions: 'To get started with ABC, please click here:',
button: {
color: '#22BC66', // Optional action button color
text: 'Confirm your account',
link: 'https://mailgen.js/'
}
}
}
};
let mail = MailGenerator.generate(response);
let message = {
from: 'wanuja18@gmail.com',
to: req.body.email,
subject: 'Welcome to ABC company!',
html: mail,
attachments: [
{
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