i try it like it as described in the instructions. But the E-Mail did not send.
The Error-Log shows this:
[2022-10-14 09:02:03] (ERROR @ /www/xxx/core/components/login/controllers/web/Register.php : 448) PHP warning: count(): Parameter must be an array or an object that implements Countable
[2022-10-14 09:02:03] (ERROR @ /www/xxx/core/components/login/controllers/web/Register.php : 451) PHP warning: count(): Parameter must be an array or an object that implements Countable
[2022-10-14 09:02:03] (ERROR @ /www/xxx/core/components/login/controllers/web/Register.php : 451) PHP warning: count(): Parameter must be an array or an object that implements Countable
[2022-10-14 09:02:03] (ERROR @ /www/xxx/core/components/login/controllers/web/Register.php : 451) PHP warning: count(): Parameter must be an array or an object that implements Countable
For testing I used the snippet-code exactly from the instructions-page. I changed only the e-mail adress to a existing adress.
$message = 'Hi, a new User signed up: '.$hook->getValue('username')
. ' with email '.$hook->getValue('email').'.';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'admin@jerrys.org');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Jerrys Site');
$modx->mail->set(modMail::MAIL_SENDER,'Jerrys Site');
$modx->mail->set(modMail::MAIL_SUBJECT,'New User Signed Up');
$modx->mail->address('to','myemail@adress.com');
$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;
Thanks for the article. I changed the code and I get these Errors:
[2022-10-14 10:48:39] (ERROR @ /www/xxx/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 1340) Could not get table class for class: MODX\Revolution\modAccess
[2022-10-14 10:48:39] (ERROR @ /www/xxx/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 1309) Could not get table name for class: MODX\Revolution\modAccess
[2022-10-14 10:48:39] (ERROR @ /www/xxx/core/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 227) Error 42000 executing statement:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 18' at line 1
)
My Code:
use MODX\Revolution\Mail\modPHPMailer;
if (!$modx->services->has('mail')) {
$modx->services->add('mail', new modPHPMailer($modx));
}
$message = 'Hi, a new User signed up: '.$hook->getValue('username')
. ' with email '.$hook->getValue('email').'.';
$mail = $modx->services->get('mail');
$mail->set(modMail::MAIL_FROM,'mymail@adress.com');
$mail->set(modMail::MAIL_FROM_NAME,'mymail@adress.com');
$mail->set(modMail::MAIL_FROM_BODY, $message);
$mail->set(modMail::MAIL_SENDER,'mymail@adress.com');
$mail->set(modMail::MAIL_SUBJECT,'New User Signed Up');
$mail = $modx->address('to','mymail2@adress.com');
if (!$sent = $mail->send()) {
$err = 'Error: ' . $mail->mailer->ErrorInfo;
$this->modx->log(xPDO::LOG_LEVEL_ERROR, $err);
return 'Failure';
} else {
$mail->reset();
return true;
}
I don’t know about these errors. They seem unrelated to the code in your hook-snippet.
But there are some errors in your snippet code: modMail::MAIL_FROM_BODY doesn’t exist, $mail = $modx->address(...); is wrong, etc.
<?php
use MODX\Revolution\Mail\modPHPMailer;
if (!$modx->services->has('mail')) {
$modx->services->add('mail', new modPHPMailer($modx));
}
$message = 'Hi, a new User signed up: '.$hook->getValue('username')
. ' with email '.$hook->getValue('email').'.';
$mail = $modx->services->get('mail');
$mail->set(modMail::MAIL_FROM,'mymail@adress.com');
$mail->set(modMail::MAIL_FROM_NAME,'mymail@adress.com');
$mail->set(modMail::MAIL_BODY, $message);
$mail->set(modMail::MAIL_SENDER,'mymail@adress.com');
$mail->set(modMail::MAIL_SUBJECT,'New User Signed Up');
$mail->address('to','mymail2@adress.com');
if (!$mail->send()) {
$err = 'Error: ' . $mail->mailer->ErrorInfo;
$modx->log(modX::LOG_LEVEL_ERROR, $err);
}
$mail->reset();
/* tell our snippet we're good and can continue */
return true;
Also in the snippet call you posted above there is a ' instead of a backtick:
I use that Code, and yes the mail will send, but it “breaks” the RIGISTER Plugin.
Therefore there is no forwarding to the “Thanks for register” site and the confirmation mail will also not send.
And still get the Error in the log:
controllers/web/Register.php : 448) PHP warning: count(): Parameter must be an array or an object that implements Countable
There seems to be a general bug when submittedResourceId is used in combination with a postHooks. (Even when the code in the post-hook is just a return true; statement.)
Somehow (and I don’t know how) the values of $this->user and $this->profile are converted from an object into an array and that causes an error → “Uncaught Error: Call to a member function get() on array”.
The easiest fix I can see is to set the property &redirectUnsetDefaultParams to 1 (if you don’t need the get-parameters in the URL.)
This code is executed when &ensurePasswordStrength=`1` is set.
To get rid of the warnings you have to change the code on lines 448 and 451 to something like $restLength = is_string($rest) ? 1 : count($rest); and $currentLength = is_null($current) ? 0 : count($current); respectively.
that work very good. I set the &redirectUnsetDefaultParams to 1 and edited the Register.php on this two lines.
Now it do work (E-Mail where sendet), but I get this Error in the Logs:
[2022-10-14 20:07:13] (ERROR @ /www/xxx/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 1340) Could not get table class for class: MODX\Revolution\modAccess
[2022-10-14 20:07:13] (ERROR @ /www/xxx/core/vendor/xpdo/xpdo/src/xPDO/xPDO.php : 1309) Could not get table name for class: MODX\Revolution\modAccess
[2022-10-14 20:07:13] (ERROR @ /www/xxx/vendor/xpdo/xpdo/src/xPDO/Om/xPDOObject.php : 227) Error 42000 executing statement:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 25' at line 1
)
Are these errors related to the registration of a new user? Do they get added to the error log when a new user registers (or could it be errors from another extra)?
Unfortunately the error messages are quite vague, so it’s unclear where they occur.
As the errors mention modAccess, I thought they are maybe related to &usergroups=`testgroup`, but I couldn’t reproduce the issue. Does anything change if you remove the property &usergroups=`testgroup` from the snippet call?
as I write in my last post everything works, except that the error is displayed.
Today I wanted to do a new test to see where the error message comes from. And I get an Error 500 as soon as I click on the submit button. Even if I remove the PostHook it stays.
This is the first time I get an Error 500 on this function. I currently have no idea why the 500 appears.
Ok, I found out that the 500 appears only if I let the first password-field blank.
I could be that this was from the beginning. I tested every time with a value in the password-field.
Ok, now I found out that this Errors in the LOG only appears when I delete a user.
While the testing from the registration I deletet all the testusers each time the registration was successfull.
Well, the code works but produces unnecessary error log messages.
Fixing it probably involves changing the MODX schema. That’s a high-risk low-reward situation and it seems nobody is willing to touch it.