We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22673
    • 72 Posts
    Hi all,

    I’m a fairly new user to MODx and I’ve been looking all over to see if its possible to create an eForm which sends an email, but also uploads a file to the server. I can see some forum posts which describe how to create an eForm which uploads files to the email and are sent as attachments, but this isn’t what I need.

    Can anyone show me how to do this or point me in the direction of some documentation / post that can tell me?

    Any help much appreciated.

    Alex.
    • You need to create a new snippet with a function to use the &eFormOnValidate, &eFormOnBeforeMailSent or &eFormOnMailSent events to grab the $_FILE and save it to disk.
      function saveFile( &$fields ){
      // get the $_FILE data and save the file to disk
      $uploaddir = 'assets/files/';
      $uploadfile = $uploaddir . basename($_FILES['fieldname']['name']);
      if (move_uploaded_file($_FILES['fieldname']['tmp_name'], $uploadfile)) {
          echo "File is valid, and was successfully uploaded.\n";
      } else {
          echo "Possible file upload attack!\n";
      }
      echo '<pre>';
      echo 'Here is some more debugging info:';
      print_r($_FILES);
      echo '</pre>';
      return true;
      }	
      

      This snippet must be called before the eForm snippet call; in the eForm snippet call you would have the &eformOnBeforeMailSent=`saveFile` parameter (or wichever event you decide to use). Note that the function must return true, otherwise eForm will simply stop at that point.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 7130
        • 2 Posts
        Hi,

        that is exactly what i need for my form.
        I have a form where Information ist stored in a database via eform2db( [!GeldForm!] ).
        No i want to add an upload function for pdf files.
        So i created a snippet called "File" and copied the code
        function saveFile( &$fields ){
        // get the $_FILE data and save the file to disk
        $uploaddir = 'assets/files/';
        $uploadfile = $uploaddir . basename($_FILES['fieldname']['name']);
        if (move_uploaded_file($_FILES['fieldname']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.\n";
        } else {
            echo "Possible file upload attack!\n";
        }
        echo '<pre>';
        echo 'Here is some more debugging info:';
        print_r($_FILES);
        echo '</pre>';
        return true;
        }

        into.
        But how do i integrate the Snippet into my form?

        Can i use the &eFormOnBeforeMailSent twice in a snippetcall like this:

        [!GeldForm!]
        [!eForm? &formid=`geldformular` &eFormOnBeforeMailSent=`GeldForm` &eformOnBeforeMailSent=`File` &tpl=`GeldForm` &thankyou=`334` &vericode=`1` &subject=`Geldprämie`!]

        Sry for my bad english, i hope u can understand my question.
          • 38733
          • 22 Posts
          This worked great, as files can get big -- thanks again, Susan, from 3.5 years ago.

          But I reply as I also wonder how to call two snippets with eFormOnBeforeMailSent or the others?

          As a workaround, I just put all my code in one snippet.
            • 3749
            • 24,544 Posts
            You can use a plugin attached to that event, and in it, use $modx->runSnippet() if that would work for you, though there's nothing wrong with having everything in one snippet (and it's probably faster).
              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
              • 37210
              • 131 Posts
              I'm guessing there isn't an add-on available to do this kinda thing?