Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Recipient Based on Form Input

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    Question Recipient Based on Form Input

    Hello. I have two arrays of countries that match up with a <select> list.

    I want to send an email to a different recipient based on the country selected.

    Would I use an fmprefinishhook to check the value of the select field against the arrays and base the recipient upon whichever array the value exists in?

    Thanks in advance.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,980

    Default Re: Recipient Based on Form Input

    Hi,

    Yes, that's one way.

    If it's feasible, you can also just do this:

    HTML Code:
    <select name="recipients">
    <options value="email1">USA</options>
    <options value="email2">UK</options>
    <options value="email3">France</options>
    ...
     </select>
    Russell Robinson - Author of Tectite FormMail and FormMailDecoder
    http://www.tectite.com/

  3. #3
    Join Date
    Mar 2010
    Posts
    6

    Default Re: Recipient Based on Form Input

    Any best practice suggestions for using the prefinish hook? EDIT:: (Should I name the file:
    fmhookpreemail.inc.php to use the following code?)

    (as simple as something like:

    Code:
    $countryvalue = $aCleanedValues["Country"];
    require_once('countryarrays.php');
     
    if(in_array($countryvalue, $countryvaluearray)){
    $SPECIAL_VALUES["recipients"] = 'email@email.com';
    }elseif(in_array($countryvalue, $countryvaluearray2)){
    $SPECIAL_VALUES["recipients"] = 'email@email.com, email2@email.com';
    }else{
    $SPECIAL_VALUES["recipients"] = 'email@email.com';
    }
    )?

    Then I would completely remove the HTML hidden field from the form...that on the right track?

    I guess I was uncertain how to access the recipients variable from your script without the hard coded hidden value in the form HTML itself.

    I am unable to use the simple HTML select option as I need the country information for other purposes.

    Thanks again for your help!
    Last edited by samalotriviera; 22-Mar-2010 at 06:47 PM.

  4. #4
    Join Date
    May 2008
    Posts
    1,291

    Default Re: Recipient Based on Form Input

    Quote Originally Posted by samalotriviera View Post
    I am unable to use the simple HTML select option as I need the country information for other purposes.
    You can do the following:

    HTML Code:
    <select name="Country">
    <option value="USA">USA</option>
    <option value="UK">UK</option>
    <option value="France">France</option>
    <option value="Germany">Germany</option>
     </select>
    Then, you could have an INI file with the following email aliases:

    Code:
    [email_addresses]
    USA = email1@whatever.com
    UK = email2@whatever.com
    France = email3@whatever.com
    Germany = email4@whatever.com
    NOTE: There should be quotation marks around those email addresses. For some reason, the forum stripped them from my post even though I have them in code tags.

    Then, in your form you need to use the derive_fields tag:

    HTML Code:
    <input type="hidden" name="derive_fields" value="recipients=Country">
    This way the Country field will still be listed in your form results, and the INI file means the appropriate email address will be used without specifying any of them in the form itself.

  5. #5
    Join Date
    Mar 2010
    Posts
    6

    Default Re: Recipient Based on Form Input

    Thanks! I am grateful for the response. That solution does seem like it would work for me (though setting up the ini file will take some time as I have every country in the world listed in my form field... )

    But my curiosity remains. Could I do it the way I have mentioned in my second post using the fmhookpreemail.inc.php?

  6. #6
    Join Date
    May 2008
    Posts
    1,291

    Default Re: Recipient Based on Form Input

    Sorry, I haven't used that feature so I can't help you with that. Perhaps someone else will stop by with that info.

  7. #7
    Join Date
    Mar 2010
    Posts
    6

    Default Re: Recipient Based on Form Input

    From what I've seen on the forums I am reasonably sure my idea will work as well as yours. I haven't had time yet to test, but I am still hopeful that Russell will confirm or deny my idea so that we have it from the man himself.

    Many thanks to all.
    Last edited by samalotriviera; 22-Mar-2010 at 08:56 PM.

  8. #8
    Join Date
    Dec 2003
    Posts
    3,980

    Default Re: Recipient Based on Form Input

    Hi,

    Quote Originally Posted by samalotriviera View Post
    Any best practice suggestions for using the prefinish hook? EDIT:: (Should I name the file:
    fmhookpreemail.inc.php to use the following code?)

    (as simple as something like:

    Code:
    $countryvalue = $aCleanedValues["Country"];
    require_once('countryarrays.php');
     
    if(in_array($countryvalue, $countryvaluearray)){
    $SPECIAL_VALUES["recipients"] = 'email@email.com';
    }elseif(in_array($countryvalue, $countryvaluearray2)){
    $SPECIAL_VALUES["recipients"] = 'email@email.com, email2@email.com';
    }else{
    $SPECIAL_VALUES["recipients"] = 'email@email.com';
    }
    )?

    Then I would completely remove the HTML hidden field from the form...that on the right track?
    Yes, you're on the right track. And "fmhookpreemail.inc.php" is the right file name.

    As with any code, the devil is always in the details and I'd have to test it to be sure you've got everything right.

    But, "$aCleanedValues" is good for normal field values (or "$aRawDataValues" if you want the actual submitted value) and "$SPECIAL_VALUES" (for setting or getting) is right for special fields (provide it's done in the right place).

    The only caveat on using the hook system to add PHP code is that we are likely to change the code of FormMail extensively for version 9. You'll note we have no documentation for the hook system - and that's the reason.

    What this means is that when you add code to FormMail, you need to bear in mind that it might not work with a future version. If this happens, it's likely to be a minor problem, but we obviously can't guarantee anything.
    Russell Robinson - Author of Tectite FormMail and FormMailDecoder
    http://www.tectite.com/

  9. #9
    Join Date
    Mar 2010
    Posts
    6

    Default Re: Recipient Based on Form Input

    Many Thanks Russell. I certainly understand about the "devil being in the details" and that you provide no guarantee. Your script is phenomenally well written and personally I think the hook system is a great way to allow people to extend its functionality without compromising your fine work. I know that I've used the hook system to insert data in my database, etc. (hope it doesnt go away! in 9)

    I tested on our live server (bad bad!) but so far it has not sent me an email. I chose a country that would have sent an email to email1 and email2. So far no email, so there must be a gremlin in the code somewhere. It did report successful email transmission though.

    Im certain youre tired of this post but if you get a chance, I noted in your reply that the special values work "provided they are in the right place" Can you elaborate?

    Many thanks for your dedication to this script and the community of users.

    Cheers!
    Last edited by samalotriviera; 23-Mar-2010 at 12:48 PM.

  10. #10
    Join Date
    Mar 2010
    Posts
    6

    Default Re: Recipient Based on Form Input

    Got it working. It is necessary to leave a recipient field in the form as formmail checks for some kind of recipient before the preemailhook. An absence of any recipient (before the hook changes the recipients value) causes formmail to:
    PHP Code:
    if (!isset($SPECIAL_VALUES["recipients"]) || empty($SPECIAL_VALUES["recipients"]))
    {
            
    //
            // No recipients - don't email anyone...
            // If nothing has been done above (CSV, logging, or CRM),
            // then report an error.
            //
        
    if (!$bDoneSomething)
            if (!
    $bGotGoBack && !$bGotNextForm)
                
    ErrorWithIgnore("no_recipients",GetMessage(MSG_NO_ACTIONS));
    }
    else
    {
            
    //
            // Hook system: before sending emails
            //
        
    if ($HOOK_DIR !== "")
            if (!@include(
    "$HOOK_DIR/fmhookpreemail.inc.php"))
                @include(
    "$HOOK_DIR/fmhookpreemail.inc");
        
    $s_invalid $s_invalid_cc $s_invalid_bcc ""
    .......etc...


    So its working now. Thanks all!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. arghh.... recipient error
    By craigkendall in forum FormMail Subscription Support
    Replies: 2
    Last Post: 06-Sep-2004, 09:26 PM
  2. No Recipient - Error
    By Chocolinx in forum FormMail Subscription Support
    Replies: 1
    Last Post: 16-May-2004, 11:58 PM
  3. configure recipient email fileds
    By Luke in forum FormMail Subscription Support
    Replies: 1
    Last Post: 02-Apr-2004, 08:49 PM
  4. no recipient errors!
    By jeriy in forum FormMail Subscription Support
    Replies: 3
    Last Post: 20-Jan-2004, 09:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •