-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from mailersend/bulk_emails
Added bulk emails functionality
- Loading branch information
Showing
8 changed files
with
195 additions
and
23 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use strict"; | ||
require('dotenv').config() | ||
|
||
const MailerSend = require("../../src/MailerSend"); | ||
|
||
const mailersend = new MailerSend({ | ||
api_key: process.env.API_KEY, | ||
}); | ||
|
||
mailersend.getBulkEmailRequestStatus({ | ||
bulk_email_id: 'xxx' | ||
}) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
console.log(data); | ||
}); | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
require('dotenv').config() | ||
|
||
const Recipient = require("../../src/Recipient"); | ||
const EmailParams = require("../../src/EmailParams"); | ||
const MailerSend = require("../../src/MailerSend"); | ||
const BulkEmails = require("../../src/BulkEmails"); | ||
|
||
const mailersend = new MailerSend({ | ||
api_key: process.env.API_KEY, | ||
}); | ||
|
||
const bulkEmails = new BulkEmails(); | ||
|
||
const recipients = [ | ||
new Recipient("your@client.com", "Your Client") | ||
]; | ||
|
||
const emailParams = new EmailParams() | ||
.setFrom("your@domain.com") | ||
.setFromName("Your Name") | ||
.setRecipients(recipients) | ||
.setSubject("Subject") | ||
.setHtml("This is the HTML content") | ||
.setText("This is the text content"); | ||
|
||
|
||
bulkEmails.addEmail(emailParams) | ||
bulkEmails.addEmails([ | ||
emailParams, | ||
emailParams | ||
]) | ||
|
||
mailersend.sendBulk(bulkEmails) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
console.log(data); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use strict"; | ||
const EmailObject = require("../src/EmailObject"); | ||
|
||
module.exports = class BulkEmails { | ||
constructor() { | ||
this.emails = []; | ||
} | ||
|
||
addEmail(emailParams) { | ||
const emailObject = new EmailObject(emailParams) | ||
this.emails.push(emailObject.data); | ||
} | ||
|
||
addEmails(emailsParamsArray) { | ||
const _self = this; | ||
|
||
emailsParamsArray.forEach(function (emailParams) { | ||
_self.addEmail(emailParams); | ||
}) | ||
} | ||
|
||
flush() { | ||
this.emails = []; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use strict"; | ||
|
||
module.exports = class EmailObject { | ||
constructor(emailParams) { | ||
this.data = { | ||
from: { | ||
email: emailParams.from, | ||
name: emailParams.fromName, | ||
}, | ||
to: emailParams.recipients, | ||
cc: emailParams.cc, | ||
bcc: emailParams.bcc, | ||
reply_to: emailParams.replyTo, | ||
attachments: emailParams.attachments, | ||
subject: emailParams.subject, | ||
text: emailParams.text, | ||
html: emailParams.html, | ||
template_id: emailParams.templateId, | ||
variables: emailParams.variables, | ||
personalization: emailParams.personalization, | ||
tags: emailParams.tags, | ||
}; | ||
} | ||
}; |
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
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,25 +1,30 @@ | ||
const EmailObject = require("mailersend").EmailObject; | ||
|
||
module.exports = { | ||
send(emailParams) { | ||
let emailObject = new EmailObject(emailParams) | ||
|
||
return this.request("/email", { | ||
method: "POST", | ||
body: { | ||
from: { | ||
email: emailParams.from, | ||
name: emailParams.fromName, | ||
}, | ||
to: emailParams.recipients, | ||
cc: emailParams.cc, | ||
bcc: emailParams.bcc, | ||
reply_to: emailParams.reply_to, | ||
attachments: emailParams.attachments, | ||
subject: emailParams.subject, | ||
text: emailParams.text, | ||
html: emailParams.html, | ||
template_id: emailParams.templateId, | ||
variables: emailParams.variables, | ||
personalization: emailParams.personalization, | ||
tags: emailParams.tags, | ||
} | ||
body: emailObject.data | ||
}); | ||
} | ||
}, | ||
|
||
sendBulk(bulkEmails) { | ||
const emails = bulkEmails.emails; | ||
bulkEmails.flush(); | ||
|
||
return this.request("/bulk-email", { | ||
method: "POST", | ||
body: emails | ||
}); | ||
}, | ||
|
||
getBulkEmailRequestStatus(params) { | ||
const { bulk_email_id } = params | ||
|
||
return this.request(`/bulk-email/${bulk_email_id}`, { | ||
method: "GET" | ||
}); | ||
}, | ||
} |