Skip to content

Commit

Permalink
Merge pull request #7 from popcm/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
guanhuan committed Jul 3, 2014
2 parents 446825e + a8d45cb commit b5367bf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
bin/setup.conf
.DS_Store
.project
5 changes: 4 additions & 1 deletion CRM/Mailjet/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
52 changes: 45 additions & 7 deletions CRM/Utils/Mail/MailjetProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions api/v3/Mailjet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions templates/CRM/Mailjet/Page/Report.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b5367bf

Please sign in to comment.