PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Automatic/Programmatic Email Routing in functions.php?

  1. Hi there, I was searching the forums to see if this discussion exists but when I click on a link it says I don't have access to view the thread, so hopefully this question gets answered.

    Is there any way to automate the routing of email notifications according to a certain form field? For example, a user will select a name/email from a drop down menu, and that submission gets sent to the selected name/email.

    From what I understand, you have to do routing manually, but is there a way to do this more quickly, similar to the filter used for adding predefined Bulk Add values in functions.php?

    I plan to pull name and email values from a custom post type's custom fields (which I've done with a Bulk Add filter for one of the fields), so it would be way better if I didn't have to enter them manually in case I add or edit something.

    Posted 14 years ago on Wednesday June 30, 2010 | Permalink
  2. Gravity Forms has built in notification routing, however you have to predefine who the emails should go to based on a form selection when setting up your notification.

    If you want it to be dynamic and based on post data you would have to write custom PHP that you place in your functions.php file to tell Gravity Forms what to do using an API hook.

    Here is a thread that discusses sending the notification email to the author of the post the form was submitted from:

    http://forum.gravityhelp.com/topic/sending-form-to-author-of-post

    From this thread you can get the code necessary to dynamically change the send to and repurpose it to use a custom field value rather than the author of the post.

    Posted 14 years ago on Wednesday June 30, 2010 | Permalink
  3. Thanks for this (using a different account). I'll try modifying the code you suggested, but I was wondering how I can access a form field value for the populate_email filter (instead of $userdata->user_email).

    Also, is this the better way to assign which email to send to, and not, say, by adding items to the routing logic list?

    Posted 14 years ago on Wednesday June 30, 2010 | Permalink
  4. Why would you route the email to a form field value when you can already use the built in User Notification to send the notification email to a form field value without writing additional code?

    Posted 14 years ago on Wednesday June 30, 2010 | Permalink
  5. Well yeah, but say I wanted the drop down menu to be a list of names and not email addresses (so it's more readable), how do I apply an email address to a form field when a user selects a name?

    Posted 14 years ago on Thursday July 1, 2010 | Permalink
  6. You use the email routing feature on the Admin Notification for the Send To address. You use that functionality to map an email address with a drop down selection.

    - Add a drop down to your form that consists of the names the user would select from.
    - Edit the Notifications for that form (select edit forms and then hover over the form and click the Notifications link)
    - Setup the Admin Notification
    - For the Send To email address select the Routing radio button
    - Setup routing rules for each name in your drop down (basically allows you to Send to john@doe.com if the name drop down = John Doe)

    Posted 14 years ago on Thursday July 1, 2010 | Permalink
  7. "Setup routing rules for each name in your drop down (basically allows you to Send to john@doe.com if the name drop down = John Doe)"

    See that's what I wanted to eliminate: manually entering those conditions so the client doesn't have to edit that every time he adds a new item to the list, especially if the list is large. I wanted to add all those conditions automatically.

    Posted 14 years ago on Thursday July 1, 2010 | Permalink
  8. The conditions can't be added automatically with the drop down. They are part of the notification setup. So regardless if you do it with code or if you use the email notification it would still basically have to be manually entered to know which email goes with which drop down selection.

    Posted 14 years ago on Thursday July 1, 2010 | Permalink
  9. Hi Splash,

    I'm not 100% sure what you're looking for, but you can use the "gform_pre_submission_filter" filter to customize the form itself. This means you can update the notification properties of the form and write a conditional that will do all the manual labor for you.

    To see what you have to work with, I'd recommend setting up a filter (instructions here) and just using php's print_r function to output the contents of the $form variable which is passed to the filter.

    If you have any more specific questions, feel free to hit me on my email: david@ounceoftalent.com

    Posted 14 years ago on Friday July 2, 2010 | Permalink
  10. @Carl: Yes, I get that I'd have to enter stuff either way but it would be more useful to the client if he can see all those notification settings displayed in the WP admin.

    @David: Thanks for the suggestion, but there doesn't seem to be a link in the "instructions here" text.

    So a list of options I could do:

    1. Do the routing in code
    2. Send notification to a form field which contains an email address
    3. Enter the routing conditions one by one in the admin

    #2 would be really great if there were a way to tie dropdown menu values to email addresses automatically, like:

    <option value="johndoe@gmail.com">John Doe</option>

    Or:

    <option value="John Doe">John Doe</option>

    updates another form field automatically using JavaScript.

    Posted 14 years ago on Tuesday July 6, 2010 | Permalink
  11. @splashpress The user COULD see all the notifications settings displayed in the WP admin. He would go to the Notifications page for that form and he could see the email to select mapping and change/update them. It wouldn't be hidden behind code. It's visible and configurable from the Notifications page for that Form in the Gravity Forms admin.

    Posted 14 years ago on Tuesday July 6, 2010 | Permalink
  12. I've got a related question where the solution splashpress is looking for would be valuable. Via other forum threads I have the drop-down displaying name and value as email for people to contact which is being pulled from entries in a custom post type.

    The alternate option - user in admin tweaking the notifications, is beyond the complexity of the user base I'm designing for. And from their perspective, add / remove an entry in the staff list should make the contact form auto-update. The display of name / email is, but the automation of routing is the missing piece of the puzzle.

    Thx,
    Jamie

    Posted 13 years ago on Saturday March 19, 2011 | Permalink
  13. Solved.

    <?php
    add_filter("gform_pre_submission_filter", "add_replyto");
    function add_replyto($form){
    
        //Change numbers to your actual form id
        if($form["id"] != 2)
            return $form;
    
        //Change input_2 with your actual input names
        //Change mymail@example.com to your actual emailaddress
        $form["notification"]["to"] = $_POST["input_5"];
    	//print_r($form["notification"]);
        return $form;
    }
    ?>
    Posted 13 years ago on Saturday March 19, 2011 | Permalink