0

I have a function that sends an email when a form is submitted. This function contains the following if-statement:

if ($guestDetails) 
{
  $mailer->addRecipient($guestDetails->email);
  $mailer->addCC( $config->get('emails_admin_email'));

  $mailer->setSubject( $subject );
  $mailer->setBody($template_layout);
  $mailer->IsHTML($mode);          
  $mailer->setSender(array( $mailfrom, $fromname ));
  $sent = $mailer->send();
}

Recently, this function has stopped working (i.e. the emails are no longer being sent). To investigate what is going on I have modified the code to log some of the variables as there was nothing being recorded in the PHP Error log. Here is how the if-statement looks now:

$testArr = array();
ClassName::_log_r("guestdetails", $guestDetails);
if ($guestDetails) 
{
  ClassName::_log_r("got into if", $testArr);
  $mailer->addRecipient($guestDetails->email);
  $mailer->addCC( $config->get('emails_admin_email'));

  $mailer->setSubject( $subject );
  $mailer->setBody($template_layout);
  $mailer->IsHTML($mode);          
  $mailer->setSender(array( $mailfrom, $fromname ));
  $sent = $mailer->send();
  ClassName::_log_r("mailer", $mailer);
  ClassName::_log_r("sent", $sent);
} else
  ClassName::_log_r("got into else", $testArr);

The _log_r is just a function that writes to a text file the contents of the given variable. Once I added this "debug" code, the emails started being sent once again and the log correctly records "got into if". Removing the afore mentioned debug code stops the emails from being sent once again.

I am puzzled at what could be possibly going on here. Has anyone ever came across anything like this? Why are emails not being sent when the debug code is removed? Please let me know if more information is needed. Thanks!

PS the code is written in PHP and the server is running PHP 5.3.10

Anton Soradoi
  • 1,841
  • 1
  • 22
  • 36
  • When you add the code and emails do get sent, do they get sent to where you intend them to? Meaning, if you do a `ClassName::_log_r("details", print_r($guestDetails, true));` what do you get? – Julian Aug 22 '12 at 16:16
  • Does it generate any kind of error message, or does the code just try to send the email and it's not received? Is there anything in your SMTP server's error logs? – andrewsi Aug 22 '12 at 16:16
  • Julian - yes, the emails are sent to the correct addressees. – Anton Soradoi Aug 22 '12 at 17:13
  • andrewsi - I do not have an access to the SMTP error log, but any errors in SMTP would be caught by the mailer class. However, PHP doesn't log any errors or warnings, so I assume there are no errors – Anton Soradoi Aug 22 '12 at 17:16

2 Answers2

1

The only likely answer that comes to mind is that, for whatever reason, $guestDetails is not defined anymore (e.g., it got renamed into $guestDetail somewhere else).

This also would need the _log_r function to redefine the argument if it does not exist.

If you check the mails being sent to the CC: only, they will arrive - an invalid $guestDetails will probably not stop the process from completing. Actually ONLY the emails to the admin will get generated.

Try something like

if (!defined($guestDetails))
    ClassName::_log_r("mailer", "Not defined");
if (empty($guestDetails))
    ClassName::_log_r("mailer", "empty");

and see whether some more detail can be worried out.

Lodder
  • 19,758
  • 10
  • 59
  • 100
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • Thank for your suggestion. I've changed the "defined" with "isset" as "$guestDetails" is an array. Then I tried adding these four lines before my if-statement and after it. When pasted after my if-statement, nothing happens - nothing is logged and the email is not sent. When added before my if-statement, nothing is logged and the emails *are sent*. – Anton Soradoi Aug 22 '12 at 18:10
  • 1
    This is crazy. It means that simply _referring to a variable_ somehow changes things? Is this running on a quantum computer by any chance? Seriously, I am not aware of Joomla! implementing this sort of "magic variables"... I fear this points to some larger bug lurking somewhere :-( – LSerni Aug 22 '12 at 21:35
  • Hahaha... I am going crazy trying to debug this. EDIT: The scary part is that, I consistently get the same results. It feels like some of variable's scope is leaked into the send() function... but that's not something that PHP does AFAIK. – Anton Soradoi Aug 23 '12 at 00:30
  • I was unable to resolve this issue, decided to circumvent the JMail and just use PHPMailer directly. (which works without the need to log anything lol) – Anton Soradoi Aug 27 '12 at 20:53
1

Instead of logging with _log_r, try to catch mail exceptions and check if you get better info on whats going on:

if ($guestDetails) 
{
    try
    {
        $mailer->addRecipient($guestDetails->email);
        $mailer->addCC( $config->get('emails_admin_email'));

        $mailer->setSubject( $subject );
        $mailer->setBody($template_layout);
        $mailer->IsHTML($mode);          
        $mailer->setSender(array( $mailfrom, $fromname ));
        $sent = $mailer->send();
    } 
    catch (phpmailerException $e) 
    {
        echo $e->errorMessage(); //you could do a _log_r here..
    } 
    catch (Exception $e) 
    {
        echo $e->getMessage(); //or here..
    } 
}
else
{
    echo "guestDetails empty!"; //or here..
}
raidenace
  • 12,789
  • 1
  • 32
  • 35
  • Tried it, same thing happened. When try-catch is included, nothing goes wrong(no exceptions are thrown) and the email is sent. Removed the try-catch and emails are no longer sent. – Anton Soradoi Aug 22 '12 at 17:25