-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; |