We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38357
    • 178 Posts
    Using Rev 2.2.4-pl and latest Login package.
    I am trying to get Register to send me an email when a new User registers on a site following the instructions at: http://rtfm.modx.com/display/ADDON/Login.Using+Pre+and+Post+Hooks

    I am unable to get the following to send an email:
    <?php
    $message = 'Hi, a new User signed up: ';
     
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$message);
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'ME');
    $modx->mail->set(modMail::MAIL_SENDER,'me');
    $modx->mail->set(modMail::MAIL_SUBJECT,'New User Signed Up');
    $modx->mail->address('to','[email protected]');
    $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();
    /* tell our snippet we're good and can continue */
    return true;

    ?>

    However this code will send an email successfully:

    <?php
    $to = "[email protected]";
     $subject = "Hi!";
     $body = "Hi,\n\nHow are you?";
     if (mail($to, $subject, $body)) {
       echo("<p>Message successfully sent!</p>");
      } else {
       echo("<p>Message delivery failed...</p>");
    ?>
      }


    So I know that the system is able to send emails successfully?

    What am I missing?

      • 16348
      • 64 Posts
      modPHPMailer is using the email settings in the System Settings but the PHP internal mail function isn't. Can that be your problem?
        • 38357
        • 178 Posts
        Thanks Kristoffer,
        OK - then for some reason modPHPMailer isn´t working. The script using it returns´1´ when it is run but the email never arrives at its destination. I have smpt activated in the system settings. There seem to have been a number of problems with this script on the forum.
          • 3749
          • 24,544 Posts
          Try the EmailResource Extra. It can often give you diagnostic info on why the mail is failing.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            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
            • 38357
            • 178 Posts
            Thanks Bob,
            I seem to have got the script working using modPHPMailer by turning off smtp.

            If I now execute a resource containing that script it sends the email correctly

            However, if I place the same script in a snippet called 'ABChookAdminEmail' and call it from a chunk using postHooks:
            [[!Register?
            
            &submitVar=`registerbtn`
            
            &activationResourceId=`64`
            
            &submittedResourceId=`63`
            
            &usergroups=`Club Members` 
            
            &activationEmailTpl=`ABClgnActivateEmailTpl` 
            
            &activationEmailSubject=`Please activate your account at [[++site_name]]`
            username:required:minLength=^5^,
            password:required:minLength=^6^,
            password_confirm:password_confirm=^password^,
            fullname:required,
            email:required:email`
            &placeholderPrefix=`reg.`
            &postHooks=`ABChookAdminEmail`
            ]] 


            It won't work. The activation email is sent correctly to the user but not the admin email. I must be missing a setting somewhere or have the above call incorrect?
              • 38357
              • 178 Posts
              Well - it seems that order is important!
              If the postHooks call is placed before theactivation Email call it works:
              [[!Register?
               
              &submitVar=`registerbtn`
               
              &activationResourceId=`64`
               
              &submittedResourceId=`63`
               
              &usergroups=`Club Members` 
              
              &postHooks=`ABChookAdminEmail`
               
              &activationEmailTpl=`ABClgnActivateEmailTpl` 
               
              &activationEmailSubject=`Please activate your account at [[++site_name]]`
              username:required:minLength=^5^,
              password:required:minLength=^6^,
              password_confirm:password_confirm=^password^,
              fullname:required,
              email:required:email`
              &placeholderPrefix=`reg.`
              ]]


              I haven't seen this documented anywhere - or maybe it is just in my case for some reason, although I have changed nothing else.
                • 16348
                • 64 Posts
                I don't think the order matters but you have a typo in your snippet call. You are missing the &validate parameter before your validation rules and I think the snippet will ignore all parameters that is passed after the typo.
                  • 38357
                  • 178 Posts
                  Thanks Kristoffer,
                  Everything is working now as it should.
                  Somehow I had deleted the &validate parameter at some time and didn't see it as missing - thanks for the pickup.
                  That wasn't the only problem. I had used the Register call from http://rtfm.modx.com/display/ADDON/Register.Example+Form+1 not realising that the register form that came with the package is different to the one used in the rtfm example. As I had changed the formatting in the original form I kept it and just changed the call - but the two are not compatible. If using the call from rtfm example you must use the form format that goes with it.

                  One more thing smiley. Is there any way to specify an exact username length? I can specify a minimum length or a maximum length, but how do I limit it to an exact length?
                    • 16348
                    • 64 Posts
                    Try to set the minLength and the maxLength validator to the same value. Or you could write your own custom validator.