Skip to content

Commit d47891f

Browse files
authored
Send email via Parse.Cloud.sendEmail (#7096)
* initial * more tests * Update CHANGELOG.md * review * log on error * change logger to error * rename * Update Parse.Cloud.js
1 parent 029edbf commit d47891f

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __BREAKING CHANGES:__
88
___
99
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
1010
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
11+
- NEW: Added convenience method Parse.Cloud.sendMail(...) to send email via mail adapter in Cloud Code. [#7089](https://github.com/parse-community/parse-server/pull/7089). Thanks to [dblythy](https://github.com/dblythy)
1112

1213
### 4.5.0
1314
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)

spec/CloudCode.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -3168,3 +3168,29 @@ describe('afterLogin hook', () => {
31683168
await query.find({ context: { a: 'a' } });
31693169
});
31703170
});
3171+
3172+
describe('sendEmail', () => {
3173+
it('can send email via Parse.Cloud', async done => {
3174+
const emailAdapter = {
3175+
sendMail: mailData => {
3176+
expect(mailData).toBeDefined();
3177+
expect(mailData.to).toBe('test');
3178+
done();
3179+
},
3180+
};
3181+
await reconfigureServer({
3182+
emailAdapter: emailAdapter,
3183+
});
3184+
const mailData = { to: 'test' };
3185+
await Parse.Cloud.sendEmail(mailData);
3186+
});
3187+
3188+
it('cannot send email without adapter', async () => {
3189+
const logger = require('../lib/logger').logger;
3190+
spyOn(logger, 'error').and.callFake(() => {});
3191+
await Parse.Cloud.sendEmail({});
3192+
expect(logger.error).toHaveBeenCalledWith(
3193+
'Failed to send email because no mail adapter is configured for Parse Server.'
3194+
);
3195+
});
3196+
});

src/cloud-code/Parse.Cloud.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Parse } from 'parse/node';
22
import * as triggers from '../triggers';
3+
const Config = require('../Config');
34

45
function isParseObjectConstructor(object) {
56
return typeof object === 'function' && Object.prototype.hasOwnProperty.call(object, 'className');
@@ -528,6 +529,37 @@ ParseCloud.beforeConnect = function (handler, validationHandler) {
528529
);
529530
};
530531

532+
/**
533+
* Sends an email through the Parse Server mail adapter.
534+
*
535+
* **Available in Cloud Code only.**
536+
* **Requires a mail adapter to be configured for Parse Server.**
537+
*
538+
* ```
539+
* Parse.Cloud.sendEmail({
540+
* from: 'Example <test@example.com>',
541+
542+
* subject: 'Test email',
543+
* text: 'This email is a test.'
544+
* });
545+
*```
546+
*
547+
* @method sendEmail
548+
* @name Parse.Cloud.sendEmail
549+
* @param {Object} data The object of the mail data to send.
550+
*/
551+
ParseCloud.sendEmail = function (data) {
552+
const config = Config.get(Parse.applicationId);
553+
const emailAdapter = config.userController.adapter;
554+
if (!emailAdapter) {
555+
config.loggerController.error(
556+
'Failed to send email because no mail adapter is configured for Parse Server.'
557+
);
558+
return;
559+
}
560+
return emailAdapter.sendMail(data);
561+
};
562+
531563
/**
532564
* Registers a before live query subscription function.
533565
*

0 commit comments

Comments
 (0)