Question

I have a php mailer script mailer.php under public_html directory. Which works fine when I directly access it through domain.com/mailer.php

PHP Mailer Script

<?php
$from = "mail@gmail.com"; // sender
    $subject = "Subject";
    $message = "Message of mail";
    $message = wordwrap($message, 70);
    mail("mail@gmail.com",$subject,$message,"From: $from\n");
?>

But I want to execute this script every minute using unix shell cron job

I am using Putty.exe Shell Terminal (For Windows)

I have saved following cron code in public_html/mailer.txt

* * * * * /home/user/public_html/mailer.php

Changed Shell Terminal Directory to /public_html and running following command

crontab mailer.txt

Now I am expecting to get mails in my above defined Email ID every minute but Its not working. Please Help me to find out where am I doing wrong.

Was it helpful?

Solution

Change:

* * * * * /home/user/public_html/mailer.php

to

* * * * * php /home/user/public_html/mailer.php

OTHER TIPS

Your file is simply not executable. You probably want to call php instead:

* * * * * php /home/user/public_html/mailer.php

... or to be on the safe side:

* * * * * /usr/bin/php /home/user/public_html/mailer.php

... where /usr/bin/php should be replaced with the actual path to your php binary.

Additionally, you can convert the file itself into a Linux script:

#!/usr/bin/php
<?php
$from = "mail@gmail.com"; // sender
    $subject = "Subject";
    $message = "Message of mail";
    $message = wordwrap($message, 70);
    mail("mail@gmail.com",$subject,$message,"From: $from\n");
?>

... and tell Linux that it is an executable file. E.g., if the file is saved as /home/user/bin/mailer (there's no need to save it into Apache document root):

chmod +x /home/user/bin/mailer

Now it's possible to schedule the file itself:

* * * * * /home/user/bin/mailer

You can find further details in the Executing PHP files section of the manual.

In your cron file, you need to be executing:

/usr/bin/wget localhost/mailer.php

This should execute the PHP code you have inside your file. However, I would recommend setting up a .htaccess file so that localhost is the only person that can execute that PHP file.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top