This is a migrated thread and some comments may be shown as answers.

Sending Cloud Code Emails

3 Answers 229 Views
Cloud Code
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 25 Nov 2014, 04:50 PM
HI there,

I'm new to the Telerik Platform and am amazed at what it has to offer.

One thing I am trying to do, and make sure that it is possible to do is sending triggered emails based on an event (afterCreate for example) or Cron type jobs.

I guess that is one question!

The other is consuming user data in emails. I have a test template setup with a variable which is populated fine but am having difficulty consuming for example the use email that has triggered the 'afterCreate' event.

I have this code in which I want the recipient to be the email of the user

Everlive.Events.afterCreate(function(request, response, context, done) {
 
   var templateName = "CPDPoints";
    var recipients = request.data.Email; // need user email
    var emailContext = {
        CPDValue: request.data.CPDValue, //value from Activities content type
        DisplayName: request.data.DisplayName // need user displayname
     };
     
    Everlive.Email.sendEmailFromTemplate(templateName, recipients, emailContext, function(err, result) {
   
    done();
         
    });
 
done();
});


Any help would be great.

Thanks,
Daniel





3 Answers, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 26 Nov 2014, 02:14 PM
Hello Daniel,

Thank you for posting to the Telerik Developer Forums.

As to your questions.

1. Yes, you can send an email based on the events in the Cloud Code or from a Cloud function.

2. I am assuming that the email is send in the afterCreate event of the Users content type. In such case you should have the Email field in the request.data.Email key of the request data. However, it appears that there is an error when sending the email and you can handle it in the callback.

In the current case the problem is that the recipients object is not an array.

A working example:
Everlive.Events.afterCreate(function(request, response, context, done) {
 
    var templateName = "CPDPoints";
    var recipients = [request.data.Email]; // the recipients parameter must always be an array
 
    var emailContext = {
        CPDValue: request.data.CPDValue, //value from Activities content type
        DisplayName: request.data.DisplayName // need user displayname
    };
 
    Everlive.Email.sendEmailFromTemplate(templateName, recipients, emailContext, function(err, result) {
 
        if (err) {
            console.log("Error: " + err.message);
        } else {
            console.log("Success");
        }
        done();
    });
    // comment the done() below, otherwise it will be parsed and executed and the result of the sendEmailFFromTemplate will not be awaited
    // done();
});

You can also explore the documentation resources about validating and modifying requests. For instance, you may need to validate the Email field in the beforeCreate event and modify the request if the email is not valid or present.

Let me know if you have questions.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
Daniel
Top achievements
Rank 1
answered on 26 Nov 2014, 02:33 PM
Hi Anton,

Thanks for your reply which helps clarify it all a bit more.

The email is triggered upon creation of an activity (from your Friends sample which I'm messing with!) so it uses the Activity content type.

I managed to get the requested data I needed, by adding the fields to the Activity content type, then populating them from the client side upon activity creation.

Is that the best approach, or is there a better server side way of doing it? 

Just trying different aspects of the system during trial, but really happy with it so far, great job!

Thanks again,
Daniel



0
Anton Dobrev
Telerik team
answered on 27 Nov 2014, 11:37 AM
Hi,

If the request is authenticated (e.g. a Authorization: Bearer ACCESS_TOKEN_HERE header is sent to the server), you receive the request.principal.data object that contains the whole information about the user making the request.

For instance:
Everlive.Events.beforeCreate(function(request, context, done) {
    if (request.principal && request.principal.data && request.principal.data.Email) {
        // send email here
        var templateName = "CPDPoints";
        var recipients = [request.data.principal.Email]; // the recipients parameter must always be an array
 
       // verify the request.data.CPDValue too
        var emailContext = {
            CPDValue: request.data.CPDValue, //value from Activities content type
            DisplayName: request.data.DisplayName // need user displayname
        };
 
        Everlive.Email.sendEmailFromTemplate(templateName, recipients, emailContext, function(err, result) {
 
            if (err) {
                console.log("Error: " + err.message);
            } else {
                console.log("Success");
            }
            done();
        });
    } else {
        var customErrorCode = 100007;
        var httpErrorCode = 403;
        Everlive.Response.setErrorResult("Error message here", customErrorCode, httpErrorCode);
        done();
    }
});

In the cloud code you have access to the whole request object and can validate each of its keys. You can output the request in the cloud console with console.log(JSON.stringify(request)); or use the Interactive Debugging to observe the request and the data in it.

If you need information about items that are not in the request, use the JavaScript SDK integration in the Cloud Code to access data from other content types.

I hope that this helps.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
Cloud Code
Asked by
Daniel
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
Daniel
Top achievements
Rank 1
Share this question
or