Sending Cloud Code Emails

4 posts, 0 answers
  1. Daniel
    Daniel avatar
    2 posts
    Member since:
    Nov 2014

    Posted 25 Nov 2014 Link to this post

    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





  2. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 26 Nov 2014 Link to this post

    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.
     
  3. Daniel
    Daniel avatar
    2 posts
    Member since:
    Nov 2014

    Posted 26 Nov 2014 in reply to Anton Dobrev Link to this post

    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



  4. Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 27 Nov 2014 Link to this post

    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.
     
Back to Top