Skip to content

Commit

Permalink
Merge pull request #24 from mailersend/bulk_emails
Browse files Browse the repository at this point in the history
Added bulk emails functionality
  • Loading branch information
JuliusB authored Nov 29, 2021
2 parents 25f9c3c + 5a5ab3e commit b8b61b3
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 23 deletions.
65 changes: 61 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ MailerSend Node.js SDK
+ [Advanced personalization](#advanced-personalization)
+ [Simple personalization](#simple-personalization)
+ [Send email with attachment](#send-email-with-attachment)
+ [Send bulk emails](#send-bulk-emails)
+ [Get bulk request status](#get-bulk-request-status)
* [Tokens](#tokens)
+ [Create a token](#create-a-token)
+ [Update token](#update-token)
Expand Down Expand Up @@ -265,6 +267,62 @@ const emailParams = new EmailParams()
mailersend.send(emailParams);
```

### Send bulk emails
```js
const Recipient = require("mailersend").Recipient;
const EmailParams = require("mailersend").EmailParams;
const BulkEmails = require("mailersend").BulkEmails;
const MailerSend = require("mailersend");

const mailersend = new MailerSend({
api_key: "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);
});
```

### Get bulk request status
```js
const MailerSend = require("mailersend");

const mailersend = new MailerSend({
api_key: "key",
});

mailersend.getBulkEmailRequestStatus({
bulk_email_id: 'xxx'
})
.then((response) => response.json())
.then((data) => {
console.log(data);
});
```

## Tokens

### Create a token
Expand Down Expand Up @@ -294,7 +352,6 @@ mailersend.createToken({
.then(data => {
console.log(data);
});

```

### Update token
Expand Down Expand Up @@ -524,7 +581,7 @@ mailersend.domainSettings({
console.log(data);
});
```
### Add a domain
### Add a domain

```js
const MailerSend = require("mailersend");
Expand All @@ -545,7 +602,7 @@ mailersend.addDomain({
});
```

### Get DNS records
### Get DNS records

```js
const MailerSend = require("mailersend");
Expand All @@ -563,7 +620,7 @@ mailersend.getDNS({
});
```

### Verify a domain
### Verify a domain

```js
const MailerSend = require("mailersend");
Expand Down
18 changes: 18 additions & 0 deletions examples/email/getBulkEmailRequestStatus.js
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);
});


38 changes: 38 additions & 0 deletions examples/email/sendBulkEmail.js
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);
});
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const Recipient = require("./src/Recipient");
const EmailParams = require("./src/EmailParams");
const MailerSend = require("./src/MailerSend");
const Attachment = require("./src/Attachment");
const EmailObject = require("./src/EmailObject");
const BulkEmails = require("./src/BulkEmails");

module.exports = MailerSend;
module.exports.EmailParams = EmailParams;
module.exports.Recipient = Recipient;
module.exports.Attachment = Attachment;
module.exports.EmailObject = EmailObject;
module.exports.BulkEmails = BulkEmails;
25 changes: 25 additions & 0 deletions src/BulkEmails.js
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 = [];
}
};
24 changes: 24 additions & 0 deletions src/EmailObject.js
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,
};
}
};
1 change: 1 addition & 0 deletions src/EmailParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = class EmailParams {
this.recipients = config.recipients;
this.cc = config.cc;
this.bcc = config.bcc;
this.replyTo = config.replyTo;
this.attachments = config.attachments;
this.subject = config.subject;
this.html = config.html;
Expand Down
43 changes: 24 additions & 19 deletions src/modules/email.js
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"
});
},
}

0 comments on commit b8b61b3

Please sign in to comment.