Home >Backend Development >PHP Tutorial >How do I configure PHP cron jobs for email notifications in cPanel?

How do I configure PHP cron jobs for email notifications in cPanel?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-07 16:58:03667browse

How do I configure PHP cron jobs for email notifications in cPanel?

Executing PHP Files in Cron Jobs via CPanel

Cron jobs are automated tasks scheduled to run at specified intervals. For PHP scripts, the syntax to run them in a CPanel cron job is:

/usr/bin/php -q /path/to/script.php >/dev/null

Email Notifications for Cron Jobs

To receive email notifications when a cron job completes successfully, configure the PHP script as follows:

<code class="php">// Set the From and To email addresses
$from = "cronjob@example.com";
$to = "your_email@example.com";

// Specify the subject and body of the email
$subject = "Cron Job Execution Report";
$body = "The cron job has completed successfully.";

// Send the email using PHP's mail function
mail($to, $subject, $body, "From: $from");</code>

Example Cron Job Command with Email Notifications

/usr/bin/php -q /home/username/public_html/cron/cron.php | mail -s "Cron Job Execution Report" your_email@example.com

Alternative Syntax for GoDaddy

For GoDaddy servers, the following command can be used to activate a cron job for a PHP file:

/usr/bin/php -q /home/username/public_html/yourfilename.php

The above is the detailed content of How do I configure PHP cron jobs for email notifications in cPanel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn