We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    Is there a plugin that will send an email when a document is published or edited? I thought I saw this before but have not found anything. Would OnDocPublished and OnDocFormSave be the best events for this?
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 4310
      • 2,310 Posts
      Here’s a modified version of one originally written by Rachel Black.
      I doubt it needs much explanation.
      I’ve used a chunk named "Emailer" with the placeholders and email text.
      The plugin would need the appropriate System Event ticked.
      Tested quickly, but it seems to work.
      $e = &$modx->Event;
      if($e->name == 'OnDocFormSave') {
      $pageid = $_POST['id'];
      $pagetitle = $_POST['pagetitle'];
      $editor = $modx->getLoginUserID() ;
      $editedby = $modx->db->getValue('SELECT `username` FROM `modx_manager_users` WHERE `id` = 1');
      $message = $modx->getChunk('Emailer');
      $message = str_replace("[+id+]", $_POST['id'], $message);
      $message = str_replace("[+pagetitle+]", $_POST['pagetitle'], $message);
      $message = str_replace("[+editedby+]", $editedby, $message);
      $to = '[email protected]';
      $subject = "Doc edited report";
      $sender = $modx->config['emailsender'];
      if(!mail("$to", $emailsubject, $message, "From: ".$sender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion()))
      {
      $modx->logEvent(1,3,"$to, $subject, $message, $sender", "Email");
      }
      return true;
      }
        • 4310
        • 2,310 Posts
        Getting carried away with this plugin business.
        Here’s an improved version with configuration parameters for :
        which plugin event, to email address, email subject & chunk name.
        Plugin code :
        $e = &$modx->Event;
        
        if($e->name == ''.$piEvent.'') {
        $pageid = $_POST['id'];
        $pagetitle = $_POST['pagetitle'];
        $editor = $modx->getLoginUserID() ;
        $editedby = $modx->db->getValue('SELECT `username` FROM `modx_manager_users` WHERE `id` = 1');
        
        $message = $modx->getChunk(''.$tplChunk.'');
        $message = str_replace("[+id+]", $_POST['id'], $message);
        $message = str_replace("[+pagetitle+]", $_POST['pagetitle'], $message);
        $message = str_replace("[+editedby+]", $editedby, $message);
        
        $sender = $modx->config['emailsender'];
        
        if(!mail("$to", $subject, $message, "From: ".$sender."\r\n"."X-Mailer: Content Manager - PHP/".phpversion()))
        {
        $modx->logEvent(1,3,"$to, $subject, $message, $sender", "Email");
        }
        return true;
        }

        Plugin configuration settings :
        &to=Mail To;string;[email protected] &piEvent=Plug In Event;string;OnDocFormSave &tplChunk=Chunk Name;string;Emailer &subject=Email Subject;string;Who's been messing with my site?
          • 7231
          • 4,205 Posts
          Thanks, that is a great help indeed. grin

          BTW - I have often wondered, is $_POST[’documentVariable’] the way to access document variables in a plugin? What about template variables, can these be accessed in the same way $_POST[’tvName’]?
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don't know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 33337
            • 3,975 Posts
            This sounds helpful.

            Guys, be sure to put in the repo and not let this cool stuff buried in the forums.

            Thanks.
              Zaigham R - MODX Professional | Skype | Email | Twitter

              Digging the interwebs for #MODX gems and bringing it to you. modx.link
              • 4310
              • 2,310 Posts
              This is a print of the TV array using the event OnTVFormSave
              Array
              (
                  [id] => 11
                  [mode] => 301
                  [params] => 
                  [stay] => 
                  [name] => Name
                  [caption] => Name
                  [description] => Name
                  [type] => richtext
                  [elements] => 
                  [default_text] => @DOCUMENT 37
                  [display] => 
                  [rank] => 0
                  [template] => Array
                      (
                          [0] => 6
                          [1] => 11
                      )
              
                  [chkalldocs] => on
                  [categoryid] => 16
                  [newcategory] => 
                  [save] => Submit Query
              )
              

              So I suppose you could access them via a plugin using the appropriate $_POST[’whatever’] format.
              Adding on to the original plugin you can add the conent of the page with $_POST[’ta’] so you could see what they’d done!