We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40226
    • 10 Posts
    hi all,
    I'm using Modx Revo. I have a webpage that allows users to either login using their email address or register as a new user before going to the next page to select a file for downloading. Once a file is selected for download, I need to pass the username and the filename(for downloading) to the website administrator email just to inform that who and which file has been downloaded.

    On my 1st process page(after the html page which allows user to login and resgister as new user), I have the
    [[!Personalize? &yesChunk=`download` &noChunk=`register`]]
    .

    The yeschunk will bring the user straight to the documents download page which looks like this :
    <div class="clear"></div>
    <h3 class="downloads">File no.1</h3>
    <a class="btn_download" href="assets/files/file1.pdf">Download</a>
    <div class="clear"></div>
    <h3 class="downloads">File no.2</h3>
    <a class="btn_download" href="assets/files/file2.pdf">Download</a>


    Once the user has clicked on the download button on one of the files, I need to send the username who has logged in before this and the file title selected to the administrator email address, eg, [email protected]. How do I do the parameters passing for this??

    Please help me.. need this soulution badly in hurry.. >,<
    Thankssssssss

    This question has been answered by multiple community members. See the first response.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The username will be in the $_SESSION.

      Perhaps the download link should instead be to another page, with the file name in the URL, and that page will have a snippet to get the username from the $_SESSION as well as the file name from the $_GET. The snippet would then take care of the mail, and redirect to the file URL. I suspect that a lot of these sites with downloads where you are sent to an empty page where all you get is the download dialog is doing something like this.

      You could also set up the links as a form, where clicking on a link "submits" the form. Then you could use something like FormIt and have the form handle the mailing along with the username ($_SESSION again) and the file name from the $_POST.
        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
        • 40226
        • 10 Posts
        Hi Susan,
        Thanks for the reply, however, I'm pretty confuse on how to get the $_Session and $_Get in the first suggestion which you have mentioned by using the snippet and redirect to the file url as I'm all new to these, sorry about this. Would really appreciate if you can show me a simple example on how to get this running?
        Plsssss and thanksssssssssss >.<
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Ok, your links should be to another page, with the filename added to the URL:

          <a href="mysite.com/downloads/abcde.html?file=file1.pdf" &target="_blank">Download file1.pdf</a>


          then abcde.html (probably best if it has no template, since the user is only on it temporarily for downloading the file) will have a snippet using this to create the email and sent it:

          $username = $modx->getLoginUserName();
          $file = $_GET['file']; // Make sure to validate and sanitize this before using it!!!
          
          // something like this may be all you need
          $filename = preg_replace('/[^0-9a-z\.\_\-]/i','',$file)
          if(file_exists('assets/files/pdfs/' . $filename)) {
          
          $msg = "User $username downloaded $filename";
          ...
          //send the mail via your preferred mailing method
          ...
          
          // redirect the user to the file itself for downloading
          $modx -> sendRedirect('assets/files/pdfs/'. $filename);
          
          }
          // just quietly die if the sanitized file doesn't exist (perhaps somebody tried to substitute a malicious filename)
          return;
          
          
          
          
          
          
          
          [ed. note: sottwell last edited this post 14 years, 3 months ago.]
            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
            • 40226
            • 10 Posts
            Hi Susan,
            1)For the abcde.html, do i actually create it under the Download folder which is under the Chunks?

            2)For the 2nd part of your codes(the codes for sending out the notification email to the administrator
            and redirect the user for downloading the file), which you've mentioned to use a snippet for this,do i
            actually create a new snippet under the Snippets(from the Elements tab) and name it as an
            independent snippet by itself?? For example, by naming it as Sendinfo snippet. If yes, how do i get
            the abcde.html to refer to this new snippet?

            Or I have got all these wrong but just create the abcde.html under the Resources tab and the write in the 2nd part of codes in it?

            Sorry for asking such a silly questions as I'm still confuse on how the entire Modx works exactly.

            Many thanksssssss again >.<
              • 3749
              • 24,544 Posts
              abcde.html (presumably with a more user-friendly name) would be a Resource in the Resource tree with no template and just the snippet tag in the Resource Content field.

              Then you create a snippet containing Susan's code and put the name of that snippet in the snippet tag on the abcde.html page.

              Here's some simple mail code:

              $modx->getService('mail', 'mail.modPHPMailer');
              $modx->mail->set(modMail::MAIL_BODY, 'Some message');
              $modx->mail->set(modMail::MAIL_FROM, '[email protected]');
              $modx->mail->set(modMail::MAIL_FROM_NAME, 'Your Name');
              $modx->mail->set(modMail::MAIL_SENDER, '[email protected]');
              $modx->mail->set(modMail::MAIL_SUBJECT, 'Some Subject');
              $modx->mail->address('to', '[email protected]', 'UserName');
              $modx->mail->address('reply-to', '[email protected]');
              $modx->mail->setHTML(true);
              $sent = $modx->mail->send();
              if ($sent) {
                 /* mail sent successfully */
                 /* remove this next line when things are working */
                 $modx->log(modX::LOG_LEVEL_ERROR, 'Sent mail successfully to: ' . $username);
              } else {
                 /* mail failed  */
                 $modx->log(modX::LOG_LEVEL_ERROR, 'Failed sending mail to: ' . $username);
              }
              
              return $output;


              The log message will be in the Error log (Reports -> Error Log). The success messages will be marked as errors, but they're not.

              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 3 months ago.]
                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
                • 40226
                • 10 Posts
                Hi Bob,
                Thanks for the info.
                Before going to the abcde.html. At the download chunk which i have mentioned in my 1st message, i have put in the link as follows in order to get the page linked to abcde.html and also passing the file1 name over :
                <a href="http://www.mywebsite.com/index.php?id=41&file=file1.pdf">File No.1</a>


                Note : id 41 is the number of the abcde.html placed under the Resources tab.

                I have started my abcde.html by placing it under the Resources tab with just the following line in it :
                [[Sendinfo? &fullName='fullname' &file=`file1.pdf`]]


                The Seninfo is the snippet name which will contain the 2nd part of codes from Susan, The fullName is the username who has logged onto the website and this variable has been taken from the Personalize snippet(this has been mentioned in my 1st message too) where the part of code in the Personalize looks like this :
                if ($modx->user->hasSessionContext($modx->context->get('key')) ) {
                   $output =  $modx->getChunk($yesChunk);
                   if (isset($ph)) {
                      if ($fullName) {
                          $profile = $modx->user->getOne('Profile');
                          $modx->setPlaceholder($ph, $profile->get('fullname'));
                      } else {
                          $modx->setPlaceholder($ph, $modx->user->get('username'));
                      }
                   }
                } else {
                   $output = $modx->getChunk($noChunk);
                }


                I hope that fullName is teh correct variable that I'm using for passing the data into the abcde.html.
                However, before implementing Susan's code for the snippet, I have tried to run the page and it seems that there is no error but by clicking at the File No.1 link will only bring me back to the homepage of the website which seems like my data is not passing on. Am I right?

                Plssss help again.
                Thankssssssssssssssss
                • discuss.answer
                  • 3749
                  • 24,544 Posts
                  Try this first:

                  <a href="[[~41? &file=`file1.pdf`]]">File</a>
                  



                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    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
                    • 40226
                    • 10 Posts
                    thanks Bob and Susan for all the helps laugh
                    I got my page working now!
                    Thanksssssssssss smiley))))
                      • 3749
                      • 24,544 Posts
                      That's great. Thanks for reporting back. smiley


                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html
                        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