Search code examples
jqueryparsingmailgun

How to send an email on a button click with jquery, Parse, mailgun?


I want a mail to be sent to the user when a button is clicked on a webpage. Is there a way to achieve this using with jquery, Parse, mailgun? From Parse console I can invoke Parse.Cloud function using curl. But how to invoke the "sendMail" function on a button click inside $("#myButton").click()?


Solution

  • I have done this and here is how I did it.

    http://www.chilifunfactory.com/chili-blog/send-emails-with-parse-and-mailgun

    You will need to write some cloud code in Parse.

        Parse.Cloud.define("sendMail", function(request, response) {
            var mailgun = require('mailgun');
    
            mailgun.initialize('your domain.mailgun.org', 'key-your-key');
            mailgun.sendEmail({
                    to: request.params.toEmail,
                    from: request.params.fromEmail,
                    subject: request.params.subject,
                    text: request.params.comments
                }, 
                {
                    success: function(httpResponse) {
                        console.log(httpResponse);
                        response.success("Email sent!");
                },
                    error: function(httpResponse) {
                    console.error(httpResponse);
                    response.error("Uh oh, something went wrong");
                }
            });
        });
    

    Then in your javascript you can simply call the method and send your email

        Parse.Cloud.run('sendMail', 
           { 
               "toEmail": "customer service email address",
               "fromEmail": "customer email address,
               "subject": "Subject",
               "comments": "Customer Comments"
           }, 
           {
               success: function(result) {
                   alert(result); 
               },
               error: function(error) {
                   alert(error.message);
               }
            }
        );