Function Array CSV Attachment
-
Hi,
I’m currently working on a function that will be fired by a cron job. My issue, I’m sure will be the cron at some point but for the time being I’m struggling with the csv attachment.
The current function downloads the csv perfectly with the data and formatting. The attachment comes through but it’s a blank file.
Any insights would be greatly appreciated.
unction weekly_lead_report($array_data, $filename){ header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="' . $filename . '"'); $output = fopen('php://output', 'w'); foreach($array_data as $row){ fputcsv($output, $row, ',', '"'); } fclose($output); $encoded = chunk_split(base64_encode($output)); } function lms_lead_posts($posttypes){ $args = array( 'posts_per_page' => '-1', 'post_status' => 'publish', 'orderby' => 'date', 'post_type' => '', 'date_query' => array( array( 'after' => '1 week ago' ) ) ); $output = array(); //DEFINE TITLES $titles = array('Date', 'Name', 'Phone', 'Email', 'License Status', 'Follow Up Date', 'Message'); array_push($output, $titles); foreach($posttypes as $type) { $args['post_type'] = $type; $query = new WP_Query( $args); if ( $query->have_posts() ): while ( $query->have_posts() ): $query->the_post(); //DEFINE DATA FIELDS $row = array( get_the_date('M j'),get_post_meta( get_the_id(),'lead_first_name', true ) . ' ' . get_post_meta( get_the_id(),'lead_last_name', true ),get_post_meta( get_the_id(),'lead_phone', true ),get_the_title(),get_post_meta( get_the_id(),'lead_re_school_status', true ),get_post_meta( get_the_id(),'lead_followup_date', true ),get_post_meta( get_the_id(),'lead_msg', true ) ); array_push($output, $row); ob_end_clean(); endwhile; endif; wp_reset_postdata(); } weekly_lead_report($output, 'weekly_lead_report.csv'); $weekly_send = new PHPMailer(); $weekly_send->Debugoutput = 'html'; $weekly_send->Host = "localhost"; $weekly_send->SetFrom('[email protected]', 'Some Name'); $weekly_send->AddReplyTo('[email protected]', 'Some Name'); $weekly_send->AddAddress('[email protected]', 'That Dude'); $weekly_send->AddStringAttachment($output, 'weekly_lead_report.csv'); $weekly_send->Subject = 'That Dudes Weekly Lead Report'; $weekly_send->Body = 'Your Some Dudes weekly lead report is attached'; $weekly_send->send(); } if( isset($_GET['lead_data']) ){ $posttypes = array('leads'); lms_lead_posts($posttypes); die(); } if( isset($_GET['_get_posttypes']) ){ $args = array( 'public' => true, '_builtin' => false ); $posttypes = get_post_types($args); print_r($posttypes); die(); }[moderator note: please put code in backticks. for larger snippets, please use gist.githumb.com and paste a link to it here.]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Function Array CSV Attachment’ is closed to new replies.