Created
May 8, 2023 11:36
-
-
Save Wanuja97/c277485aac2e1317ba216200879097bb to your computer and use it in GitHub Desktop.
How to send emails in nodejs + express application - Part 03
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
// 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: '[email protected]', | |
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