Skip to content

Commit

Permalink
fixed email configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kaxada committed Dec 11, 2023
1 parent 5d73b6c commit af52c9d
Showing 1 changed file with 73 additions and 71 deletions.
144 changes: 73 additions & 71 deletions helpers/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,85 +11,87 @@ const mailer = async (
htmlLink,
results
) => {
if (
!process.env.EMAIL_HOST ||
!process.env.EMAIL_HOST ||
!process.env.EMAIL_USERNAME ||
!process.env.EMAIL_PASSWORD
) {
console.error("Email service is not configured");

if (process.env.NODE_ENV === "development") {
console.log(`Sending email to '${email}'`, results);
}

return;
}

// Create a transporter using your email service provider's SMTP settings
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "[email protected]",
pass: process.env.MAIL_PASSWORD,
},
});
try {
if (
!process.env.EMAIL_HOST ||
!process.env.EMAIL_ADDRESS ||
!process.env.EMAIL_PASSWORD
) {
console.error("Email service is not configured");
return;
} else {
try {
// Create a transporter using your email service provider's SMTP settings
const transporter = nodemailer.createTransport({
service: process.env.EMAIL_HOST,
auth: {
user: process.env.EMAIL_ADDRESS,
pass: process.env.EMAIL_PASSWORD,
},
});

let mailOptions = {};
let mailOptions = {};

if (results) {
// Read the HTML template file
const templatePath = path.resolve(
__dirname,
"./email_templates/failure.html"
);
const html = fs.readFileSync(templatePath, { encoding: "utf-8" });
if (results) {
// Read the HTML template file
const templatePath = path.resolve(
__dirname,
"./email_templates/failure.html"
);
const html = fs.readFileSync(templatePath, { encoding: "utf-8" });

// Replace placeholders with dynamic values in the HTML template
const replacedHTML = await html
.replace("{{recipientName}}", recipientName)
.replace("{{badgeName}}", badgeName)
.replace("{{results}}", results);
// Replace placeholders with dynamic values in the HTML template
const replacedHTML = await html
.replace("{{recipientName}}", recipientName)
.replace("{{badgeName}}", badgeName)
.replace("{{results}}", results);

// Define the email options with HTML content
mailOptions = {
from: "[email protected]",
to: email,
subject: "DEI Badging report",
html: replacedHTML,
};
} else {
// Read the HTML template file
const templatePath = path.resolve(
__dirname,
"./email_templates/success.html"
);
const html = fs.readFileSync(templatePath, { encoding: "utf-8" });
// Define the email options with HTML content
mailOptions = {
from: process.env.EMAIL_ADDRESS,
to: email,
subject: "DEI Badging report",
html: replacedHTML,
};
} else {
// Read the HTML template file
const templatePath = path.resolve(
__dirname,
"./email_templates/success.html"
);
const html = fs.readFileSync(templatePath, { encoding: "utf-8" });

// Replace placeholders with dynamic values in the HTML template
const replacedHTML = await html
.replace("{{recipientName}}", recipientName)
.replace("{{badgeName}}", badgeName)
.replace("{{markdownLink}}", markdownLink)
.replace("{{htmlLink}}", htmlLink);
// Replace placeholders with dynamic values in the HTML template
const replacedHTML = await html
.replace("{{recipientName}}", recipientName)
.replace("{{badgeName}}", badgeName)
.replace("{{markdownLink}}", markdownLink)
.replace("{{htmlLink}}", htmlLink);

// Define the email options with HTML content
mailOptions = {
from: "[email protected]",
to: email,
subject: "DEI Badging report",
html: replacedHTML,
};
}
// Define the email options with HTML content
mailOptions = {
from: process.env.EMAIL_ADDRESS,
to: email,
subject: "DEI Badging report",
html: replacedHTML,
};
}

// Send the email
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log("Error:", error);
} else {
console.log("Email sent:", info.response);
// Send the email
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log("Error:", error);
} else {
console.log("Email sent:", info.response);
}
});
} catch (error) {
console.error(error);
}
}
});
} catch (error) {
console.error(error);
}
};

module.exports = mailer;

0 comments on commit af52c9d

Please sign in to comment.