Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

send_periodic_report_emails()

Parameters

ColumnTypeDefaultDescription
$echo_out true

Location

include/reporting_functions.php lines 248 to 372

Definition

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

    
# 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());
    "
;
    
$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 remote HTML table.
        
$output=do_report($report["report"], $from_y$from_m$from_d$to_y$to_m$to_d,false,true);

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

        
# 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 sprintf('<br />%s<br />%s/?dr=%s',
            
$lang['report_delete_periodic_email_link'],
            
$baseurl,
            
$report['ref']
        );

        
$unsubscribe="<br />" $lang["unsubscribereport"] . "<br />" $baseurl "/?ur=" $report["ref"];
        
$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);
            }

        
// 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']);
            }

        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'] . '";');
        }
    }

This article was last updated 11th July 2020 21:35 Europe/London time based on the source file dated 24th June 2020 14:38 Europe/London time.