Register Mail to CC

Hi,

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;

What can it be?

The problem is usually this line. MAIL_SENDER has to be an email-address. Use the same value as for
MAIL_FROM (or delete this line altogether).


These warnings are from the Login extra and not from your hook code.

Deleting the line …MAIL_SENDER … or change “Jerrys Site” to a valid e-mail-adress changed nothing.

  • EMail where not be sendet.

My REGISTER call is like so:

[[!Register?
			  &ensurePasswordStrength=`1`
			  &activationEmailTpl=`mailcontentAutoregister`
			  &placeholderPrefix=`reg.`
			  &validationErrorMessage=`Das Formular wurde nicht gesendet da Fehler aufgetreten sind!`
			  &submittedResourceId=`12`
			  &activationResourceId=`172`
			  &activationEmailSubject=`Bitte aktivieren Sie Ihren Account`
			  &usergroups=`testgroup`
			  &postHooks=`hookSendInfo'
			  &submitVar=`registerbtn`
			  &validate=`nospam:blank,
			  username:required:minLength=^6^,
			  password:required:minLength=^6^,
			  password_confirm:password_confirm=^password^,
			  fullname:required,
			  email:required:email,
			  company:required,
			  agb:required,
			  daten:required,
			  zusatzinfo:required
			  `
			]]

I see that you tagged MODX 3.
If you are using MODX 3, then you have to change the code slightly.

Take a look at the section Mail in this article:

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:

&postHooks=`hookSendInfo'

I found a topic from bobray about the Mailsend-Thing:
https://community.modx.com/t/sending-mail-without-using-getservices-in-modx-3/5342

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.)

[[!Register?
  ...
  &submittedResourceId=`12`
  &postHooks=`hookSendInfo`
]]

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.)

It’s not an error, just a warning.

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.

Thank you so much!

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?

Hi, this MODx-monday startet strange to me :slight_smile:

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.

So the “modAccess” error is an old bug in MODX (even in MODX 2.x)

Here is a Github issue from 2014(!) with the quote “It is harmless, but annoying.”

This is another bug when the property &ensurePasswordStrength=`1` is used.

You can fix it by adding this code

if (strlen($password) == 0) {
	return $ensured;
}

after this line

Thank you so much for that great support!

It is interesting that the bug that is reported in 2014, is still present in Version 3. I didn’t expect this.

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.

I understand :grinning: