Skip to main content

Send Email attachment to recepients

<?php

    global $base_url;
   

    $log_type='Partners';
    $report_type='Download';
   
    $thedate = strtotime("-1 day");
    $datetime = date('Y-m-d',$thedate).'_02_00';   
   
    $name = "$log_type-clicks-$datetime.xls";   
   
    $date_from_year = date('Y',$thedate);
    $date_from_month = date('m',$thedate);
    $date_from_day = date('d',$thedate);

    $date_from = strtotime($date_from_year.'-'.$date_from_month.'-'.$date_from_day.' 00:00:00');
    $date_to = strtotime($date_from_year.'-'.$date_from_month.'-'.$date_from_day.' 23:59:59');   

    $time=$date_from_year.'_'.$date_from_month.'_'.$date_from_day;
    $when=$date_from_day.'-'.$date_from_month.'-'.$date_from_year;
   
    $rows = array();
    $sql= "
        SELECT count(product_url) AS 'Clicks', product_name , partner_name , product_url
        FROM {usertrends_partner_click_log}
        WHERE log_type = '%s'
        AND (when_clicked >=%d AND when_clicked <=%d)
        GROUP BY product_url
        ORDER BY Clicks DESC   
    ";

    $query=db_query($sql,$log_type,$date_from,$date_to);
    $cnt=0;
    $total_clicks=0;
    while ($row = db_fetch_array($query)) {
        if($cnt<1){$headers=array_keys($row);}
        $total_clicks+=$row['Clicks'];
        $rows[]=$row;
        $cnt++;
    }
    $return1='';
    $attributes = array('width' => '77%;' );   
   
    if($cnt>0){
   
        if($report_type!='Display'){
            $table1 = theme_table($headers,$rows,$attributes);
        }
           
        $return1 .= "<h2>Report1: Summary of clicks for $log_type on $when : $total_clicks TOTAL CLICKS</h2>";
        $return1 .= theme_table($headers,$rows,$attributes);

       
        $rows2 = array();
        $sql= "
            SELECT a.uid,a.mbid,u.profile_title,u.profile_firstname,u.profile_surname,us.mail,a.product_name,when_clicked,a.product_url FROM usertrends_partner_click_log a
            INNER JOIN databasefordrupal.userprofile u
            ON a.uid=u.uid
            INNER JOIN databasefordrupal.users us
            ON u.uid=us.uid
            WHERE log_type = '%s'
            AND (a.when_clicked >=%d AND a.when_clicked <=%d)
            ORDER BY a.when_clicked
        ";       
       
        $query=db_query($sql,$log_type,$date_from,$date_to);
        $cnt2=0;
        while ($row = db_fetch_array($query)) {
            if($cnt2<1){$headers=array_keys($row);}
            $row['when_clicked']=date('Y-m-d H:i:s',$row['when_clicked']);
            $rows2[]=$row;
            $cnt2++;
        }
        $return2='';

        if($report_type!='Display'){
       
            $table2 .= theme_table($headers,$rows2,$attributes);

            $filename= "$log_type".'_click_log_'.$time.'.xls';
            $attributes = array('width' => '77%;' );
       
            $output = '<html>';
            $output .= '<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"></head>';
            $output .= '<body>';
            $output .= "<b>Report1: Summary of clicks for $log_type on $when : $total_clicks TOTAL CLICKS</b><hr/>";
            $output .= $table1;
            $output .= "<hr/><b>Report2: $log_type List of mum who clicked and what clicked on $when</b><hr/>";
            $output .= $table2;
            $output .= "</body></html>";

           
            $mailto  = 'person1@email.com' . ', ';
            $mailto .= 'person2@email.com' . ', ';
            $mailto .= 'person3@email.com' . ', ';
            $mailto .= 'person4@email.com' . ', ';
            $mailto .= 'person5@email.com';
                       

            $subject="Report1: Summary of $log_type clicks on $when : $total_clicks TOTAL CLICKS";
            $content = chunk_split(base64_encode($output));
            $uid = md5(uniqid(time()));

   
            $message = "<h2>$subject file attached.</h2>";

            $header = "From:webadmin <admin@email.com>\r\n";
            $header .= "Reply-To: admin@email.com\r\n";
            $header .= "MIME-Version: 1.0\r\n";
            $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
            $header .= "This is a multi-part message in MIME format.\r\n";
            $header .= "--".$uid."\r\n";
            $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
            $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            $header .= $message."\r\n\r\n";
            $header .= "--".$uid."\r\n";
            $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
            $header .= "Content-Transfer-Encoding: base64\r\n";
            $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
            $header .= $content."\r\n\r\n";
            $header .= "--".$uid."--";
            if (@mail($mailto, $subject, "", $header)) {
                return TRUE;
            } else {
                echo FALSE;
            }   
        }
           
        $return2 .= "<br/><hr/><h2>Report2: $log_type List of mum who clicked and what clicked on $when </h2>";
        $return2 .= theme_table($headers,$rows2,$attributes);
           
        $report = $return1.$return2;
    }
   
    return $output;