Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mailgun.js #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 54 additions & 73 deletions server/services/mailgun.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,70 @@
const Mailgun = require('mailgun-js');

const template = require('../config/template');
const keys = require('../config/keys');

const { key, domain, sender } = keys.mailgun;

class MailgunService {
init() {
try {
return new Mailgun({
apiKey: key,
domain: domain
});
} catch (error) {
console.warn('Missing mailgun keys');
constructor() {
if (!key || !domain || !sender) {
throw new Error('Missing mailgun keys');
}
this.mailgun = new Mailgun({ apiKey: key, domain: domain });
}
}

const mailgun = new MailgunService().init();
sendEmail = async (email, type, host, data) => {
try {
const message = this.prepareTemplate(type, host, data);

exports.sendEmail = async (email, type, host, data) => {
try {
const message = prepareTemplate(type, host, data);
if (!message) {
throw new Error('Invalid email type provided');
}

const config = {
from: `MERN Store! <${sender}>`,
to: email,
subject: message.subject,
text: message.text
};
const config = {
from: `MERN Store! <${sender}>`,
to: email,
subject: message.subject,
text: message.text,
};

return await mailgun.messages().send(config);
} catch (error) {
return error;
await this.mailgun.messages().send(config);
console.log(`Email sent to ${email}`);
} catch (error) {
console.error('Error sending email:', error.message);
throw new Error('Failed to send email. Please try again later.');
}
};

prepareTemplate(type, host, data) {
switch (type) {
case 'reset':
return template.resetEmail(host, data);
case 'reset-confirmation':
return template.confirmResetPasswordEmail();
case 'signup':
return template.signupEmail(data);
case 'merchant-signup':
return template.merchantSignup(host, data);
case 'merchant-welcome':
return template.merchantWelcome(data);
case 'newsletter-subscription':
return template.newsletterSubscriptionEmail();
case 'contact':
return template.contactEmail();
case 'merchant-application':
return template.merchantApplicationEmail();
case 'merchant-deactivate-account':
return template.merchantDeactivateAccount();
case 'order-confirmation':
return template.orderConfirmationEmail(data);
default:
return null; // Return null for invalid types
}
}
};

const prepareTemplate = (type, host, data) => {
let message;

switch (type) {
case 'reset':
message = template.resetEmail(host, data);
break;

case 'reset-confirmation':
message = template.confirmResetPasswordEmail();
break;

case 'signup':
message = template.signupEmail(data);
break;

case 'merchant-signup':
message = template.merchantSignup(host, data);
break;

case 'merchant-welcome':
message = template.merchantWelcome(data);
break;

case 'newsletter-subscription':
message = template.newsletterSubscriptionEmail();
break;

case 'contact':
message = template.contactEmail();
break;

case 'merchant-application':
message = template.merchantApplicationEmail();
break;

case 'merchant-deactivate-account':
message = template.merchantDeactivateAccount();
break;

case 'order-confirmation':
message = template.orderConfirmationEmail(data);
break;
}

default:
message = '';
}
// Create an instance of MailgunService
const mailgunService = new MailgunService();

return message;
};
// Export the sendEmail method
exports.sendEmail = mailgunService.sendEmail.bind(mailgunService);