-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle unexpected error on checkout endpoint
- Loading branch information
1 parent
9142ee9
commit 6a01270
Showing
2 changed files
with
89 additions
and
85 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 |
---|---|---|
@@ -1,100 +1,100 @@ | ||
require('dotenv').config() | ||
const nodemailer = require('nodemailer') | ||
const handlebars = require('handlebars') | ||
const fs = require('fs') | ||
require("dotenv").config(); | ||
const nodemailer = require("nodemailer"); | ||
const handlebars = require("handlebars"); | ||
const fs = require("fs"); | ||
|
||
async function sendEmail (contactObj) { | ||
async function sendEmail(contactObj) { | ||
try { | ||
const transporter = nodemailer.createTransport({ | ||
host: process.env.EMAIL_HOST, | ||
port: process.env.EMAIL_PORT, | ||
secure: true, | ||
auth: { | ||
user: process.env.EMAIL_USERNAME, | ||
pass: process.env.EMAIL_PASSWORD | ||
} | ||
}) | ||
let res = null | ||
if (contactObj.type === 'contact') { | ||
pass: process.env.EMAIL_PASSWORD, | ||
}, | ||
}); | ||
let res = null; | ||
if (contactObj.type === "contact") { | ||
res = await transporter.sendMail({ | ||
from: process.env.EMAIL_USERNAME, | ||
to: process.env.EMAIL_USERNAME, | ||
subject: 'User Inquiry through Contact Us', | ||
text: `Dear Raghav,\n\nI hope this email finds you well. I am reaching out to inform you that we have received an inquiry through the Contact Us section of your website. The details of the inquiry are as follows:\n\nName:${contactObj.name}\nContact Details:${contactObj.phone}\n\nMessage:${contactObj.message}\n\nWe appreciate your efforts in creating a user-friendly platform for customers to connect with your brand. We will be taking prompt action to respond to the user and address their concerns accordingly.\n\nThank you for your attention to this matter.\n\nBest regards,\n${contactObj.name}\n${contactObj.phone}\n${contactObj.email}` | ||
}) | ||
const source = fs.readFileSync('../mail_assets/contact.hbs', 'utf8') | ||
const template = handlebars.compile(source) | ||
const html = template({ user: contactObj.name }) | ||
subject: "User Inquiry through Contact Us", | ||
text: `Dear Raghav,\n\nI hope this email finds you well. I am reaching out to inform you that we have received an inquiry through the Contact Us section of your website. The details of the inquiry are as follows:\n\nName:${contactObj.name}\nContact Details:${contactObj.phone}\n\nMessage:${contactObj.message}\n\nWe appreciate your efforts in creating a user-friendly platform for customers to connect with your brand. We will be taking prompt action to respond to the user and address their concerns accordingly.\n\nThank you for your attention to this matter.\n\nBest regards,\n${contactObj.name}\n${contactObj.phone}\n${contactObj.email}`, | ||
}); | ||
const source = fs.readFileSync("../mail_assets/contact.hbs", "utf8"); | ||
const template = handlebars.compile(source); | ||
const html = template({ user: contactObj.name }); | ||
if (contactObj.email) { | ||
await transporter.sendMail({ | ||
from: process.env.EMAIL_USERNAME, | ||
to: contactObj.email, | ||
subject: 'Thank you for contacting Raagwaas.', | ||
subject: "Thank you for contacting Raagwaas.", | ||
html, | ||
attachments: [ | ||
{ | ||
filename: 'white-logo.png', | ||
path: '../mail_assets/white-logo.png', | ||
cid: 'white-logo.png' | ||
filename: "white-logo.png", | ||
path: "../mail_assets/white-logo.png", | ||
cid: "white-logo.png", | ||
}, | ||
{ | ||
filename: 'instagram.png', | ||
path: '../mail_assets/instagram.png', | ||
cid: 'instagram.png' | ||
filename: "instagram.png", | ||
path: "../mail_assets/instagram.png", | ||
cid: "instagram.png", | ||
}, | ||
{ | ||
filename: 'facebook.png', | ||
path: '../mail_assets/facebook.png', | ||
cid: 'facebook.png' | ||
} | ||
] | ||
}) | ||
filename: "facebook.png", | ||
path: "../mail_assets/facebook.png", | ||
cid: "facebook.png", | ||
}, | ||
], | ||
}); | ||
} | ||
} else { | ||
res = await transporter.sendMail({ | ||
from: process.env.EMAIL_USERNAME, | ||
to: process.env.EMAIL_USERNAME, | ||
subject: 'Request for Subscription', | ||
text: `Dear Raghav,\n\nI want to subscribing your newsletter! \n\n Email address:${contactObj?.email}` | ||
}) | ||
const source = fs.readFileSync('../mail_assets/subscribe.hbs', 'utf8') | ||
const template = handlebars.compile(source) | ||
const html = template({ user: contactObj.name }) | ||
subject: "Request for Subscription", | ||
text: `Dear Raghav,\n\nI want to subscribing your newsletter! \n\n Email address:${contactObj?.email}`, | ||
}); | ||
const source = fs.readFileSync("../mail_assets/subscribe.hbs", "utf8"); | ||
const template = handlebars.compile(source); | ||
const html = template({ user: contactObj.name }); | ||
if (contactObj.email) { | ||
await transporter.sendMail({ | ||
from: process.env.EMAIL_USERNAME, | ||
to: contactObj.email, | ||
subject: 'Thank you for contacting Raagwaas.', | ||
subject: "Thank you for contacting Raagwaas.", | ||
html, | ||
attachments: [ | ||
{ | ||
filename: 'white-logo.png', | ||
path: '../mail_assets/white-logo.png', | ||
cid: 'white-logo.png' | ||
filename: "white-logo.png", | ||
path: "../mail_assets/white-logo.png", | ||
cid: "white-logo.png", | ||
}, | ||
{ | ||
filename: 'instagram.png', | ||
path: '../mail_assets/instagram.png', | ||
cid: 'instagram.png' | ||
filename: "instagram.png", | ||
path: "../mail_assets/instagram.png", | ||
cid: "instagram.png", | ||
}, | ||
{ | ||
filename: 'facebook.png', | ||
path: '../mail_assets/facebook.png', | ||
cid: 'facebook.png' | ||
} | ||
] | ||
}) | ||
filename: "facebook.png", | ||
path: "../mail_assets/facebook.png", | ||
cid: "facebook.png", | ||
}, | ||
], | ||
}); | ||
} | ||
} | ||
if (res?.messageId || res?.response?.includes('OK')) { | ||
return { data: res } | ||
if (res?.messageId || res?.response?.includes("OK")) { | ||
return { data: res }; | ||
} else { | ||
return false | ||
return false; | ||
} | ||
} catch (error) { | ||
console.log('Error occurred during sending mail: ' + error.message) | ||
return null | ||
console.log("Error occurred during sending mail: " + error.message); | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = sendEmail | ||
module.exports = sendEmail; |
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