We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46309
    • 79 Posts
    I'm using FileUpload snippet for one project and want to do the following:

    1. To send email notification to the admin or some email address when a file is uploaded from the user. I read in Bob's guide that there is such an option through the FileUpload Plugin but I have no idea how to do it.

    2. Because there is a big chance of users to upload files with same name I want to add some serial numbers or date at the end of the file name by default. For example file-1, file-2, etc. or file-2016-04-09, file-2016-04-10 etc. The first option with the numbers is preferable but the second with the dates is also ok. It's not necessary to check if a file with same name exist or not because I think it will be harder and not so suitable for my case.

    Here is my FileUpload code:
    [[!FileUpload?
        &filefields=`1`
        &uploadgroups=`Members`
        &createpath=`Yes`
        &path=`userfiles/[[+modx.user.username]]/`
        &maxsize=`1000000
        &allowoverwrite=`0`
        &dirpermission=`755`
    &extensions=`txt,html,htm,xml,zip,gz,rar,z,tgz,tar,pdf,doc,docx,xls,xlsx,ppt,pptx,jpg,jpeg,png,tiff,svg,svgz,gif,psd,ico,bmp,odt,ods,odp,odb,odg,odf`
    ]]


    And this is the Fileupload Plugin with OnFileManagerUpload checked:
    /* Connected to OnFileManagerUpload Event */
    
    if (! isset($fromfileupload)) {
        /* Event not fired by FileUpload snippet */
        return '';
    
    }
    
    /** @var  $path string */
    /** @var $modx modX */
    
    $directory = $path;
    
    foreach($files as $file) {
        $name = $file['name'];
        $type = $file['type'];
        $extension = $file['extension'];
        $size = $file['size'];
        $tmp_name = $file['tmp_name'];
        $error = $file['error'];
    
        /* Your code below. This is an example that writes results to the MODX error log.
    
         * Note: Since $file is an associative array, you can have a Tpl chunk with
         * placeholders and use $msg .= $modx->getChunk('MyChunk', $file) for sending
         * emails or making your own upload log;
         */
    
        if (empty($error)) {
            /* Success */
            $modx->log(modX::LOG_LEVEL_ERROR, 'Success: ' . $name);
        } else {
            /* Failure -- $error contains the error message */
            $modx->log(modX::LOG_LEVEL_ERROR, 'ERROR: ' . $name . ' -- ' . $error);
        }
    }
    
    return '';


    Any suggestions will be greatly appreciated.

    This question has been answered by deyand. See the first response.

      peace&love / bike&ride
      • 46309
      • 79 Posts
      Nobody knows how to do that?
      Especially the email notification.
        peace&love / bike&ride
        • 3749
        • 24,544 Posts
        Are you sure you have a potential problem with filename collisions? The files will be in separate directories, so the only problem is if a user uploads the same file twice, which should be harmless.

        For the email:

        /* Set these variables */
        $msg = ''; // The body of the message
        $from = ''; // Joe Blow <[email protected]
        $fromName = ''; // name of sender
        $sender = ''; // Sender's email address
        $subject = ''; // Email subject
        $reply = ''; // Reply-to email address
        /* ************ */
        
        /* Send the message */
        $modx->getService('mail', 'mail.modPHPMailer');
        $modx->mail->set(modMail::MAIL_BODY, $msg);
        $modx->mail->set(modMail::MAIL_FROM, $from);
        $modx->mail->set(modMail::MAIL_FROM_NAME, $fromName);
        $modx->mail->set(modMail::MAIL_SENDER, $sender);
        $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
        $modx->mail->address('to', $email, $name);
        $modx->mail->address('reply-to',$reply);
        $modx->mail->setHTML(true);
        $sent = $modx->mail->send();
        $modx->mail->reset();


          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
          • 46886
          • 1,154 Posts
          Thanks so much BobRay.
            • 46309
            • 79 Posts
            Thank you for the info Bob!

            I'll give it a try and write here some feedback.
              peace&love / bike&ride
              • 46309
              • 79 Posts
              Maybe I'm doing something wrong. I duplicated the FileUpload Plugin as written in the instruction, implemented the provided code inside, connected it to the OnFileManagerUpload event, and enabled it. But nothing happens.

              Here is the error log:
              [2016-04-27 14:30:48] (ERROR @ /xxx/xxx/public_html/subdomain/core/model/modx/mail/modphpmailer.class.php : 152) modPHPMailer does not support unsetting specific addresses. Use reset() to clear all recipients and add back the ones you want to send to.
              [2016-04-27 14:30:48] (ERROR @ /xxx/xxx/public_html/subdomain/core/cache/includes/elements/modplugin/5.include.cache.php : 77) An error occurred while trying to send the email: You must provide at least one recipient email address.
              [2016-04-27 14:30:48] (ERROR @ /xxx/xxx/public_html/subdomain/core/cache/includes/elements/modplugin/5.include.cache.php : 84) Success: test.docx


              Am I missing something here? I'm not very familiar with the modx plugins. Do I need to use chunks, placeholders or something else?

                peace&love / bike&ride
                • 3749
                • 24,544 Posts
                Did you put something sensible in all the quotation marks here?

                $msg = ''; // The body of the message
                $from = ''; // Joe Blow <[email protected]
                $fromName = ''; // name of sender
                $sender = ''; // Sender's email address
                $subject = ''; // Email subject
                $reply = ''; // Reply-to email address


                  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
                  • 46309
                  • 79 Posts
                  I tried both ways - with and without putting something in the quotation marks. Actually I don't know what exactly have to be placed inside the quotation marks especially for the first four values. Here is an example of my copied plugin called FileUploadClientsPlugin with OnFileManagerUpload checked:

                  <?php
                  /**
                   *  Copyright 2015 by Bob Ray
                   *
                   * fileupload plugin
                   *
                   * FileUpload is free software; you can redistribute it and/or modify it
                   * under the terms of the GNU General Public License as published by the Free
                   * Software Foundation; either version 2 of the License, or (at your option) any
                   * later version.
                   *
                   * FileUpload is distributed in the hope that it will be useful, but WITHOUT ANY
                   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
                   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
                   *
                   * You should have received a copy of the GNU General Public License along with
                   * FileUpload; if not, write to the Free Software Foundation, Inc., 59 Temple
                   * Place, Suite 330, Boston, MA 02111-1307 USA
                   *
                   * @package fileupload
                   *
                   */
                  
                  /* Connected to OnFileManagerUpload Event */
                  
                  if (! isset($fromfileupload)) {
                      /* Event not fired by FileUpload snippet */
                      return '';
                  
                  }
                  
                  /** @var  $path string */
                  /** @var $modx modX */
                  
                  $directory = $path;
                  
                  foreach($files as $file) {
                      $name = $file['name'];
                      $type = $file['type'];
                      $extension = $file['extension'];
                      $size = $file['size'];
                      $tmp_name = $file['tmp_name'];
                      $error = $file['error'];
                  
                      /* Your code below. This is an example that writes results to the MODX error log.
                  
                       * Note: Since $file is an associative array, you can have a Tpl chunk with
                       * placeholders and use $msg .= $modx->getChunk('MyChunk', $file) for sending
                       * emails or making your own upload log;
                       */
                  
                  //Email
                  
                  /* Set these variables */
                  $msg = ''; // The body of the message
                  $from = ''; // Joe Blow <[email protected]
                  $fromName = ''; // name of sender
                  $sender = ''; // Sender's email address
                  $subject = 'New File Upload'; // Email subject - tried also with empty value 
                  $reply = '[email protected]'; // Reply-to email address - tried also with empty value
                  /* ************ */
                   
                  /* Send the message */
                  $modx->getService('mail', 'mail.modPHPMailer');
                  $modx->mail->set(modMail::MAIL_BODY, $msg);
                  $modx->mail->set(modMail::MAIL_FROM, $from);
                  $modx->mail->set(modMail::MAIL_FROM_NAME, $fromName);
                  $modx->mail->set(modMail::MAIL_SENDER, $sender);
                  $modx->mail->set(modMail::MAIL_SUBJECT, $subject);
                  $modx->mail->address('to', $email, $name);
                  $modx->mail->address('reply-to',$reply);
                  $modx->mail->setHTML(true);
                  $sent = $modx->mail->send();
                  $modx->mail->reset();
                  
                      if (!$modx->mail->send()) {
                          $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
                      }else{
                       $modx->log(modX::LOG_LEVEL_ERROR,'This mail was sent: '.$message);
                      }
                  
                      if (empty($error)) {
                          /* Success */
                          $modx->log(modX::LOG_LEVEL_ERROR, 'Success: ' . $name);
                      } else {
                          /* Failure -- $error contains the error message */
                          $modx->log(modX::LOG_LEVEL_ERROR, 'ERROR: ' . $name . ' -- ' . $error);
                      }
                  }
                  
                   return '';


                  My FileUpload code is the same as the one from my first post. Tried with or without @FileUploadClientsPlugin? After the [[FileUpload… but still the same.

                  What kind of info and how have to be placed inside the quotation marks? The $reply and $subject can be fixed and known values but how to know the rest of the values which are not fixed, for example $sender which is the email of the sender.

                  Sorry for my stupid questions but if I know how to do this I wouldn’t be asking here.



                    peace&love / bike&ride
                    • 3749
                    • 24,544 Posts
                    I don't know what I can say beyond the comments on each line, but I'll try:

                    $msg = ''; // The body of the message,  i.e. the text of the message you want to send (with or without placeholders)
                    
                    $from = ''; // Joe Blow <[email protected]>, i.e., your email address
                    $fromName = ''; // name of sender, i.e. Your name
                    $sender = ''; // Sender's email address, use the same thing as $from.
                      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
                      • 46309
                      • 79 Posts
                      Nope, still the same...

                      Here is my FileUploadClientsPlugin with values in the quotation marks:
                      ...
                      //Email
                      
                      /* Set these variables */
                      $msg = 'A User uploaded a file to his profile.'; // The body of the message,  i.e. the text of the message you want to send (with or without placeholders)
                      $from = '[email protected]'; // Joe Blow <[email protected]>, i.e., your email address
                      $fromName = 'Admin'; // name of sender, i.e. Your name
                      $sender = 'Admin'; // Sender's email address, use the same thing as $from.
                      $subject = 'New File Upload'; // Email subject
                      $reply = '[email protected]'; // Reply-to email address
                      /* ************ */
                       
                      ...
                      [ed. note: deyand last edited this post 10 years, 5 months ago.]
                        peace&love / bike&ride