kentico-mvckentico-12

Submitting form item via code not triggering an email notification


I am trying to submit a new form item via api on my MVC web app (grabbed the code from form's code tab) but the email notification is not working. It did create a new record except for the email alert. I checked the email queue but no records of the form item. I configured the smtp server settings properly. I checked the event logs as well but I dont see any errors. Am I missing something or this feature is only on portal engine?

enter image description here


Solution

  • Apparently if we do the submission of form manually via code and not using the default mvc form widgets it does not trigger the notification automatically. We also need to send the notifications via code. form data documentation

    // Gets the form object representing the 'ContactUs' form on the current site
    BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);
    
    if (formObject != null)
    {
    // Gets the class name of the 'ContactUs' form
    DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
    string formClassName = formClass.ClassName;
    
    // Creates a new data record for the form
    BizFormItem newFormItem = BizFormItem.New(formClassName);
    
    // Sets the values for the form's fields (UserMessage in this case)
    newFormItem.SetValue("UserMessage", "This is a message submitted through the API.");
    
    // Saves the new form record into the database
    // Set values for all 'Required' fields in the form before calling the Insert method, otherwise an exception will occur
    newFormItem.Insert();
    
    // Obtains a factory object used to create a form notification sender service for the given form
    IBizFormMailSenderFactory senderFactory = Service.Resolve<IBizFormMailSenderFactory>();
    
    // Creates an instance of the form notification sender for the inserted form item
    IBizFormMailSender sender = senderFactory.GetFormMailSender(formObject, newFormItem);
    
    // Sends a notification email to users (as specified on the form's 'Email notification' tab)
    sender.SendNotificationEmail();
    
    // Sends a confirmation email to the submitter (based on the form's autoresponder settings)
    sender.SendConfirmationEmail();
    }