I want to send a mail automatically using cron job. Till i searched about that, there is only how to set the time and path of the file in cron folder. But i want to know about how it will run?

5 Years
Discussion Span
Last Post by cereal

The execution is automatic, you need to write also the command path. for example:

30 * * * * /usr/bin/php /srv/cron/mail.php

Otherwise add #!/usr/bin/php at the top of your script and make the file executable:

    // . . .
    echo 'example';

In command line use chmod: chmod +x mail.php
And in your crontab you can write:

30 * * * * /srv/cron/mail.php

At specific time the server will just run the script. Then you can go to /var/log to check crontab executions and you can also add error_log() to write a specific log about the excution of the functions in your script: http://php.net/manual/en/function.error-log.php

It this what you were asking for? Bye!

Edited by cereal


I have written the following code and save it in etc/cron.d/crontab.txt

2 * * * * /srv/cron/cr.php

Then i store the following php mail function in srv/cron/cr.php. Also included that smtp_confi.php and smtp_class.php in that srv/cron/cr.php

        $to = "xxx@gmail.com";
        $from = "yyy@gmail.com";
        $subject = 'Mail send';
        $body = "Automatically mail send is done";
        $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
        $SMTPChat = $SMTPMail->SendMail();

Is this right way or not?


Add the command path to the crontab file or to the script, as I showed in my previous post and it will be executed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.