Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

send_periodic_report_emails()

Parameters

ColumnTypeDefaultDescription
$echo_out true
$toemail true

Location

include/reporting_functions.php lines 296 to 460

Definition

 
function send_periodic_report_emails($echo_out true$toemail=true)
    {
    
# For all configured periodic reports, send a mail if necessary.
    
global $lang,$baseurl$report_rows_zip_limit;

    
# Query to return all 'pending' report e-mails, i.e. where we haven't sent one before OR one is now overdue.
    
$query "
        SELECT pe.*,
               u.email,
               r.name
          FROM report_periodic_emails pe
          JOIN user u ON pe.user = u.ref
          JOIN report r ON pe.report = r.ref
         WHERE pe.last_sent IS NULL
            OR date_add(date(pe.last_sent), INTERVAL pe.email_days DAY) <= date(now());
    "
;

    
// Keep record of temporary CSV/ZIP files to delete after emails have been sent
    
$deletefiles = array();

    
$reports=sql_query($query);
    foreach (
$reports as $report)
        {
        
$start=time()-(60*60*24*$report["period"]);

        
$from_y date("Y",$start);
        
$from_m date("m",$start);
        
$from_d date("d",$start);

        
$to_y date("Y");
        
$to_m date("m");
        
$to_d date("d");

        
// Translates the report name.
        
$report["name"] = lang_or_i18n_get_translated($report["name"], "report-");

        
# Generate report (table or CSV)
        
$output=do_report($report["report"], $from_y$from_m$from_d$to_y$to_m$to_d,false,true$toemail);

        
// Formulate a title
        
$title $report["name"] . ": " str_replace("?",$report["period"],$lang["lastndays"]);

        
// If report is large, make it an attachment (requires $use_phpmailer=true)
        
$reportfiles = array();
        if(
is_array($output) && isset($output["file"]))
            {
            
$deletefiles[] = $output["file"];
            
//Include the file as an attachment
            
if($output["rows"] > $report_rows_zip_limit)
                {
                
// Convert to  zip file
                
$unique_id=uniqid();
                
$zipfile get_temp_dir(false"Reports") . "/Report_" $unique_id ".zip";
                
$zip = new ZipArchive();
                
$zip->open($zipfileZIPARCHIVE::CREATE);
                
$zip->addFile($output["file"], $output["filename"]);
            
                
$zip->close();
                
$deletefiles[] = $zipfile;
                
$zipname str_replace(".csv",".zip"$output["filename"]);
                
$reportfiles[$zipname] = $zipfile;
                }
            else
                {
                
$reportfiles[$output["filename"]] = $output["file"];
                }
            
$output str_replace("%%REPORTTITLE%%"$title$lang["report_periodic_email_report_attached"]);
            }

        
// Send mail to original user - this contains the unsubscribe link
        // Note: this is basically the only way at the moment to delete a periodic report
        
$delete_link "<br /><br />" $lang["report_delete_periodic_email_link"] . "<br /><a href=\"" $baseurl "/?dr=" $report["ref"] . "\" target=\"_blank\">" $baseurl "/?dr=" $report["ref"] . "</a>";
        
        
$unsubscribe="<br /><br />" $lang["unsubscribereport"] . "<br /><a href=\"" $baseurl "/?ur=" $report["ref"] . "\" target=\"_blank\">" $baseurl "/?ur=" $report["ref"] . "</a>";
        
$email=$report["email"];

        
// Check user unsubscribed from this report
        
$query sprintf('
                SELECT true as `value`
                  FROM report_periodic_emails_unsubscribe
                 WHERE user_id = "%s"
                   AND periodic_email_id = "%s";
            '
,
            
$report['user'],
            
$report['ref']
        );
        
$unsubscribed_user sql_value($queryfalse);
        if(!
$unsubscribed_user)
            {
            if (
$echo_out) {echo $lang["sendingreportto"] . " " $email "<br />" $output $delete_link $unsubscribe "<br />";}
            
send_mail($email,$title,$output $delete_link  $unsubscribe,"","","","","","","",$reportfiles);
            }

        
// Jump to next report if this should only be sent to one user
        
if(!$report['send_all_users'] && empty($report['user_groups']))
            {
            
# Mark as done.
            
sql_query('UPDATE report_periodic_emails set last_sent = now() where ref = "' $report['ref'] . '";');
            
            continue;
            }

        
# Send to all other active users, if configured.
        # Send the report to all active users.
        
$users get_users(0,"","u.username",false,-1,1);

        
// Send e-mail reports to users belonging to the specific user groups
        
if(!empty($report['user_groups']))
            {
            
$users get_users($report['user_groups'],"","u.username",false,-1,1);
            }

        foreach(
$users as $user)
            {
            
$email $user['email'];

            
# Do not send to original report user, as they receive the mail with the unsubscribe link above.
            
if(($email == $report['email']) || !filter_var($emailFILTER_VALIDATE_EMAIL))
                {
                continue;
                }

            
// Check user unsubscribed from this report
            
$query sprintf('
                    SELECT true as `value`
                      FROM report_periodic_emails_unsubscribe
                     WHERE user_id = "%s"
                       AND periodic_email_id = "%s";
                '
,
                
$user['ref'],
                
$report['ref']
            );
            
$unsubscribed_user sql_value($queryfalse);

            if(!
$unsubscribed_user)
                {
                
$unsubscribe_link sprintf('<br />%s<br />%s/?ur=%s',
                    
$lang['unsubscribereport'],
                    
$baseurl,
                    
$report['ref']
                );

                if (
$echo_out) {echo $lang["sendingreportto"] . " " $email "<br />" $output $unsubscribe_link "<br />";}
                
send_mail($email$title$output $unsubscribe_link);
                }
            }

        
# Mark as done.
        
sql_query('UPDATE report_periodic_emails set last_sent = now() where ref = "' $report['ref'] . '";');
        }

    
$GLOBALS["use_error_exception"] = true;
    foreach(
$deletefiles as $deletefile)
        {
        try
            {
            
unlink($deletefile);
            }
        catch(
Exception $e)
            {
            
debug("BANG Unable to delete - file not found: " $deletefile);
            }
        }
    unset(
$GLOBALS["use_error_exception"]);
    }

This article was last updated 23rd November 2020 16:05 Europe/London time based on the source file dated 18th November 2020 08:15 Europe/London time.