diff --git a/.gitignore b/.gitignore index 644e897..74612bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ bin/setup.conf +.DS_Store +.project diff --git a/CRM/Mailjet/BAO/Event.php b/CRM/Mailjet/BAO/Event.php index 137d40f..03cdc3a 100644 --- a/CRM/Mailjet/BAO/Event.php +++ b/CRM/Mailjet/BAO/Event.php @@ -5,9 +5,11 @@ class CRM_Mailjet_BAO_Event extends CRM_Mailjet_DAO_Event { static function getMailjetCustomCampaignId($jobId){ if($jobId !== null){ + //get the mailing job $mailingJob = civicrm_api3('MailingJob', 'get', $params = array('id' => $jobId)); if(isset($mailingJob['values'][$jobId]['job_type'])){ $jobType = $mailingJob['values'][$jobId]['job_type']; + //if job is not test if($jobType == 'child'){ $timestamp = strtotime($mailingJob['values'][$jobId]['scheduled_date']); return $jobId . 'MJ' . $timestamp; @@ -23,7 +25,8 @@ static function recordBounce($params) { $mailingId = CRM_Utils_Array::value('mailing_id', $params); //CiviCRM mailling ID $contactId = CRM_Utils_Array::value('contact_id' , $params); $emailId = CRM_Utils_Array::value('email_id' , $params); - $jobId = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mailingId, 'id', 'mailing_id'); + $email = CRM_Utils_Array::value('email' , $params); + $jobId = CRM_Utils_Array::value('job_id' , $params); $eqParams = array( 'job_id' => $jobId, 'contact_id' => $contactId, diff --git a/CRM/Utils/Mail/MailjetProcessor.php b/CRM/Utils/Mail/MailjetProcessor.php index bde96a6..d574f46 100644 --- a/CRM/Utils/Mail/MailjetProcessor.php +++ b/CRM/Utils/Mail/MailjetProcessor.php @@ -43,35 +43,71 @@ class CRM_Utils_Mail_MailjetProcessor { */ static function processBounces($mailingId = NULL) { require_once('packages/mailjet-0.1/php-mailjet.class-mailjet-0.1.php'); - // Create a new Mailjet Object + // Create a new Mailjet Object @php-mailjet.class-mailjet-0.1.php $mj = new Mailjet(MAILJET_API_KEY, MAILJET_SECRET_KEY); + + //G: TODO $mj->debug = 0; + if($mailingId){ - $mailjetParams = array('custom_campaign' => CRM_Mailjet_BAO_Event::getMailjetCustomCampaignId($mailingId)); + $apiParams = array( + 'mailing_id' => $mailingId + ); + $campaignJobId = 0; + $mailJobResult = civicrm_api3('MailingJob', 'get', $apiParams); + foreach ($mailJobResult['values'] as $jobId => $currentJob) { + if(isset($currentJob['job_type'])){ + $jobType = $currentJob['job_type']; + //if job is not test + if($jobType == 'child'){ + $campaignJobId = $jobId; + } + } + } + + $mailjetParams = array('custom_campaign' => CRM_Mailjet_BAO_Event::getMailjetCustomCampaignId($campaignJobId)); + + //G: https://uk.mailjet.com/docs/api/message/list + //List all your messages (both transactional and campaign) with numerous filters. $response = $mj->messageList($mailjetParams); if(!$response){ return TRUE; //always return true - we don't process bounces if there is no reponse. } $campaign = $response->result[0]; + //G: https://uk.mailjet.com/docs/api/report/emailbounce + //Lists emails declared as bounce. + //Call $response = $mj->reportEmailBounce(array('campaign_id' => $campaign->id)); }else{ $response = $mj->reportEmailBounce(); } + //Result $bounces = $response->bounces; foreach ($bounces as $bounce) { $params = array('email' => $bounce->email,'sequential' => 1); $emailResult = civicrm_api3('Email', 'get', $params); if(!empty($emailResult['values'])){ //we always get the first result + //TODO: might related to bounce record issue $contactId = $emailResult['values'][0]['contact_id']; $emailId = $emailResult['values'][0]['id']; + $emailAddress = $emailResult['values'][0]['email']; if(!$bounce->customcampaign){ //do not process bounce if we dont have custom campaign continue; } $campaingArray = explode("MJ", $bounce->customcampaign); - $mailingId = $campaingArray[0]; - $params = array( + //TODO: related to bounce record issue + $jobId = $campaingArray[0]; + $mailingJobResult = civicrm_api3('MailingJob', 'get', array('id'=>$jobId)); + $mailingResult = civicrm_api3('Mailing', 'get', array('id'=>$mailingJobResult['values'][$jobId]['mailing_id'])); + + $currentMailingId = 0; + foreach ($mailingResult['values'] as $mailingId => $mailing) { + $currentMailingId = $mailingId; + } + + /*$params = array( 'mailing_id' => $mailingId, ); $result = civicrm_api3('MailingJob', 'get', $params); @@ -83,12 +119,12 @@ static function processBounces($mailingId = NULL) { $params = array( 1 => array( $contactId, 'Integer'), 2 => array( $emailId, 'Integer') - ); + );*/ $query = "SELECT eq.id FROM civicrm_mailing_event_bounce eb LEFT JOIN civicrm_mailing_event_queue eq ON eq.id = eb.event_queue_id WHERE 1 - AND eq.job_id IN ($jobIds) + AND eq.job_id = $jobId AND eq.email_id = $emailId AND eq.contact_id = $contactId"; $dao = CRM_Core_DAO::executeQuery($query); @@ -101,9 +137,11 @@ static function processBounces($mailingId = NULL) { if(!$isBounceRecord){ $bounceArray = array( 'is_spam' => FALSE, - 'mailing_id' => $mailingId, + 'mailing_id' => $currentMailingId, + 'job_id' => $jobId, 'contact_id' => $contactId, 'email_id' => $emailId, + 'email' => $emailAddress, 'blocked' => 0, //if it's manual refresh, we fource it as a normal bounce not blocked 'hard_bounce' => $bounce->hard_bounce, 'date_ts' => $bounce->date_ts, diff --git a/api/v3/Mailjet.php b/api/v3/Mailjet.php index dd3e096..fae12e6 100644 --- a/api/v3/Mailjet.php +++ b/api/v3/Mailjet.php @@ -25,6 +25,7 @@ function civicrm_api3_mailjet_processbounces($params) { return civicrm_api3_create_error('Could not acquire lock, another MailjetProcessor process is running'); } $mailingId = CRM_Utils_Array::value('mailing_id', $params); + //G: this is called when click on "Manually refresh Mailjet's stats" button if (!CRM_Utils_Mail_MailjetProcessor::processBounces($mailingId)) { $lock->release(); return civicrm_api3_create_error('Process Bounces failed'); diff --git a/templates/CRM/Mailjet/Page/Report.tpl b/templates/CRM/Mailjet/Page/Report.tpl index cfcea4c..608bd77 100644 --- a/templates/CRM/Mailjet/Page/Report.tpl +++ b/templates/CRM/Mailjet/Page/Report.tpl @@ -158,6 +158,8 @@ } }); + //G: lock is called here + //TODO: check mailing_id $( "#updateMailjetButton" ).on( "click", function() { CRM.api('Mailjet','processBounces',{'mailing_id': {/literal}{$mailing_id}{literal}}, {success: function(data) {