From e29b3607131dbcc88bf84d26c8a8990fc10aecc8 Mon Sep 17 00:00:00 2001 From: popcm Date: Thu, 5 Jun 2014 11:58:01 +0100 Subject: [PATCH 1/5] Fixed the issue that when one email address got deleted when put on hold. --- CRM/Mailjet/BAO/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Mailjet/BAO/Event.php b/CRM/Mailjet/BAO/Event.php index 137d40f..b10df18 100644 --- a/CRM/Mailjet/BAO/Event.php +++ b/CRM/Mailjet/BAO/Event.php @@ -56,7 +56,7 @@ static function recordBounce($params) { //put the email into on hold $params = array( 'id' => $emailId, - 'email' => $email, + //'email' => $email, 'on_hold' => 1, 'hold_date' => $time, ); From 5334420e83893d5d4125db4fadad3be333d9a617 Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Tue, 24 Jun 2014 15:51:07 +0100 Subject: [PATCH 2/5] Added detail comments for future debugging. --- .gitignore | 2 ++ CRM/Mailjet/BAO/Event.php | 2 +- CRM/Utils/Mail/MailjetProcessor.php | 14 +++++++++++++- api/v3/Mailjet.php | 1 + templates/CRM/Mailjet/Page/Report.tpl | 2 ++ 5 files changed, 19 insertions(+), 2 deletions(-) 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..b10df18 100644 --- a/CRM/Mailjet/BAO/Event.php +++ b/CRM/Mailjet/BAO/Event.php @@ -56,7 +56,7 @@ static function recordBounce($params) { //put the email into on hold $params = array( 'id' => $emailId, - 'email' => $email, + //'email' => $email, 'on_hold' => 1, 'hold_date' => $time, ); diff --git a/CRM/Utils/Mail/MailjetProcessor.php b/CRM/Utils/Mail/MailjetProcessor.php index bde96a6..898e13e 100644 --- a/CRM/Utils/Mail/MailjetProcessor.php +++ b/CRM/Utils/Mail/MailjetProcessor.php @@ -43,26 +43,37 @@ 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)); + + //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']; if(!$bounce->customcampaign){ @@ -70,6 +81,7 @@ static function processBounces($mailingId = NULL) { continue; } $campaingArray = explode("MJ", $bounce->customcampaign); + //TODO: related to bounce record issue $mailingId = $campaingArray[0]; $params = array( 'mailing_id' => $mailingId, 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) { From 2d983949a8e412c87d6728f3df76ee027f22e761 Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Tue, 24 Jun 2014 19:09:54 +0100 Subject: [PATCH 3/5] Fixed the bounce fetch from Mailjet. --- CRM/Mailjet/BAO/Event.php | 2 ++ CRM/Utils/Mail/MailjetProcessor.php | 35 ++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CRM/Mailjet/BAO/Event.php b/CRM/Mailjet/BAO/Event.php index b10df18..2d30769 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; diff --git a/CRM/Utils/Mail/MailjetProcessor.php b/CRM/Utils/Mail/MailjetProcessor.php index 898e13e..4d72c06 100644 --- a/CRM/Utils/Mail/MailjetProcessor.php +++ b/CRM/Utils/Mail/MailjetProcessor.php @@ -50,7 +50,22 @@ static function processBounces($mailingId = NULL) { $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. @@ -82,8 +97,16 @@ static function processBounces($mailingId = NULL) { } $campaingArray = explode("MJ", $bounce->customcampaign); //TODO: related to bounce record issue - $mailingId = $campaingArray[0]; - $params = array( + $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); @@ -95,12 +118,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); @@ -113,7 +136,7 @@ static function processBounces($mailingId = NULL) { if(!$isBounceRecord){ $bounceArray = array( 'is_spam' => FALSE, - 'mailing_id' => $mailingId, + 'mailing_id' => $$currentMailingId, 'contact_id' => $contactId, 'email_id' => $emailId, 'blocked' => 0, //if it's manual refresh, we fource it as a normal bounce not blocked From 479719671949cf11de243c9d9a8a8df0448a3b82 Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Thu, 3 Jul 2014 16:37:03 +0100 Subject: [PATCH 4/5] Bounce now will be recorded to contact correctly and the email address will not get deleted --- CRM/Mailjet/BAO/Event.php | 3 ++- CRM/Utils/Mail/MailjetProcessor.php | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CRM/Mailjet/BAO/Event.php b/CRM/Mailjet/BAO/Event.php index 2d30769..6856021 100644 --- a/CRM/Mailjet/BAO/Event.php +++ b/CRM/Mailjet/BAO/Event.php @@ -25,6 +25,7 @@ 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); + $email = CRM_Utils_Array::value('email' , $params); $jobId = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mailingId, 'id', 'mailing_id'); $eqParams = array( 'job_id' => $jobId, @@ -58,7 +59,7 @@ static function recordBounce($params) { //put the email into on hold $params = array( 'id' => $emailId, - //'email' => $email, + 'email' => $email, 'on_hold' => 1, 'hold_date' => $time, ); diff --git a/CRM/Utils/Mail/MailjetProcessor.php b/CRM/Utils/Mail/MailjetProcessor.php index 4d72c06..4e7b935 100644 --- a/CRM/Utils/Mail/MailjetProcessor.php +++ b/CRM/Utils/Mail/MailjetProcessor.php @@ -91,6 +91,7 @@ static function processBounces($mailingId = NULL) { //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; @@ -136,9 +137,10 @@ static function processBounces($mailingId = NULL) { if(!$isBounceRecord){ $bounceArray = array( 'is_spam' => FALSE, - 'mailing_id' => $$currentMailingId, + 'mailing_id' => $currentMailingId, '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, From a8d45cb8f72143cc74fe8dbd2676405e9f961be7 Mon Sep 17 00:00:00 2001 From: Guanhuan Chen Date: Thu, 3 Jul 2014 17:46:44 +0100 Subject: [PATCH 5/5] Correct the Job ID in event processing part. --- CRM/Mailjet/BAO/Event.php | 2 +- CRM/Utils/Mail/MailjetProcessor.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CRM/Mailjet/BAO/Event.php b/CRM/Mailjet/BAO/Event.php index 6856021..03cdc3a 100644 --- a/CRM/Mailjet/BAO/Event.php +++ b/CRM/Mailjet/BAO/Event.php @@ -26,7 +26,7 @@ static function recordBounce($params) { $contactId = CRM_Utils_Array::value('contact_id' , $params); $emailId = CRM_Utils_Array::value('email_id' , $params); $email = CRM_Utils_Array::value('email' , $params); - $jobId = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $mailingId, 'id', 'mailing_id'); + $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 4e7b935..d574f46 100644 --- a/CRM/Utils/Mail/MailjetProcessor.php +++ b/CRM/Utils/Mail/MailjetProcessor.php @@ -138,6 +138,7 @@ static function processBounces($mailingId = NULL) { $bounceArray = array( 'is_spam' => FALSE, 'mailing_id' => $currentMailingId, + 'job_id' => $jobId, 'contact_id' => $contactId, 'email_id' => $emailId, 'email' => $emailAddress,