简体   繁体   中英

PHP cron send mail many times

I have a script that generate reports data of my big database.
I have create a cron that every week on Sundat at 23:00 generate this reports and send it to many emails.
If I run the script directly (copy the link of the php script inside a browser and launch it) the script works fine foreach client send me an email what I want.

If the cron start the script I receive only the email of the first client but not of the other, I receive the number of email correct but not for client. Example:
I have to 5 clients name: a, b, c, d, e.
If I launch script directly in browser I receive:
one email for a
one email for b
one email for c
one email for d
one email for e

If the cron launch the script I receive:
5 email for a

How is possible that? The script is the same! the cron script is inside crontab:

0 23    * * 0   root    wget http://127.0.0.1/reports/generate_reports_settimanale.php

Another consideration is that this script takes about 30 minutes or more because I have large data and many clients.

The script in PHP is very large I cut some part of data calculator.

<?php
$db = DataManager::_getConnection();
ini_set("include_path", "../inc/phpmailer/");
require("class.phpmailer.php");
ini_set("memory_limit","64M");

//-----------------------------------REPORT SETTIMANALE------------------------------------------

$qry_inv = $db->query("SELECT * FROM customer WHERE ntype > 0 AND report_settimanale='1' ORDER BY name ASC");
while( $inv = $qry_inv->fetch_array() ) {
    //check if table exists
    $qry_tb = $db->query("SHOW TABLES LIKE 'chart_voltage_".$inv['id']."_".date('Y')."'");
    if($qry_tb->num_rows > 0){
        //saving the excel sheet
        //...
        //sending the emails
        $yourName = 'CLIENT';

        $mail = new PHPMailer();

        $mail->From     = 'email@email.com';
        $mail->FromName = 'Saeg';
        $mail->AddAddress('my_email@my_em.it'); 

        $mail->AddAttachment('/var/www/reports/my_reports.xls');

        $mail->WordWrap = 50;                              // set word wrap
        $mail->IsHTML(true);                               // send as HTML

        $mail->Subject  =  'Riepilogo settimanale dal '.$week_start_title.' al '.$week_end_title;

        $mail->Body     =  "Riepilogo settimanale dell'impianto: ".utf8_decode($inv['name']);

        $mail->AltBody  =  "Riepilogo settimanale dell'impianto: ".utf8_decode($inv['name']);

        if ( $mail->Send()){
            //ok
        }
        else{
        ?>
            <script type="text/javascript">
                alert('<?php echo ("ERROR");?>');
                window.close();
            </script>
        <?php
        }
    }
}

exit;

?>

I don't know if the problem is the duration of the script... because is imposible taht If I launch it in the browser works fine and with cron not... is not possible that. Why?

There is no obvious reason why the code would behave differently with wget rather than a browser.

You've got no instrumentation in your code to log what it's doing - which might give you a clue as to why it is failing. As a minimum you should ensure that errors are being logged by you php config, and check the relevant log for errors.

However expecting an HTTP request lasting 30 minutes to succeed is rather optimistic. If you can set up cron jobs on the machine then it makes a lot more sense to invoke the script directly rather than via the webserver and a an http client:

0 23 * * 0   root /usr/bin/php -q /var/www/html/reports/generate_reports_settimanale.php

(do replace /var/www/html with whatever your document root is).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM