?
avatar Garry309
Garry309
5 Dec 2016

First of all - im newby in github. I just found an error and fixed it on my site, but i hope that i can help to community)))

Steps to reproduce the issue

Joomla! 3.6.4
JFactory::getMailer() generate an error while registering new user in administrator interface or when user tries to register itself

Expected result

User registred succesfull

Actual result

Get "Fatal error: Call to a member function addRecipient() on a non-object in /home/host1526565/gagarinskaya-usadba.ru/htdocs/www/plugins/user/joomla/joomla.php on line 141"
from administrator interface.

Error message while self registering.

Additional comments

This solution works for me.
First of all the problem in this section:

In "plugins/user/joomla/joomla.php" line 134:

$mail = JFactory::getMailer()
						->setSender(
							array(
								$this->app->get('mailfrom'),
								$this->app->get('fromname')
							)
						)
						->addRecipient($user['email'])
						->setSubject($emailSubject)
						->setBody($emailBody);

This code generate an error " Call to a member function addRecipient() on a non-object "
I simply rewrite code:

$mail = JFactory::getMailer();
$mail->setSender(
array(
	$this->app->get('mailfrom'),
	$this->app->get('fromname')
	)
	);
					
$mail->addRecipient($user['email']);
$mail->setSubject($emailSubject);
$mail->setBody($emailBody);

That works fine.

Next, same error, but whitout "fatal error screen":

In "components\com_users\models\registration.php" lines 159, 197, 558, 598:

$return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $row->email, $emailSubject, $emailBody);

Code returning false and don`t send emails.
But in my case works this:

$mailer = JFactory::getMailer();
					$mailer->setSender(
							array(
								$data['mailfrom'],
								$data['fromname']
							)
						);
					
$mailer->addRecipient($row->email);
$mailer->setSubject($emailSubject);
$mailer->setBody($emailBody);
$return = $mailer->Send();

System information (as much as possible)

Dump of joomla sys-info in JSON
systeminfo-json.zip

avatar Garry309 Garry309 - open - 5 Dec 2016
avatar joomla-cms-bot joomla-cms-bot - change - 5 Dec 2016
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 5 Dec 2016
avatar mbabker
mbabker - comment - 5 Dec 2016

The code fix is technically correct, any of the methods adding email addresses can return a boolean false value. However, this also indicates a configuration issue on your site. The setSender() method is rejecting the email address configured as the from address, you'll want to check your site's configuration to make sure you've got a valid address there.

avatar Garry309
Garry309 - comment - 5 Dec 2016

I checked my settings, and you right - it fix the problem.
Thanks)
But, maybe it still important thing?
I think, its not good to trow fatal error, when I just forget to set site-email, doesn`t it?

avatar mbabker
mbabker - comment - 5 Dec 2016

That's why I said the code fix is correct ?

Even better would be to check for an error like at https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/mail/mail.php#L738-L741 and do something knowing that setting up the email to be sent has failed (the message might still send but depending on which method fails it would be missing data).

avatar Garry309
Garry309 - comment - 5 Dec 2016

Yes, you right, its better)))
In fact, I thought, that my version of php doesn``t support this way of creating objects, thats why i wrote an issue. )))) I```m not a php-developer)))
I am guessing, will be useful to add warning message, that say something like "email cannot be send because you don`t set site email", or something like this.

avatar sovainfo
sovainfo - comment - 5 Dec 2016

Which is why the fix is NOT correct. It doesn't handle error situations correctly!
The construction of the mail should be enclosed in a try / catch. Failing to construct should result in same message as failing to send. You may choose for a separate message but the handling is expected to be the same. Again, IGNORING it is not the right thing to do!

avatar Garry309
Garry309 - comment - 6 Dec 2016

Hi. @sovainfo - I agree with you. In my situation, I just doesn``t knew the reason of error. And I thought, that problem in generating an mailer object.
I realized, that my solution is wrong.
Nevertheless, I don`t delete this issue, because this problem should be handled.

avatar brianteeman brianteeman - change - 6 Dec 2016
Category Libraries
avatar franz-wohlkoenig franz-wohlkoenig - change - 5 Apr 2017
Status New Confirmed
avatar franz-wohlkoenig franz-wohlkoenig - change - 5 Apr 2017
Build master 3.6.4
avatar Quy
Quy - comment - 29 May 2017

Fixed in #16107

avatar brianteeman brianteeman - change - 29 May 2017
Status Confirmed Closed
Closed_Date 0000-00-00 00:00:00 2017-05-29 22:02:39
Closed_By brianteeman
avatar brianteeman brianteeman - close - 29 May 2017

Add a Comment

Login with GitHub to post a comment