Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __BREAKING CHANGES:__
___
- 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)
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
- 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)

### 4.5.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)
Expand Down
26 changes: 26 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3168,3 +3168,29 @@ describe('afterLogin hook', () => {
await query.find({ context: { a: 'a' } });
});
});

describe('sendMail', () => {
it('can send email via Parse.Cloud', async done => {
const emailAdapter = {
sendMail: mailData => {
expect(mailData).toBeDefined();
expect(mailData.to).toBe('test');
done();
},
};
await reconfigureServer({
emailAdapter: emailAdapter,
});
const mailData = { to: 'test' };
await Parse.Cloud.sendMail(mailData);
});

it('cannot send email without adapter', async () => {
const logger = require('../lib/logger').logger;
spyOn(logger, 'warn').and.callFake(() => {});
await Parse.Cloud.sendMail({});
expect(logger.warn).toHaveBeenCalledWith(
'Failed to send email because no mail adapter is configured for Parse Server.'
);
});
});
31 changes: 31 additions & 0 deletions src/cloud-code/Parse.Cloud.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Parse } from 'parse/node';
import * as triggers from '../triggers';
const Config = require('../Config');

function isParseObjectConstructor(object) {
return typeof object === 'function' && Object.prototype.hasOwnProperty.call(object, 'className');
Expand Down Expand Up @@ -528,6 +529,36 @@ ParseCloud.beforeConnect = function (handler, validationHandler) {
);
};

/**
* Sends an email through the Parse Server mail adapter.
*
* **Available in Cloud Code only.**
* **Requires a mail adapter to be configured for Parse Server.**
*
* ```
* Parse.Cloud.sendMail({
* from: 'Example <test@example.com>',
* to: 'contact@example.com',
* subject: 'Test email.',
* text: 'This email is a test'
* });
*```
*
* @method sendMail
* @name Parse.Cloud.sendMail
* @param {Object} data The object of the mail data to send
*/
ParseCloud.sendMail = function (data) {
const config = Config.get(Parse.applicationId) || {};
const emailAdapter = config.userController.adapter;
if (emailAdapter) {
return emailAdapter.sendMail(data);
}
config.loggerController.warn(
'Failed to send email because no mail adapter is configured for Parse Server.'
);
};

/**
* Registers a before live query subscription function.
*
Expand Down