Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,39 @@ const server = new ParseServer({
});
```

### Example for ZeptoMail Service

This is an example for the ZeptoMail Service client using the ZeptoMail JavaScript SDK.
Provide comma separated email adddresses in recepient parameter to send to multiple.

```js
// Configure mail client
var { SendMailClient } = require('zeptomail');

const url = process.env.ZEPTOMAIL_URL;
const token = process.env.ZEPTOMAIL_TOKEN;
let zeptoMaiClient = new SendMailClient({url, token});


// Configure Parse Server
const server = new ParseServer({
...otherServerOptions,

emailAdapter: {
module: 'parse-server-api-mail-adapter',
options: {
... otherAdapterOptions,

apiCallback: async ({ payload, locale }) => {
const zeptoMailPayload = ApiPayloadConverter.zeptomail(payload);
await zeptoMaiClient.sendMail(zeptoMailPayload);
},
}
}
});
```


## Custom API

This is an example of how the API payload can be adapted in the adapter configuration `apiCallback` according to a custom email provider's API specification.
Expand Down
59 changes: 59 additions & 0 deletions spec/ApiMailAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,65 @@ describe('ApiMailAdapter', () => {
expect(payload.Message.Body.Text.Data).toBe(examplePayload.text);
expect(payload.Message.Body.Html.Data).toBe(examplePayload.html);
});

it('converts payload for ZeptoMail for single recepient', () => {
const payload = converter.zeptomail(examplePayload);
expect(payload.from.address).toEqual(examplePayload.from);
// Check if 'to' is an array
expect(payload.to).toBeInstanceOf(Array);
// Check if the array has at least one element
expect(payload.to.length).toBe(1);
expect(payload.to[0].email_address.address).toEqual(examplePayload.to);

// Check if 'to' is an array
expect(payload.reply_to).toBeInstanceOf(Array);

// Check if the array has at least one element
expect(payload.reply_to.length).toBe(1);
expect(payload.reply_to[0].address).toEqual(examplePayload.replyTo);
expect(payload.subject).toBe(examplePayload.subject);
expect(payload.textbody).toBe(examplePayload.text);
expect(payload.htmlbody).toBe(examplePayload.html);
});

it('converts payload for ZeptoMail for multiple recepients', () => {

const multipleRecepientExamplePayload = {
from: "[email protected]",
to: "[email protected],[email protected]",
replyTo: "[email protected], [email protected]",
subject: "ExampleSubject",
text: "ExampleText",
html: "ExampleHtml"
}

const payload = converter.zeptomail(multipleRecepientExamplePayload);

expect(payload.from.address).toEqual(examplePayload.from);

// Check if 'to' is an array
expect(payload.to).toBeInstanceOf(Array);
//test multiple to addresses
const toAddresses = payload.to.map(entry => entry.email_address.address);
payload.to.forEach((entry, index) => {

expect(entry.email_address.address).toBe(toAddresses[index]);

});

// Check if 'reply_to' is an array
expect(payload.reply_to).toBeInstanceOf(Array);
//test multiple to addresses
const replyToAddresses = payload.reply_to[0].address.split(',').map(addr => addr.trim());
const [firstAddress, secondAddress] = replyToAddresses;
expect(replyToAddresses).toContain(firstAddress);
expect(replyToAddresses).toContain(secondAddress);

expect(payload.subject).toBe(multipleRecepientExamplePayload.subject);
expect(payload.textbody).toBe(multipleRecepientExamplePayload.text);
expect(payload.htmlbody).toBe(multipleRecepientExamplePayload.html);
});

});

describe('invoke _sendMail', function () {
Expand Down
62 changes: 62 additions & 0 deletions src/ApiPayloadConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,68 @@ class ApiPayloadConverter {

return payload;
}

/**
* @description Converts the mail payload for the ZeptoMail.
* @param {Object} originalPayload The original payload (provider agnostic).
* @returns {Object} The payload according to ZeptoMail SDK specification.
*/
static zeptomail(originalPayload) {

// Clone payload
const payload = Object.assign({}, originalPayload);

// Transform sender
payload.from = {
address: payload.from
}

// Extract the string of email addresses, need to be comma separated if multiple
const emailString = payload.to;
const emailAddresses = emailString.split(',').map(email => email.trim());
const formattedEmails = emailAddresses.map((address) => ({
email_address: {
address: address.trim()
}
}));
payload.to = formattedEmails

// Transform reply-to
if (payload.replyTo) {
payload.reply_to = [{
address: payload.replyTo
}
];
delete payload.replyTo;
}

// If message has content
if (payload.subject || payload.textbody || payload.htmlbody) {

// If message has body

if (payload.text || payload.html) {

// Set default body
payload.textbody = {};

// Transform plain-text
if (payload.text) {
payload.textbody = payload.text,
delete payload.text;
}

// Transform HTML
if (payload.html) {
payload.htmlbody = payload.html
delete payload.html;
}
}
}

return payload;
}

}

module.exports = ApiPayloadConverter;