Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Edge 26 posts 80 karma points
    Apr 04, 2017 @ 04:14
    Anthony Edge
    0

    Also send enquiries to an email address managed in Page Nodes?

    We're looking to add form submission email recipients on a per-page level under Umbraco > Content nodes. Usual requirements for our clients is to specify a default To: recipient email address in the root node, then allow an override email address per page (if populated).

    Populating a generic Formulate variable on the Page Template from a CMS field seems like it could be a simple way to facilitate this, with Formulate "watching" for this value and utilising it when present. ie, var formulateToEmail = @Umbraco.Field("formulateToEmail") or some such.

    Of course, this rather basic idea would not play nicely when two forms are present on one page (which is common for us too). But maybe there's a clue in this approach that someone, more tech savvy than I, could build upon.

    I know Nicholas has tackled such a situation before and would love to have that approach documented here. It would be ideal if any such bespoke solution would "survive" a package update to newer versions of Formulate as they are released.

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 04, 2017 @ 04:44
    Nicholas Westby
    0

    I actually haven't done the same situation, but a similar situation. In my case, I generated an email address based on values submitted via two form fields, as well as some content nodes used to store settings. However, my implementation was very similar to what you'd want to do. I'll explain roughly what you'd want to do below.

    At a high level, what you'll want to do is modify the form during submission to set a field value to an email address, then be sure you configure the "Send Email" handler to email to that field:

    Recipients

    First, subscribe to this event: https://github.com/rhythmagency/formulate/blob/e1063adaf5a9cb5f5a6ce07adb8fefae81a522ca/src/formulate.api/Submissions.cs#L52

    In your event handler, you'll modify the field value. In order to do that, you'll need to identify the field. I recommend doing that by creating a field category (just a config file edit), then assigning that category to the field. I believe I have used a field of type "Text" for this purpose. You can add the field to your form without adding it to the layout (there is no need for it to be rendered to the page).

    The form submission handler would look a bit like this (this code may contain typos):

    private static void SetEmailFieldDynamically(FormSubmissionContext context)
    {
    
        // Find the field.
        var field = context.Form.Fields
            .FirstOrDefault(x => x.Category == "Dynamic Email Field");
    
        // If the field wasn't found, this is probably another form, so exit now.
        if (field == null)
        {
            return;
        }
    
        // Get or create the field data.
        var newData = new List<FieldSubmission>(context.Data);
        var fieldData = newData.FirstOrDefault(x => x.FieldId == field.Id);
        if (fieldData == null)
        {
            fieldData = new FieldSubmission()
            {
                FieldId = field.Id
            };
            newData.Add(fieldData);
            context.Data = newData;
        }
    
        // Set field value.
        var email = context.CurrentPage.GetPropertyValue<string>("dynamicEmail");
        fieldData.FieldValues = new List<string>() { email };
    
    }
    

    You'll need to make modifications for further customizations (e.g., if you want to set a fallback email address when one is not specified in the property on the current page).

    Note also that the context object has a few other properties you may find useful: https://github.com/rhythmagency/formulate/blob/cc8ad53128594739731141e2ed94c867087ea143/src/formulate.app/Forms/FormSubmissionContext.cs

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Jun 12, 2018 @ 00:33
    Nicholas Westby
    0

    FYI, I wrote an article about how to send emails from Formulate with dynamic recipients: https://code101.net/dynamic-email-recipients-with-umbraco-formulate-c927943d4ba5

Please Sign in or register to post replies

Write your reply to:

Draft