Skip to content

Commit 06f3cd1

Browse files
authored
Merge pull request #26 from mtrezza/fix-send-email-without-template
Fix send email without template
2 parents e487dcf + a54e918 commit 06f3cd1

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
# main
44
[Full Changelog](https://github.com/mtrezza/parse-server-api-mail-adapter/compare/1.0.5...master)
55

6+
### ⚠️ Breaking Changes
7+
*(none)*
8+
9+
### 🚀 Notable Changes
10+
*(none)*
11+
12+
### 🧬 Other Changes
13+
- Fixes failing to send email in Cloud Code without template (Manuel Trezza) [#26](https://github.com/mtrezza/parse-server-api-mail-adapter/pull/26)
14+
615
# 1.0.5
716
[Full Changelog](https://github.com/mtrezza/parse-server-api-mail-adapter/compare/1.0.4...1.0.5)
817

spec/ApiMailAdapter.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ describe('ApiMailAdapter', () => {
251251
expect(_sendMail.calls.all()[0].args[0]).toEqual(expectedArguments);
252252
});
253253

254+
it('allows sendMail() without using a template', async () => {
255+
const adapter = new ApiMailAdapter(config);
256+
const apiCallbackSpy = spyOn(adapter, 'apiCallback').and.callFake(apiResponseSuccess);
257+
const options = {
258+
sender: config.sender,
259+
recipient: '[email protected]',
260+
subject: 'ExampleSubject',
261+
text: 'ExampleText',
262+
html: 'ExampleHtml',
263+
};
264+
265+
await expectAsync(adapter.sendMail(options)).toBeResolved();
266+
const apiPayload = apiCallbackSpy.calls.all()[0].args[0].payload;
267+
expect(apiPayload.from).toEqual(options.sender);
268+
expect(apiPayload.to).toEqual(options.recipient);
269+
expect(apiPayload.subject).toEqual(options.subject);
270+
expect(apiPayload.text).toEqual(options.text);
271+
expect(apiPayload.html).toEqual(options.html);
272+
});
273+
254274
it('passes user to callback when user is passed to sendMail()', async () => {
255275
const adapter = new ApiMailAdapter(config);
256276
const localeCallbackSpy = spyOn(config.templates.customEmailWithLocaleCallback, 'localeCallback').and.callThrough();
@@ -385,7 +405,6 @@ describe('ApiMailAdapter', () => {
385405
const adapter = new ApiMailAdapter(config);
386406
const configs = [
387407
{ templateName: 'invalid' },
388-
{ templateName: 'invalid', direct: true }
389408
];
390409
for (const config of configs) {
391410
await expectAsync(adapter._sendMail(config)).toBeRejectedWith(Errors.Error.noTemplateWithName('invalid'));

src/ApiMailAdapter.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ class ApiMailAdapter extends MailAdapter {
125125
const templateName = email.templateName;
126126

127127
// If template name is not set
128-
if (!templateName) {
128+
if (!templateName && !email.direct) {
129129
throw Errors.Error.templateConfigurationNoName;
130130
}
131131

132132
// Get template
133133
const template = this.templates[templateName];
134134

135135
// If template does not exist
136-
if (!template) {
136+
if (!template && !email.direct) {
137137
throw Errors.Error.noTemplateWithName(templateName);
138138
}
139139

@@ -142,7 +142,12 @@ class ApiMailAdapter extends MailAdapter {
142142
// 1. Placeholders set in the template (default)
143143
// 2. Placeholders set in the email
144144
// 3. Placeholders returned by the placeholder callback
145-
const placeholders = template.placeholders || {};
145+
let placeholders = {};
146+
147+
// Add template placeholders
148+
if (template) {
149+
placeholders = Object.assign(placeholders, template.placeholders || {});
150+
}
146151

147152
// If the email is sent directly via Cloud Code
148153
if (email.direct) {
@@ -218,7 +223,7 @@ class ApiMailAdapter extends MailAdapter {
218223
*/
219224
async _createApiData(options) {
220225
let { message } = options;
221-
const { template, user, placeholders = {} } = options;
226+
const { template = {}, user, placeholders = {} } = options;
222227
const { placeholderCallback, localeCallback } = template;
223228
let locale;
224229

0 commit comments

Comments
 (0)