@b2y/email-service 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Config.js +4 -0
- package/constants/AppConstants.js +8 -0
- package/constants/StatusMessageConstants.js +2 -1
- package/enum/EmailProvider.js +2 -1
- package/index.js +5 -0
- package/package.json +3 -1
- package/providers/MailgunProvider.js +85 -0
package/Config.js
CHANGED
|
@@ -19,6 +19,10 @@ const Config = {
|
|
|
19
19
|
postmarkApiKey: process.env.ACCESS_KEY,
|
|
20
20
|
postmarkFromEmail: process.env.FROM_EMAIL,
|
|
21
21
|
|
|
22
|
+
// MailGun Configuration
|
|
23
|
+
mailgunApiKey: process.env.ACCESS_KEY,
|
|
24
|
+
mailgunDomain: process.env.ACCESS_DOMAIN,
|
|
25
|
+
|
|
22
26
|
// Amazon SES Configuration
|
|
23
27
|
awsRegion: process.env.REGION,
|
|
24
28
|
awsAccessKeyId: process.env.ACCESS_KEY,
|
|
@@ -33,7 +33,8 @@ const StatusMessage = {
|
|
|
33
33
|
NODEMAILER_SEND_ERROR:'Nodemailer failed to send email',
|
|
34
34
|
POSTMARK_SEND_ERROR:'Postmark failed to send email',
|
|
35
35
|
SENDGRID_SEND_ERROR:'SendGrid failed to send email',
|
|
36
|
-
AMAZON_SES_SEND_ERROR: 'Amazon SES failed to send email'
|
|
36
|
+
AMAZON_SES_SEND_ERROR: 'Amazon SES failed to send email',
|
|
37
|
+
MAILGUN_SEND_ERROR: 'Mailgun failed to send email',
|
|
37
38
|
};
|
|
38
39
|
|
|
39
40
|
module.exports = StatusMessage;
|
package/enum/EmailProvider.js
CHANGED
package/index.js
CHANGED
|
@@ -3,6 +3,7 @@ const NodeMailerProvider = require('./providers/NodeMailerProvider');
|
|
|
3
3
|
const SendgridProvider = require('./providers/SendgridProvider');
|
|
4
4
|
const PostmarkProvider = require('./providers/PostmarkProvider');
|
|
5
5
|
const AmazonSesProvider = require('./providers/AmazonSESProvider');
|
|
6
|
+
const MailgunProvider = require('./providers/MailgunProvider');
|
|
6
7
|
const TemplateEngine = require('./utils/TemplateEngine');
|
|
7
8
|
const EmailService = require('./service/EmailService');
|
|
8
9
|
const Config = require('./Config');
|
|
@@ -10,6 +11,7 @@ const logger = require('./Logger');
|
|
|
10
11
|
const { sequelize, EmailTemplate } = require('./model/Connect');
|
|
11
12
|
const { EmailProvider } = require('./enum/EmailProvider');
|
|
12
13
|
const StatusMessage = require('./constants/StatusMessageConstants');
|
|
14
|
+
|
|
13
15
|
// Initialize database connection
|
|
14
16
|
async function initializeDatabase() {
|
|
15
17
|
try {
|
|
@@ -35,6 +37,8 @@ function createEmailProvider() {
|
|
|
35
37
|
return new PostmarkProvider();
|
|
36
38
|
case EmailProvider.AMAZON_SES:
|
|
37
39
|
return new AmazonSesProvider();
|
|
40
|
+
case EmailProvider.MAILGUN:
|
|
41
|
+
return new MailgunProvider();
|
|
38
42
|
default:
|
|
39
43
|
logger.warn(StatusMessage.PROVIDER_DEFAULT_FALLBACK);
|
|
40
44
|
return new NodeMailerProvider();
|
|
@@ -69,6 +73,7 @@ module.exports = {
|
|
|
69
73
|
SendgridProvider,
|
|
70
74
|
PostmarkProvider,
|
|
71
75
|
AmazonSesProvider,
|
|
76
|
+
MailgunProvider,
|
|
72
77
|
EmailService,
|
|
73
78
|
TemplateEngine,
|
|
74
79
|
createEmailProvider,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b2y/email-service",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -42,8 +42,10 @@
|
|
|
42
42
|
"@sendgrid/mail": "^8.1.5",
|
|
43
43
|
"b2y-logger": "^1.1.0",
|
|
44
44
|
"dotenv": "^16.5.0",
|
|
45
|
+
"form-data": "^4.0.5",
|
|
45
46
|
"handlebars": "^4.7.8",
|
|
46
47
|
"log4js": "^6.9.1",
|
|
48
|
+
"mailgun.js": "^12.7.0",
|
|
47
49
|
"nodemailer": "^6.10.0",
|
|
48
50
|
"pg": "^8.16.3",
|
|
49
51
|
"pg-hstore": "^2.3.4",
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// providers/MailgunProvider.js
|
|
2
|
+
const formData = require('form-data');
|
|
3
|
+
const Mailgun = require('mailgun.js');
|
|
4
|
+
const BaseProvider = require('./BaseProvider');
|
|
5
|
+
const Config = require('../Config');
|
|
6
|
+
const logger = require('../Logger');
|
|
7
|
+
const StatusMessage = require('../constants/StatusMessageConstants');
|
|
8
|
+
const { EmailProvider } = require('../enum/EmailProvider');
|
|
9
|
+
const AppConstants = require('../constants/AppConstants');
|
|
10
|
+
class MailgunProvider extends BaseProvider {
|
|
11
|
+
constructor() {
|
|
12
|
+
super();
|
|
13
|
+
|
|
14
|
+
if (!Config.mailgunApiKey) {
|
|
15
|
+
throw new Error('Mailgun API key is required');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!Config.mailgunDomain) {
|
|
19
|
+
throw new Error('Mailgun domain is required');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const mailgun = new Mailgun(formData);
|
|
23
|
+
this.client = mailgun.client({
|
|
24
|
+
username: AppConstants.MAILGUN_AUTH_USERNAME,
|
|
25
|
+
key: Config.mailgunApiKey
|
|
26
|
+
});
|
|
27
|
+
this.domain = Config.mailgunDomain;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async sendEmail(options) {
|
|
31
|
+
try {
|
|
32
|
+
this.validateEmailOptions(options);
|
|
33
|
+
|
|
34
|
+
const messageData = {
|
|
35
|
+
from: this.formatFromAddress(
|
|
36
|
+
options.from || Config.fromEmail,
|
|
37
|
+
options.fromName || Config.fromName
|
|
38
|
+
),
|
|
39
|
+
to: this.formatRecipients(options.to),
|
|
40
|
+
subject: options.subject,
|
|
41
|
+
html: options.html
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Add CC if present
|
|
45
|
+
if (options.cc && options.cc.length > 0) {
|
|
46
|
+
messageData.cc = this.formatRecipients(options.cc);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Add BCC if present
|
|
50
|
+
if (options.bcc && options.bcc.length > 0) {
|
|
51
|
+
messageData.bcc = this.formatRecipients(options.bcc);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Add attachments if present
|
|
55
|
+
if (options.attachments && options.attachments.length > 0) {
|
|
56
|
+
messageData.attachment = options.attachments.map(att => ({
|
|
57
|
+
filename: att.filename,
|
|
58
|
+
data: att.content,
|
|
59
|
+
contentType: att.contentType
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const result = await this.client.messages.create(this.domain, messageData);
|
|
64
|
+
|
|
65
|
+
logger.info('Email sent successfully via Mailgun', {
|
|
66
|
+
messageId: result.id,
|
|
67
|
+
recipients: { to: options.to, cc: options.cc, bcc: options.bcc },
|
|
68
|
+
tenantId: options.tenantId,
|
|
69
|
+
from: messageData.from
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
success: true,
|
|
74
|
+
messageId: result.id,
|
|
75
|
+
provider: EmailProvider.MAILGUN,
|
|
76
|
+
tenantId: options.tenantId
|
|
77
|
+
};
|
|
78
|
+
} catch (error) {
|
|
79
|
+
logger.error('Mailgun failed to send email:', error);
|
|
80
|
+
throw new Error(StatusMessage.MAILGUN_SEND_ERROR);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
module.exports = MailgunProvider;
|