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

    I am looking for a way to send a simple email notification to an email address every time a web user logs into a site. I found a plugin code that someone wrote for evolution which I have been trying to get to work but so far with no luck. Am I on the right lines with this?

    I have created a plugin and selected onWebLogin and onManagerLogin events. My current code is:

    <?php 
    $e = &$modx->Event;
    
    global $modx;
    
     
    
    if($email && $wun && $e->name == "OnWebLogin"){//check Web User Names
        $aWun = explode(',',$wun);
        foreach($aWun as $un){//loop usernames
    
            if($username == trim($un)){
    
                $subject = 'Web User Login';
    
                $message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
    
                sendEmail($email, $subject, $message);
    
            }
    
        }
    
    }
    
     
    
    if($email && $mun && $e->name == "OnManagerLogin"){//check Manager User Names
    
        $aMun = explode(',',$mun);
    
        foreach($aMun as $un){//loop username
    
            if($username == trim($un)){
    
                $subject = 'Manager User Login';
    
                $message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
    
                sendEmail($email, $subject, $message);
    
            }
    
        }
    
    }
    
     $to = "***MyEmailAddress***";
    
    // Send email
    function sendEmail($to, $subject, $message) {
    
        $headers ="MIME-Version: 1.0\n".
    
                  "Content-type: text/html; charset=iso-8859-1\n".
    
                  "From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
    
                  "X-Mailer: PHP/".phpversion();
    
     
    
    $body = <<<EOD
    
    $message
    EOD;
    
       $mail = mail($to, $subject, $body, $headers);
    
    }
    



    Any help would be greatly appreciated!
      • 3749
      • 24,544 Posts
      I think it could be much simpler in Revolution. I don't think you need anything more than something like this (untested):

      function sendEmail($subject, $message) {
       
          $headers ="MIME-Version: 1.0\n".
       
                    "Content-type: text/html; charset=iso-8859-1\n".
       
                    "From: ".$_SERVER['SERVER_NAME']."<no-reply@{$_SERVER['HTTP_HOST']}>\n".
       
                    "X-Mailer: PHP/".phpversion();
       
         $body =  $message
      
       
         $mail = mail('[email protected]', $subject, $body, $headers);
       
      }
      
      switch ($modx->event->name) {
          case 'OnManagerLogin':
             $subject = 'Manager User Login';
             break;
          case 'OnWebLogin':
             $subject = 'Web User Login';
             break;
      }
      
      $username = $user->get('username');
      $profile = $user->getOne('Profile');
      $email = $profile->get('email');
      
      $message = $username . ' has logged in on ' . date('l jS \of F Y h:i:s A');
      sendEmail($subject, $message);
      


      Note that the email function should go at the top, and I would test the function in a snippet, where debugging will be much easier, before putting it in the plugin.

      BTW, if a lot of users are logging in, I would be tempted to create a log file (like the error log) and a snippet to display it (with a button to clear the log) rather than getting a whole bunch of emails. You could also very easily log the information to the existing Manager Actions log with $modx->logManagerAction(). If you do it right, that will record the time, username, and type of login. You can see it in Reports -> Manager Actions and can search and filter the report as well as clearing it. [ed. note: BobRay last edited this post 13 years 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