Exemplo n.º 1
0
        public static JObject FormSubmit(JObject form, SettingsDTO settings)
        {
            if (form != null)
            {
                HandlebarsEngine hbs      = new HandlebarsEngine();
                dynamic          data     = null;
                string           formData = "";
                if (form != null)
                {
                    /*
                     * if (!string.IsNullOrEmpty(settings.Settings.SiteKey))
                     * {
                     *  Recaptcha recaptcha = new Recaptcha(settings.Settings.SiteKey, settings.Settings.SecretKey);
                     *  RecaptchaValidationResult validationResult = recaptcha.Validate(form["recaptcha"].ToString());
                     *  if (!validationResult.Succeeded)
                     *  {
                     *      return Request.CreateResponse(HttpStatusCode.Forbidden);
                     *  }
                     *  form.Remove("recaptcha");
                     * }
                     */
                    data = FormUtils.GenerateFormData(form.ToString(), out formData);
                }
                // Send emails
                string Message = "Form submitted.";
                var    Errors  = new List <string>();
                if (settings != null && settings.Notifications != null)
                {
                    foreach (var notification in settings.Notifications)
                    {
                        try
                        {
                            MailAddress from  = FormUtils.GenerateMailAddress(notification.From, notification.FromEmail, notification.FromName, notification.FromEmailField, notification.FromNameField, form);
                            MailAddress to    = FormUtils.GenerateMailAddress(notification.To, notification.ToEmail, notification.ToName, notification.ToEmailField, notification.ToNameField, form);
                            MailAddress reply = null;
                            if (!string.IsNullOrEmpty(notification.ReplyTo))
                            {
                                reply = FormUtils.GenerateMailAddress(notification.ReplyTo, notification.ReplyToEmail, notification.ReplyToName, notification.ReplyToEmailField, notification.ReplyToNameField, form);
                            }
                            string body = formData;
                            if (!string.IsNullOrEmpty(notification.EmailBody))
                            {
                                body = hbs.Execute(notification.EmailBody, data);
                            }

                            string send = FormUtils.SendMail(from.ToString(), to.ToString(), (reply == null ? "" : reply.ToString()), notification.EmailSubject, body);
                            if (!string.IsNullOrEmpty(send))
                            {
                                Errors.Add("From:" + from.ToString() + " - To:" + to.ToString() + " - " + send);
                            }
                        }
                        catch (Exception exc)
                        {
                            Errors.Add("Notification " + (settings.Notifications.IndexOf(notification) + 1) + " : " + exc.Message);
                            Log.Logger.Error(exc);
                        }
                    }
                }
                if (settings != null && settings.Settings != null)
                {
                    if (!string.IsNullOrEmpty(settings.Settings.Message))
                    {
                        Message = hbs.Execute(settings.Settings.Message, data);
                    }
                    else
                    {
                        Message = "Message sended.";
                    }
                    //Tracking = settings.Settings.Tracking;
                    if (!string.IsNullOrEmpty(settings.Settings.Tracking))
                    {
                        //res.RedirectUrl = Globals.NavigateURL(ActiveModule.TabID, "", "result=" + content.ContentId);
                    }
                }
                var res = new JObject();
                res["message"] = Message;
                return(res);
            }
            return(null);
        }
Exemplo n.º 2
0
 public static JObject FormSubmit(JObject form, SettingsDTO settings, JObject item = null)
 {
     if (form != null)
     {
         HandlebarsEngine hbs      = new HandlebarsEngine();
         dynamic          data     = null;
         string           formData = "";
         if (form != null)
         {
             /*
              * if (!string.IsNullOrEmpty(settings.Settings.SiteKey))
              * {
              *  Recaptcha recaptcha = new Recaptcha(settings.Settings.SiteKey, settings.Settings.SecretKey);
              *  RecaptchaValidationResult validationResult = recaptcha.Validate(form["recaptcha"].ToString());
              *  if (!validationResult.Succeeded)
              *  {
              *      return Request.CreateResponse(HttpStatusCode.Forbidden);
              *  }
              *  form.Remove("recaptcha");
              * }
              */
             data = FormUtils.GenerateFormData(form.ToString(), out formData);
             if (item != null)
             {
                 data.Item = JsonUtils.JsonToDynamic(item.ToString());
             }
         }
         // Send emails
         string message = "Form submitted.";
         var    errors  = new List <string>();
         if (settings?.Notifications != null)
         {
             foreach (var notification in settings.Notifications)
             {
                 try
                 {
                     ProcessTemplates(hbs, data, notification);
                     MailAddress from  = FormUtils.GenerateMailAddress(notification.From, notification.FromEmail, notification.FromName, notification.FromEmailField, notification.FromNameField, form);
                     MailAddress to    = FormUtils.GenerateMailAddress(notification.To, notification.ToEmail, notification.ToName, notification.ToEmailField, notification.ToNameField, form);
                     MailAddress reply = null;
                     if (!string.IsNullOrEmpty(notification.ReplyTo))
                     {
                         reply = FormUtils.GenerateMailAddress(notification.ReplyTo, notification.ReplyToEmail, notification.ReplyToName, notification.ReplyToEmailField, notification.ReplyToNameField, form);
                     }
                     string body = formData;
                     if (!string.IsNullOrEmpty(notification.EmailBody))
                     {
                         try
                         {
                             body = hbs.Execute(notification.EmailBody, data);
                         }
                         catch (Exception ex)
                         {
                             throw new Exception("Email Body : " + ex.Message, ex);
                         }
                     }
                     string subject = notification.EmailSubject;
                     if (!string.IsNullOrEmpty(notification.EmailSubject))
                     {
                         try
                         {
                             subject = hbs.Execute(notification.EmailSubject, data);
                             subject = HttpUtility.HtmlDecode(subject);
                         }
                         catch (Exception ex)
                         {
                             throw new Exception("Email Subject : " + ex.Message, ex);
                         }
                     }
                     var attachements = new List <Attachment>();
                     if (form["Files"] is JArray)
                     {
                         foreach (var fileItem in form["Files"] as JArray)
                         {
                             var file = FileManager.Instance.GetFile((int)fileItem["id"]);
                             attachements.Add(new Attachment(FileManager.Instance.GetFileContent(file), fileItem["name"].ToString()));
                         }
                     }
                     string send = FormUtils.SendMail(from.ToString(), to.ToString(), reply?.ToString() ?? "", notification.CcEmails, notification.BccEmails, subject, body, attachements);
                     if (!string.IsNullOrEmpty(send))
                     {
                         errors.Add("From:" + from.ToString() + " - To:" + to.ToString() + " - " + send);
                     }
                 }
                 catch (Exception exc)
                 {
                     errors.Add("Error in Email Notification " + (settings.Notifications.IndexOf(notification) + 1) + " : " + exc.Message);
                     App.Services.Logger.Error(exc);
                 }
             }
         }
         if (settings?.Settings != null)
         {
             if (!string.IsNullOrEmpty(settings.Settings.Message))
             {
                 message = hbs.Execute(settings.Settings.Message, data);
             }
             else
             {
                 message = "Message sent.";
             }
             //Tracking = settings.Settings.Tracking;
             if (!string.IsNullOrEmpty(settings.Settings.Tracking))
             {
                 //res.RedirectUrl = Globals.NavigateURL(ActiveModule.TabID, "", "result=" + content.ContentId);
             }
         }
         var res = new JObject();
         res["message"] = message;
         res["errors"]  = new JArray(errors);
         return(res);
     }
     return(null);
 }