$val){ // Skip attachment explode for first row if( $kk != 0 && $attachment_key !== false && !empty($val[$attachment_key]) ){ $attachment = explode(',', $val[$attachment_key]); $attach = ''; foreach($attachment as $attach_val){ $attach .= explode('*', $attach_val)[2].', '; } $val[$attachment_key] = rtrim($attach, ', '); } fputcsv($file, array_values($val)); } fclose($file); wp_die(); } // Export XML files function gosmtp_export_xls($data){ $filename = "gosmtp_export_".date('Ymd') . ".xls"; header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=\"$filename\""); // We have all the field sequences in the first line // Get the key of attachments form the sequences $attachment_key = array_search('attachments', $data[0], true); foreach($data as $kk => $val) { array_walk($val, 'gosmtp_filterData'); // Skip attachment explode for first row if( $kk != 0 && $attachment_key !== false && !empty($val[$attachment_key]) ){ $attachment = explode(',', $val[$attachment_key]); $attach =""; foreach($attachment as $attach_val){ $attach .= explode('*', $attach_val)[2].', '; } $val[$attachment_key] = rtrim($attach, ", "); } echo esc_html(implode("\t", array_values($val)) . "\n"); } wp_die(); } // Export EML files function gosmtp_export_eml($data){ $files = array(); $first_row = $data[0]; // Replace key with value $fields = array_flip($data[0]); $attachment_key = $fields['attachments']; foreach($data as $key => $value){ // Skip for first row if($key == 0){ continue; } $files[$key] = "gosmtp_export_".$key."_".date('Ymd') . ".eml"; header("Content-Type: application/application/eml"); header("Content-Disposition: attachment; filename=\"$files[$key]\""); $output_buffer = fopen('php://output', 'w'); $boundary = md5(uniqid(mt_rand())); $output_buffer = 'MIME-Version: 1.0 Date: '.date_format( date_create( $value[$fields['created_at']] ), 'd-m-y H:i:s').' +0100 From: '.$value[$fields['from']].' To: '.$value[$fields['to']].' Cc: '.$value[$fields['cc']].' Ccc: '.$value[$fields['bcc']].' Subject: '.$value[$fields['subject']].' Content-Type: multipart/mixed; boundary="'.$boundary.'" This is a message with multiple parts in MIME format. --'.$boundary.' Content-Type: '.$value[$fields['content-type']].' '.$value[$fields['body']]; // Added attachment if any if(isset($value[$attachment_key]) && !empty($value[$attachment_key])){ $attachment = explode(',', $value[$attachment_key]); foreach($attachment as $attach){ $all_attach = explode('*', $attach); $attachment_file = file_get_contents(trim($all_attach[0])); if(empty($attachment_file)){ continue; } $encoded_attach = base64_encode($attachment_file); $type = $all_attach[4]; $base_name = $all_attach[2]; $output_buffer .= ' --'.$boundary.' Content-Type: '.$type.';name="'.$base_name.'" Content-Transfer-Encoding: base64 Content-Disposition: attachment;filename="'.$base_name.'" '.$encoded_attach; } } file_put_contents($files[$key], $output_buffer); } $zipname = "gosmtp_export_".date('Ymd') . ".zip"; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach($files as $file){ $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname); unlink($zipname); wp_die(); } // Export page HTML function gosmtp_export_page(){ // Styles and Scripts wp_enqueue_style( 'gosmtp-admin' ); wp_enqueue_script( 'gosmtp-admin' ); $common_field = array( 'to' => __('To Address'), 'from' => __('From Address'), 'subject' => __('Subject'), 'body' => __('Body'), 'created_at' => __('Created At'), 'attachments' => __('Attachments'), ); $addtition_field = array( 'status' => __('Status'), 'reply-to' => __('Reply To'), 'cc' => __('Carbon Copy (CC)'), 'bcc' => __('Blind Carbon Copy (BCC)'), 'provider' => __('Provider'), 'response' => __('Response'), 'source' => __('Source'), 'content-type' => __('Content Type') ); $search_field = array( 'from' => __('from'), 'to' => __('To'), 'subject' => __('Subject'), 'body' => __('Body'), ); $all_field = array_merge(array_keys($common_field), array_keys($addtition_field)); ?>