We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49311
    • 22 Posts
    I am trying to get my form to function by sending out separate emails depending on the options the user selects (multiple selects).

    For example, if a user is interested in products A, B and C, upon submit, A will go to one email address, B to another and C to a third.

    I am using this snippet (placeholder emails in place);

    <?php
    // Type: Snippet
    //$recipient = $hook->getValue('products_interested[]');
    $formFields = $hook->getValues();
    $recipient = $formFields['products_interested[]'];
    
    
    $recipientList = array();
    
    if (strpos($recipient,'Product A') !== false) {
        array_push($recipientList, "email A");
    }
    
    if (strpos($recipient,'Product B') !== false) {
        array_push($recipientList, "email B");
    }
    
    if (strpos($recipient,'Product C') !== false) {
        array_push($recipientList, "email C");
    }
    
    
    else{
        //$email = 'email Admin';
    }
    
    $result = array_unique($recipientList);
    //$result = substr(implode(",", $result), 0);
    
    echo $result;
    $hook->setValue('sendTo',$result);
    
    return true;
    //



    In my chunk, I have this call:

    [[!FormIt? 
      &hooks=`recipient,email,redirect` 
      &emailTpl=`adminEmailRequestAQuote` 
      &emailTo=`email Address`
      &emailCC=`[[+sendTo]]`
      &emailSubject='Request a Quote'
      &emailMultiSeparator=`, `
      &redirectTo=`1` 
      &validate=`products_interested:required,
      contact_email_phone:required,
      mm_subscribe:required` ]]


    When I take `[[+sendTo]]` out of FormIt, it will show the applicable email address in the email response, but when I apply it to &emailCC, the emails will not go out to separate emails.

    Any thoughts or suggestions would be greatly appreciated!

    Thanks.
      • 3749
      • 24,544 Posts
      Your multi separator in the snippet tag is `, `, but your implode it just ',', so the separator probably isn't being found. I'd change the one in the snippet tag to `,` and just use this (the substr isn't doing anything):

      $result = implode(",", $result);




        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 49311
        • 22 Posts
        Thanks for the quick reply Bob.

        I'm now basing the what I'm trying to do on what you've previously helped out with in this thread: https://forums.modx.com/thread/71174/formit-with-conditional-emailto

        Putting the "recipient" snippet above the FormIt tag and setting the "sendTo" placeholder in the snippet got the multiple email recipients working.

        Thanks for your help!

        Modified Chunk Code:
        [[!recipient? &input=[[+products_interested]] ]]
        [[!FormIt? 
          &hooks=`email,redirect` 
          &emailTpl=`adminEmailRequestAQuote` 
          &emailTo=`[[+sendTo]]`
          &emailSubject='Request a Quote'
          &emailMultiSeparator=`, `
          &redirectTo=`1` 
          &validate=`products_interested:required` ]]
        
        <p>[[!+fi.validation_error_message:notempty=`
        [[!+fi.validation_error_message]]</p>
        `]]</p>
        
        <form action="[[~[[*id]]]]" method="post" class="form">
          <input type="hidden" name="nospam:blank" value="[[+fi.nospam]]" />
        
        
        <div class="form-group">
        
        
         <div class="form-group">
          <label class="control-label" for="products_interested">What product/services are you interested in? * <span class="error">[[!+fi.error.products_interested]]</span></label> 
        
        
        <div class="checkbox">
          <label>
            <input name="products_interested[]" type="checkbox" value="val1" [[!+fi.products_interested:FormItIsChecked=`val1`]]>Value 1
          </label>
        </div>
        <div class="checkbox">
          <label>
            <input name="products_interested[]" type="checkbox" value="val2"[[!+fi.products_interested:FormItIsChecked=`val2`]] >Value 2
          </label>
        </div>
        
        <label>
            <input name="products_interested[]" type="checkbox" value="val3"[[!+fi.products_interested:FormItIsChecked=`val3`]] >Value 3
          </label>
        </div>
        
        
        <br class="clear" />
          [[!+formit.recaptcha_html]] [[!+fi.error.recaptcha]] <br class="clear" />
        
        <div class="form-group">
        
            <button id="submit" name="submit" type="submit" class="btn btn-primary">Submit</button>
        </div>
        
        </form>
        
        


        Modified Snippet Code:
         
        <?php
        // Type: Snippet
        
        $recipient = $_POST['products_interested'];
        
        if ($recipient == '') { return ''; }
          foreach ($recipient as $key => $value) {
            switch ($value)
            {
              case "val1":
                $recipientList[] = '[email protected]';
                break;
              case "val2":
                $recipientList[] = '[email protected]';
                break;
              case "val3":
                $recipientList[] = '[email protected]';
                break;
              default:
                $recipientList[] = '[email protected]';
            }
          }
        
        $modx->setPlaceHolder('sendTo', implode(', ', array_unique($recipientList)));
        
          • 3749
          • 24,544 Posts
          Looks good. I'm glad you got it working. smiley
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting