On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi.
    How to attach local file from server, not file submited through form?
    thx
    • Actually not a big problem. Just create custom snippet like this (call it Attachment)
      <?php
      if(!empty($attachment)){
        $basepath = $modx->getOption('base_path');
        $scriptProperties['hook']->modx->mail->mailer->AddAttachment($basepath . $attachment);
      }
      return true;


      And call formit like this.
      [[!FormIt?
      &attachment=`path_to_file_on_server`
      &hooks=`spam,email,Attachment,FormItAutoResponder,redirect`
      &emailTpl=`myTpl` etc


      Important put the Attachment after email in hooks parameter.
      • I'm having a problem with this, maybe someone can shine a light on this.
        I copied the code exactly as in dj13 example for the snippet.
        I checked the output of the path for the attachment and this is 100% correct.

        This is my FormIt call:
            [[!FormIt?
                &attachment=`pdf/[[+File]]`
                &hooks=`email,Attachment,redirect`
                &emailTpl=`emailTemplateBrochure`
                &emailTo=`[[+brochureEmail]]`
                &emailBCC=`info@internetmakkers.nl`
                &emailFrom=`info@leendersmakelaardij.nl`
                &emailFromName=`Leenders Makelaardij`
                &emailSubject=`Brochure opgevraagd op de site van Leenders Makelaardij`
                &redirectTo=`28`
                &validate=`brochureNaam:required, brochureEmail:email:required`
                &submitVar=`brochureSubmit`
            ]]
        

        The e-mail is sent and works fine, except for the pdf that is not attached to the e-mail.
        Anyone knows what goes wrong here?
        • Couldn't get it to work with dj13's example in the first place either. But after I switched the 'Attachment' with the 'email' in the '&hooks' line it worked:

          [[!FormIt?
          &attachment=`path_to_file_on_server`
          &hooks=`spam,Attachment,email,FormItAutoResponder,redirect`
          &emailTpl=`myTpl` etc
          • up !

            Hi everybody,

            It is not working for me ! i have a blank page after the "submit".
            this is my formit call :
            [[!FormIt?
            &attachment=`pdf/file.pdf`
            &hooks=`Attachment,email,redirect`
            &emailTpl=`devisTpl` 
            &emailSubject=`Subject` 
            &emailTo=`email@email.com` 
            &redirectTo=`6` 
            &validate=`nospam:blank,email:required`
            ]]
            


            here my Snippet:
            <?php
            if(!empty($attachment)){
              $basepath = $modx->getOption('/var/www/');
              $scriptProperties['hook']->modx->mail->mailer->AddAttachment($basepath.$attachment);
            }
            return true;
            


            Could someone help me please ?
            • I have the same issue as sironder. Any thoughts anyone?
                Twitter @alexmercenary
              • You must edit the code in both the formit call and the snippet so that it forms the correct full path to the file you want to attach.
                  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
                • Wow that was super fast Susan!! I think I understand, however: -

                  M one problem is that I have an auto created file on form submission which puts all the fields into a csv and then saves it to the server with a unique name based on the forename and surname fields and date. ie. alex-mercer-30-12-13.csv and then once this is created I want to attach it to the email. So I can't put the full path to the file in the formit call as the name is dynamic, based on user input. I have put it in the snippet like so:

                  $scriptProperties['hook']->modx->mail->mailer->AddAttachment($filepath);


                  But I'm just getting the white screen on submission.

                  Thanks for any advice
                    Twitter @alexmercenary
                  • Well, you'll have to get the filename somehow to use for the attachment. You should be able to generate the filename in code from the POST and the date, just as it is done for creating the actual file.
                      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
                    • I realize I'm once again resurrecting a long-dead thread, but I've spent several hours being extremely frustrated that there didn't seem to be an actual answer to this problem. Like sironder and alexmercenary, I was getting a blank white page when I attempted to implement the above solutions for emailing a local file using a custom Formit hook. Here's my simplified FormIt call (I'll use '***' throughout to designate redacted information):

                      [[!FormIt?
                          &hooks=`FormItAttach,email`
                          &emailTpl=`emailTpl`
                          &emailSubject=`***`
                          &emailTo=`***@***.***`
                      ]]

                      And here's a simplified version of my original "FormItAttach" snippet:

                      $attachment = '/tmp/***.***';
                      $scriptProperties['hook']->modx->mail->mailer->AddAttachment($attachment);
                      return true;

                      When I would submit my form, I would get a blank white page. After making sure everything was as it should be, I checked PHP's error log. Here's what I found:

                      [Wed Apr 30 10:56:35 2014] [error] [client ***] PHP Fatal error: Call to a member function AddAttachment() on a non-object in /***/core/cache/includes/elements/modsnippet/120.include.cache.php on line 31, referer: http://***/

                      This led me on a wild and frustrating Google-chase. Eventually I stumbled upon the documentation for modMail (http://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/modx-services/modmail). In the provided example, they used the following before they started creating an email:

                      $modx->getService('mail', 'mail.modPHPMailer');

                      Curious, I modified my "FormItAttach" snippet to the following:

                      $attachment = '/tmp/***.***';
                      $hook->modx->getService('mail', 'mail.modPHPMailer');
                      $hook->modx->mail->mailer->AddAttachment($attachment);
                      return true;

                      And, what do you know, it worked. I can't really tell you exactly why it worked—or why the previous solutions worked for some people, but not others, but this worked for me, and if I can save someone else a few frustrating hours of fruitless Googling, so be it.

                      That is all.