irongit
35 lines982 BJavaScript
1const nodemailer = require('nodemailer');
2
3// Function to send an email
4const sendEmail = async (to, subject, text, html) => {
5 try {
6 if (to && process.env.user && process.env.pass) {
7 const transporter = nodemailer.createTransport({
8 host: 'smtp.office365.com',
9 port: 587,
10 auth: {
11 user: process.env.user,
12 pass: process.env.pass
13 }
14 });
15
16 const mailOptions = {
17 from: process.env.user,
18 to: to,
19 subject: subject,
20 text: text,
21 html: html
22 };
23
24 const info = await transporter.sendMail(mailOptions);
25 console.log('Email sent: ' + info.response);
26 return info.response;
27 }
28 } catch (error) {
29 console.error('Failed to send email:', error);
30 throw error;
31 }
32};
33
34
35module.exports = { sendEmail }