We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14877
    • 110 Posts
    MODx Revolution 2.0.6 (traditional); Apache ; PHP 5.2.13; PHPMailer 2.0.4;

    I have the Login extra working fine. I am now attempting to extend it to send an e-mail notification when a user registers (via the posthooks parameter). My entire snippet code is at the end of this post (so you can read the preamble and skip this if not interested).

    I have followed Shaun’s example posthook snippet very closely (ASIDE: I am continually amazed and grateful at the effort Shaun has put into MODx Revolution).

    I have validated the code locally using "php -l testxxx.php", but am running PHP 5.3.3 on my Mac. However, the validation was purely to look for silly syntax errors (I removed the <?php and ?> to validate).

    Shaun’s sample snippet runs fine, but obviously doesn’t send an e-mail message as the e-mail addresses are meaningless (I hope!). My snipped produces the following error message (copy-and-pasted):

    Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /data/13/0/91/18/91833/user/93230/cgi-bin/modx/core/cache/wccr/elements/modsnippet/33.include.cache.php on line 27

    The problem is something to do with the code starting with the line (which BBEdit says is line 25):
    $modx->getService(’mail’, ’mail.modPHPMailer’);

    Ideas (not guesses)? Here is the snippet ("wccr_login_notify" is a system setting I added that is the address to which administrative notifications from the login system are to be sent):

    <?php
    /* Post-hook for Login:Registration. Sends an e-mail message regarding the registration to the membership committee.
    
    Notes:
      1. Refer to the documentation for the "login" extra for basic documentation.
    
    Extensions to the sample in the basic documentation:
    
      1. The body of the e-mail message is provided in a template (TPL). The field values are merged using MODx methods.
      2. Messages are sent from the e-mail address set in the system settings: "emailsender" (not hard-coded here)
      3. Messages are sent to the e-mail address set in the system settings: "wccr_login_notify"
    */
    
    $formPlaceHolderPairs = array (
      'username' => $hook->getValue('username'),
      'fullname' => $hook->getValue('fullname'),
      'email' => $hook->getValue('email'),
      'date' => date('Y-m-d H:i')
    );
    $formTpl = $modx->getObject('modChunk', array( 'name' => 'wccrRegNotifyTpl'));
    $message = $formTpl->process($formPlaceHolderPairs);
    $emailSender = modx->getOption('emailsender');
    $wccrLoginNotify = modx->getOption('wccr_login_notify');
    
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY, $message);
    $modx->mail->set(modMail::MAIL_FROM, $emailSender);
    $modx->mail->set(modMail::MAIL_FROM_NAME, 'WCCR Web Site User Admin');
    $modx->mail->set(modMail::MAIL_SENDER, 'WCCR Web Site User Admin');
    $modx->mail->set(modMail::MAIL_SUBJECT, 'WCCR: New User Registered');
    $modx->mail->address('to', $wccrLoginNotify);
    $modx->mail->setHTML(true);
    if (!$modx->mail->send()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$err);
    }
    $modx->mail->reset();
    
    return true;
    ?>

      __________________
      JRG
      • 28215
      • 4,149 Posts
      $emailSender = modx->getOption('emailsender');
      $wccrLoginNotify = modx->getOption('wccr_login_notify');
      


      should be:

      $emailSender = $modx->getOption('emailsender');
      $wccrLoginNotify = $modx->getOption('wccr_login_notify');
      

      Note the missing $ before modx in the first.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 14877
        • 110 Posts
        Thanks for spotting the transcription error. Now I know for sure that I must read the section of the PHP book on OOP.

        Now I feel like an idiot! Well, not the first time and I imagine it won’t be the last.

        So, how do you scan syntax better than the PHP interpreter?
          __________________
          JRG
          • 28215
          • 4,149 Posts
          Years of typos myself. tongue
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 3749
            • 24,544 Posts
            That one used to bite me hard until I got an editor that warns me about it.

            One that still bites me (it cost me more hours than I care to admit the other day) is using a $ instead of a & for a snippet parameter. Even a debugger won’t help you with that one. wink
              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