0

I am using Joomla 1.5 and I would like to know if my mail will be sent if I run the function after redirect();

My code is as below:

$callback   = new stdClass();
$callback->LeaveID    = $leaveid;
$callback->callback   = 1;
$callback->balance_cf = $balance_cf;
$callback->remarks    = $remarks;

if($db->updateObject('#__leaves',$callback,'LeaveID')) {
  $app->redirect($url,' Callback Perfomed Successfully');
} 
else {
  echo 'Not saved - Leave App';
}

mail($user->email,$somesubject,$message,implode("\r\n",$headers));`

Will the mail function be executed if the $db->updateObject() returns true?

  • Why do you want to execute the mail() function after the redirect? Can you not put it inside the if statement and redirect if the mail if successful? – Lodder Dec 11 '15 at 9:14
  • i want the mail to be sent no matter the outcome of the if(){} block – Patrick Mutwiri Dec 11 '15 at 9:16
  • In which case, you can send the mail before the if statement – Lodder Dec 11 '15 at 9:52
  • i fear mail() using up all the resources before trying the update..that's why am running it after – Patrick Mutwiri Dec 11 '15 at 10:09
  • I'll compile an answer for you in a minute – Lodder Dec 11 '15 at 10:12
1

Rather than using the PHP mail function, I'll show you how to use Joomla's JMail class. We'll also make sure that the email is always sent and the database is updated after:

$callback             = new stdClass();
$callback->LeaveID    = $leaveid;
$callback->callback   = 1;
$callback->balance_cf = $balance_cf;
$callback->remarks    = $remarks;

// Let's firstly send the email
$mail = &JFactory::getMailer();
$mail->setSubject($somesubject);
$mail->setSender('me@example.com');
$mailer->addRecipient('to@example.com');
$mail->isHTML(true);
$mail->setBody($message);

$send = $mail->Send();

if ($send == true)
{
    // The email was sent, so we can now do the database updates
    if ($db->updateObject('#__leaves', $callback, 'LeaveID'))
    {
        $app->redirect($url, 'Callback Perfomed Successfully');
    } 
    else
    {
        echo 'Not saved - Leave App';
    }
} 
else
{
    // Error sending the email
    echo 'Error sending email: ' . $send->__toString();
}

Hope this helps.

Note: I may have accidentally made a small mistake as it's been a long time since I've written any code for the old Joomla 1.5, but hopefully it will push you in the right direction

  • does this work in j1.5? I use this in v3.* – Patrick Mutwiri Dec 11 '15 at 10:43
  • @PatrickMutwiri - I'm not sure about your database update query (updateObject), but the mail should work. Like I said, it's been a long time since I wrote any code for Joomla 1.5......I only use 3.x these days which is quite different – Lodder Dec 11 '15 at 11:26
0

Your Question:

Will the mail function be executed if the $db->updateObject() returns true?

Answer: No.

If you want to execute functions before returns or redirects put them just before such statements.

i fear mail() using up all the resources before trying the update..that's why am running it after –

This shouldn't happen unless your host server is from the 80s or you have just < 16 MB RAM.

Recommendations

Please switch to the latest Joomla, don't use deprecated software. Your site is vulnerable to hacks, and puts your visitors to unnecessary risk.

Use the native Joomla mailer lib.

If you are using dynamically generated recipient lists (like sending a mail from a contact form), limit the max recipients.

Use $db->quote() for inserting data or at least htmlspecialchars.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.