SOLVED: Automated scheduled emails

dimoss

Well-Known Member
Hi

i am using the following php script for a scheduled task to send emails:

PHP:
use Joomla\CMS\Factory;

use Fabrik\Helpers\FabrikWorker;
$config = Factory::getConfig();

$db = \Joomla\CMS\Factory::getContainer()->get('DatabaseDriver');
$queryString = "SELECT player_id, name, assoc, email FROM vw_expired_passports WHERE assoc = 'XXX'";
$db->setQuery($queryString);
$rows = $db->loadAssocList();

$successCount = 0;
$failureCount = 0;

foreach ($rows as $row) {
    $pid = $row['player_id'];
    $playerName = $row['name'];
    $playerAssoc = $row['assoc'];
    $playerEmail = $row['email'];

    $subject = "Expired Passport for Player: " . $playerName;
    $msg = "Hello $playerAssoc,\nThis is a reminder to update the passport of the player: $playerName. Please do so through the link.";

    $res = FabrikWorker::sendMail(
        $config->get('mailfrom'),
        $config->get('fromname'),
        $playerEmail,
        $subject,
        $msg
    );

    if ($res) {
        $successCount++;
    } else {
        $failureCount++;
    }
}

// Send email notification
$notificationSubject = "Email Sending Summary";
$notificationMessage = "Emails sent successfully: $successCount\nEmails failed: $failureCount";

$notificationRes = FabrikWorker::sendMail(
    $config->get('mailfrom'),
    $config->get('fromname'),
    "xxx@xxx.com",
    $notificationSubject,
    $notificationMessage
);

if ($notificationRes) {
    echo "Notification email sent successfully.<br>";
} else {
    echo "Failed to send notification email.<br>";
}

Settings in the schedule tasks are below:
upload_2023-8-23_15-30-35.png


I have also set the cron job in the Server to run on specific date / time:

wget https://xxxxxxxxxxxx/index.php?fabrik_cron=xxxxx

However the emails are not sent and i dont receive any email notification.
Any help?
Thanks,
 
I assume you already tested with "Run" in the backend?
You'll only see echo (or debug or error messages) in the backend "Run" if you add an exit; (don't forget to remove it after backend testing).

If you want a notification and/or logs (independend of the code) set the params.
 
Using "Run" on backend works.
Works also if I remove the "Require querystring" to YES with the Secret Word but i dont want that as many emails have to be sent and dont want to overload the server.
Did i do something wrong in the server cron?
 
Well, i tried to run the "https://xxxxxxxxxxxx/index.php?fabrik_cron=xxxxx" on the browser and the email was sent successfuly.
Also I have set it in 1 minute.
Also the website time has been set in the same timezone as the server.
However it seems the command does not executed as a cron which i weird because in the same server i have same cron jobs in a F3 website and works fine.
 
Quotes, typo, selected run time...?
If it's running with the browser URL I think it's not Joomla or Fabrik.
 
I asked the host and I changed the cron to:

wget -q -O - https://xxxxxxxxxxxx/index.php?fabrik_cron=xxxxx

I did also some slight modification in the script and the final one is:

PHP:
use Joomla\CMS\Factory;
$db = \Joomla\CMS\Factory::getContainer()->get('DatabaseDriver');
$config = Factory::getConfig();

$queryString = "SELECT player_id, name, assoc, email FROM vw_expired_passports WHERE assoc = 'XXX'";
$db->setQuery($queryString);
$rows = $db->loadAssocList();

$successCount = 0;
$failureCount = 0;

foreach ($rows as $row) {
    $pid = $row['player_id'];
    $playerName = $row['name'];
    $playerAssoc = $row['assoc'];
    $playerEmail = $row['email'];

    $to = $playerEmail;
    $subject = "Expired Passport for Player: " . $playerName;
    $msg = "Hello $playerAssoc,\nThis is a reminder to update the passport of the player: $playerName. Please do so through the link.";

    $res = FabrikWorker::sendMail(
        $config->get('mailfrom'),
        $config->get('fromname'),
        $to,
        $subject,
        $msg
    );

    if ($res) {
        $successCount++;
    } else {
        $failureCount++;
    }
}

// Send email notification
$notificationSubject = "Email Sending Summary";
$notificationMessage = "Emails sent successfully: $successCount\nEmails failed: $failureCount";

$notificationRes = FabrikWorker::sendMail(
    $config->get('mailfrom'),
    $config->get('fromname'),
    "xxx@xxx.com",
    $notificationSubject,
    $notificationMessage
);

if ($notificationRes) {
    echo "Notification email sent successfully.<br>";
} else {
    echo "Failed to send notification email.<br>";
}

The frequency has been set to 1 minute.

Seems it works fine now :)
 
@troester If i have similar multiple scheduled tasks with different secret name and i have respective cron jobs in my server for each with the specific secret word set in different times does this mean the specific scheduled task will run at the give time?
Thanks.
 
To my understanding if you have different Fabrik scheduled task with different secrets and your server cron it doing something like

each day - 5:40 - wget ...&fabrik_cron=secret1
each 1st day of month - 14:30 - wget ...&fabrik_cron=secret2
your server will call the scheduled tasks at the given times
(and they will only run if the last-run-time + selected unit is over; so make sure to set a small unit)

You can add a notification in your server (at least I can do it) to see if/when the server call is going out
and you can enable the log/email in Fabrik to see if the scheduled task has really run.
 
To my understanding if you have different Fabrik scheduled task with different secrets and your server cron it doing something like

each day - 5:40 - wget ...&fabrik_cron=secret1
each 1st day of month - 14:30 - wget ...&fabrik_cron=secret2
your server will call the scheduled tasks at the given times
(and they will only run if the last-run-time + selected unit is over; so make sure to set a small unit)

You can add a notification in your server (at least I can do it) to see if/when the server call is going out
and you can enable the log/email in Fabrik to see if the scheduled task has really run.

Yes, I have to enable the log/email to check the correct execution of each scheduled task.
If each scheduled task runs on a different time as set in the server cron, then i am on the correct track.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top