170 lines
5.3 KiB
PHP
170 lines
5.3 KiB
PHP
<?php
|
|
|
|
// require_once( '/Users/osx/Sites/Float/adminsavvy/backend.php' );
|
|
include '../backend.php';
|
|
require_once('../vendor/autoload.php');
|
|
require_once('common/Logger.php');
|
|
|
|
use \SendGrid\Mail\Mail;
|
|
|
|
/**
|
|
* Send email Float App sign up emails last 24 hours daily report
|
|
*/
|
|
class EmailLast24hrsReport
|
|
{
|
|
|
|
public $conn;
|
|
public $repConn;
|
|
|
|
public $sendgrid_api;
|
|
public $sender;
|
|
public $sender_name;
|
|
public $receivers;
|
|
|
|
protected $hard_limit = 550;
|
|
|
|
function __construct()
|
|
{
|
|
global $savvyext;
|
|
$this->conn = self::__cnn($savvyext);
|
|
$this->repConn = self::__repCnn($savvyext);
|
|
|
|
$this->sendgrid_api = $savvyext->cfgReadChar('mailsend.api_key');
|
|
$this->sender = $savvyext->cfgReadChar('mailsend.from');
|
|
$this->sender_name = $savvyext->cfgReadChar('mailsend.name');
|
|
$this->receivers = array(
|
|
'jervis@float.sg' => 'Jervis',
|
|
'tnguyen@float.sg' => 'Tri Nguyen',
|
|
//'charlie.nguyen.goldenowl@gmail.com' => 'Charlie Nguyen',
|
|
);
|
|
}
|
|
|
|
private static function __cnn($savvyext)
|
|
{
|
|
$db_host = $savvyext->cfgReadChar('database.host');
|
|
$db_name = $savvyext->cfgReadChar('database.name');
|
|
$db_user = $savvyext->cfgReadChar('database.user');
|
|
$db_pass = $savvyext->cfgReadChar('database.pass');
|
|
$db_port = $savvyext->cfgReadLong('database.port');
|
|
$connstr = sprintf('host=%s port=%s dbname=%s user=%s password=%s', $db_host, $db_port, $db_name, $db_user, $db_pass);
|
|
$conn = pg_connect($connstr);
|
|
|
|
return $conn;
|
|
}
|
|
|
|
private static function __repCnn($savvyext)
|
|
{
|
|
$db_host = $savvyext->cfgReadChar('database_replica.host');
|
|
$db_name = $savvyext->cfgReadChar('database_replica.name');
|
|
$db_user = $savvyext->cfgReadChar('database_replica.user');
|
|
$db_pass = $savvyext->cfgReadChar('database_replica.pass');
|
|
$db_port = $savvyext->cfgReadLong('database_replica.port');
|
|
$connstr = sprintf('host=%s port=%s dbname=%s user=%s password=%s', $db_host, $db_port, $db_name, $db_user, $db_pass);
|
|
$conn = pg_connect($connstr);
|
|
|
|
return $conn;
|
|
}
|
|
|
|
private function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "\\")
|
|
{
|
|
$f = fopen('php://memory', 'r+');
|
|
foreach ($data as $item) {
|
|
fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
|
|
}
|
|
rewind($f);
|
|
return stream_get_contents($f);
|
|
}
|
|
/**
|
|
* Send email report via SendGrid
|
|
* @param array $data list email fail
|
|
* @return mixed
|
|
*/
|
|
protected function send($data)
|
|
{
|
|
$short_date = (new \DateTime())->format('Y-M-d');
|
|
// create email layout
|
|
ob_start();
|
|
require_once(__DIR__ . '/email/sign_up_email_last_24hrs.php');
|
|
$layout = ob_get_clean();
|
|
// create sendgrid instance
|
|
$sendgrid = new \SendGrid\Mail\Mail;
|
|
$sendgrid->setFrom($this->sender, $this->sender_name);
|
|
$sendgrid->setSubject('Float App sign up emails last 24 hours daily report');
|
|
$sendgrid->addTos($this->receivers);
|
|
$sendgrid->addContent('text/html', $layout);
|
|
|
|
|
|
//HEADER of csv files
|
|
array_unshift($data,[
|
|
'Date','Email name', 'Total emails'
|
|
]);
|
|
$att1 = new \SendGrid\Mail\Attachment();
|
|
//$att1->setContent( file_get_contents("test_files/1/1.txt") );
|
|
$att1->setContent( $this->array2csv($data) );
|
|
$att1->setType("text/csv");
|
|
|
|
$att1->setFilename("sign-up-emails-last-24-hrs-report-".$short_date.".csv");
|
|
$att1->setDisposition("attachment");
|
|
$sendgrid->addAttachment( $att1 );
|
|
|
|
// send
|
|
$send = new \SendGrid($this->sendgrid_api);
|
|
$response = $send->send($sendgrid);
|
|
if ($response->statusCode() !== 202) {
|
|
throw new Exception('Send email via Sendgrid get errors!');
|
|
}
|
|
}
|
|
public function getemails()
|
|
{
|
|
try {
|
|
// HH24:MI
|
|
$sql = sprintf("
|
|
SELECT to_char(added::DATE,'YYYY-Mon-DD') as date,'Welcome to Float' as email_name, count(id) as total FROM members
|
|
WHERE added >= NOW() - INTERVAL '24 HOURS'
|
|
GROUP BY added::DATE
|
|
ORDER BY added::DATE ASC;
|
|
");
|
|
|
|
$results = pg_query($this->repConn, $sql);
|
|
|
|
if ($error = pg_last_error($this->repConn)) {
|
|
throw new Exception($error);
|
|
}
|
|
|
|
return pg_fetch_all($results);
|
|
|
|
} catch (Exception $e) {
|
|
Logger::debug($e->getMessage());
|
|
}
|
|
}
|
|
private function getEmptyData(){
|
|
return [
|
|
'date' => (new \DateTime())->modify('-24 hours')->format('Y-M-d'), // H:i
|
|
'email_name' => 'Welcome to Float',
|
|
'total' => '0',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Excute jobs
|
|
* @return mixed
|
|
*/
|
|
public function excute()
|
|
{
|
|
try {
|
|
$data = $this->getemails();
|
|
if (!$data) {
|
|
$data = [$this->getEmptyData()];
|
|
//throw new Exception('No Email Receipt which parsed failed!');
|
|
}
|
|
|
|
|
|
$send = $this->send($data);
|
|
} catch (Exception $e) {
|
|
Logger::debug($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
$instace = new EmailLast24hrsReport;
|
|
$instace->excute(); |