Forums

V3.2 debug code in emails.php incorrect

polderguy 14 Feb, 2012
Hi,

I think there is something wrong in the emails.php file of ChronoForms V3.2
Below the incorrect piece of code in emails.php for sending an email when submitting a form:


/**
 * Send the email(s)
 */
$email_sent = JUtility::sendMail($from, $fromname, $recipients, $subject, $email_body, $mode, $ccemails, $bccemails, $this_attachments, $replyto_email, $replyto_name );
				
if ($email_sent)$MyForm->addDebugMsg('An email has been SENT successfully from ('.$fromname.')'.$from.' to '.implode(',', $recipients));
if (!$email_sent)$MyForm->addDebugMsg('An email has failed to be sent from ('.$fromname.')'.$from.' to '.implode(',', $recipients));


According to this article:

http://www.packtpub.com/article/session-and-user-with-joomla-1.5-part2

Notice that we check THE MIXED RETURN value of JUtility::sendMail() to check if the email was sent
successfully. The return value will always be TRUE on success and a JException OBJECT on failure.



So I think the 2 "if" statements are not working properly. These 2 lines assume that JUtility::sendMail() return values are either true of false. Which obviously is not the case.

I started to notice this when I had debug on and tested a form with email on a local pc. There is no way an email can be sent from this local machine but after submitting the form the debug message kept telling me:

An email has been SENT successfully from (me)polderguy@xxxx.com to [email]anonymous@yyyy.com[/email]



As soon as I replaced the 2 "if" lines in emails.php with this:


if ($email_sent !== true) {
	$MyForm->addDebugMsg('An email has failed to be sent from ('.$fromname.')'.$from.' to '.implode(',', $recipients));
}
else {
	$MyForm->addDebugMsg('An email has been SENT successfully from ('.$fromname.')'.$from.' to '.implode(',', $recipients));
}


I started receiving the correct debug message:

An email has failed to be sent from (me)polderguy@xxxx.com to [email]anonymous@yyyy.com[/email]



The ChronoForms debug option should make our lives easier. However this way it creates confusion.
Please note this issue doesn't affect the actual sending of an email.

I've searched throught the forums but I am unable to find anything on this topic.
I simply cannot believe I'm the first person who stumbled upon this ...

Something else. Can I do the same email result check in the "On Submit code - after sending email:" box ?

eg:


<?php
if ($email_sent !== true) {
	echo 'An email has failed to be sent';
}
else {
	echo 'An email has been SENT successfully';
}
?>


PolderGuy
GreyHead 14 Feb, 2012
Hi polderguy,

Well found. I knew that the message was unreliable but hadn't dug far enough to find this. It's quite likely that the same is true in CFv4 (I just looked and it is).

I'll make this post sticky for future reference.

I'm not sure if you can access $email_sent from the On Submit box - they are executed using eval and I don't think that the scope extends enough. You'd need to declare it as global first. Otherwise the $MyForm->addDebugMsg are probably available - not an ideal fix though.

Bob
GreyHead 14 Feb, 2012
Hi,

I updated the ChronoForms v4 Email [GH] action to check the sent state correctly and to show the error message from the Joomla! Exception object on failure.

Bob
polderguy 19 Feb, 2012
Bob,

Thank you for the confirmation.
I think you're right about the scope. I'll figure out a way to
check the email sent state.

PolderGuy
This topic is locked and no more replies can be posted.