mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-01-25 02:08:24 +00:00
* fix hosted app, register by email * update mailgun configuration * update payment method when update card * fix * fix * fix * change free plan settings * fix forgot password * fix forgot password * fix * fix
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
const formData = require('form-data');
|
|
const Mailgun = require('mailgun.js');
|
|
const mailgun = new Mailgun(formData);
|
|
const bent = require('bent');
|
|
const validateEmail = (email) => {
|
|
// eslint-disable-next-line max-len
|
|
const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
return re.test(email);
|
|
};
|
|
|
|
const emailSimpleText = async(logger, to, subject, text) => {
|
|
const from = 'jambonz Support <support@jambonz.org>';
|
|
if (process.env.CUSTOM_EMAIL_VENDOR_URL) {
|
|
await sendEmailByCustomVendor(logger, from, to, subject, text);
|
|
} else {
|
|
await sendEmailByMailgun(logger, from, to, subject, text);
|
|
}
|
|
|
|
};
|
|
|
|
const sendEmailByCustomVendor = async(logger, from, to, subject, text) => {
|
|
try {
|
|
const post = bent('POST', {
|
|
'Content-Type': 'application/json',
|
|
...((process.env.CUSTOM_EMAIL_VENDOR_USERNAME && process.env.CUSTOM_EMAIL_VENDOR_PASSWORD) &&
|
|
({
|
|
'Authorization':`Basic ${Buffer.from(
|
|
`${process.env.CUSTOM_EMAIL_VENDOR_USERNAME}:${process.env.CUSTOM_EMAIL_VENDOR_PASSWORD}`
|
|
).toString('base64')}`
|
|
}))
|
|
});
|
|
|
|
const res = await post(process.env.CUSTOM_EMAIL_VENDOR_URL, {
|
|
from,
|
|
to,
|
|
subject,
|
|
text
|
|
});
|
|
logger.debug({
|
|
res
|
|
}, 'sent email to custom vendor.');
|
|
} catch (err) {
|
|
logger.info({
|
|
err
|
|
}, 'Error sending email From Custom email vendor');
|
|
}
|
|
};
|
|
|
|
const sendEmailByMailgun = async(logger, from, to, subject, text) => {
|
|
if (!process.env.MAILGUN_API_KEY) throw new Error('MAILGUN_API_KEY env variable is not defined!');
|
|
if (!process.env.MAILGUN_DOMAIN) throw new Error('MAILGUN_DOMAIN env variable is not defined!');
|
|
|
|
const mg = mailgun.client({
|
|
username: 'api',
|
|
key: process.env.MAILGUN_API_KEY,
|
|
...(process.env.MAILGUN_URL && {url: process.env.MAILGUN_URL})
|
|
});
|
|
|
|
try {
|
|
const res = await mg.messages.create(process.env.MAILGUN_DOMAIN, {
|
|
from,
|
|
to,
|
|
subject,
|
|
text
|
|
});
|
|
logger.debug({
|
|
res
|
|
}, 'sent email');
|
|
} catch (err) {
|
|
logger.info({
|
|
err
|
|
}, 'Error sending email From mailgun');
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
validateEmail,
|
|
emailSimpleText
|
|
};
|