Skip to content

Instantly share code, notes, and snippets.

@JudeRosario
Created December 17, 2019 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JudeRosario/11e41f0bb7fff806001296cf97e4465b to your computer and use it in GitHub Desktop.
Save JudeRosario/11e41f0bb7fff806001296cf97e4465b to your computer and use it in GitHub Desktop.
function emailPreviousMonthReportTo($To)
{
global $wpdb;
//Get Last Months Report
$arrResult = $wpdb->get_results("SELECT `id`,
`date`,
`first_name`,
`last_name`,
`email`,
`business_name`,
`phone`,
`city`,
`country`,
`state`,
`joined_date`,
`product_title` FROM `".self::LOG_TABLE_NAME."` WHERE MONTH(`date`) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) ORDER BY `date` DESC;");
$CurrentMonth = date("F",strtotime("-1 month"));
$filepath = dirname(__FILE__) . '/TEIRockDrills_Brochures_'.$CurrentMonth.'.csv';
$fileHandle = fopen($filepath, 'w');
if($fileHandle === FALSE) {
die('Failed to open temporary file');
}
fputcsv( $fileHandle, array('Entry ID',
'Date',
'First Name',
'Last Name',
'Email',
'Business Name',
'Phone',
'City',
'Country',
'State',
'Joined Date',
'Product Title'));
foreach ( $arrResult as $Entry ) {
$modified_values = array($Entry->id,
$Entry->date,
$Entry->first_name,
$Entry->last_name,
$Entry->email,
$Entry->business_name,
$Entry->phone,
$Entry->city,
$Entry->country,
$Entry->state,
$Entry->joined_date,
$Entry->product_title);
fputcsv( $fileHandle, $modified_values );
}
rewind($fileHandle);
fclose($fileHandle);
$Subject = "TEIRockDrills Monthly Report - ".$CurrentMonth;
$Message = "Attached is the monthly report for the month of ".$CurrentMonth.".";
$Headers[] = 'From: TEI RockDrills <NoReply@TEIRockDrills.com>';
$Attachments = array($filepath);
wp_mail( $To, $Subject, $Message, $Headers, $Attachments );
// unlink($filepath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment