Skip to content

Commit

Permalink
[#2] mail processor (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjendres committed Dec 4, 2018
1 parent 78d247b commit 65666fd
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 38 deletions.
66 changes: 65 additions & 1 deletion CRM/Mailingtools/AnonymousOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*-------------------------------------------------------+
| SYSTOPIA Mailingtools Extension |
| Copyright (C) 2018 SYSTOPIA |
| Author: B. Endres ([email protected]) |
| Author: B. Endres ([email protected]) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
Expand Down Expand Up @@ -88,9 +88,73 @@ public static function processAnonymousOpenEvent($mid) {
}

// all good: add entry
CRM_Core_Error::debug_log_message("Tracked anonymous open event for mailing [{$mid}]");
CRM_Core_DAO::executeQuery("
INSERT INTO civicrm_mailing_event_opened (event_queue_id, time_stamp)
VALUES (%1, NOW())", [
1 => [$event_queue_id, 'Integer']]);
}

/**
* This function will manipulate open tracker URLs in emails, so they point
* to the anonymous handler instead of the native one
*/
public static function modifyEmailBody(&$body) {
$config = CRM_Mailingtools_Config::singleton();
if (!$config->getSetting('anonymous_open_enabled')
|| !$config->getSetting('anonymous_open_url')) {
// NOT ENABLED
return;
}

// get the base URL
$core_config = CRM_Core_Config::singleton();
$system_base = $core_config->userFrameworkBaseURL;

// find all all relevant links and collect queue IDs
if (preg_match_all("#{$system_base}sites/all/modules/civicrm/extern/open.php\?q=(?P<queue_id>[0-9]+)[^0-9]#i", $body, $matches)) {
$queue_ids = $matches['queue_id'];

if (!empty($queue_ids)) {
// resolve queue_id => mailing_id
$queue_id_to_mailing_id = self::getQueueID2MailingID($queue_ids);

// replace open trackers
foreach ($queue_id_to_mailing_id as $queue_id => $mailing_id) {
$new_url = $config->getSetting('anonymous_open_url') . "?mid={$mailing_id}";
$body = preg_replace("#{$system_base}sites/all/modules/civicrm/extern/open.php\?q={$queue_id}#i", $new_url, $body);
}
}
}
}

/**
* Resolve a list of queue ids to mailing IDs
*
* @param $queue_ids array list of queue IDs
* @return array list of queue_id => mailing id
*
* @todo: pre-caching of all queue IDs for the current mailing?
*/
public static function getQueueID2MailingID($queue_ids) {
$queue_id_to_mailing_id = [];
if (empty($queue_ids) || !is_array($queue_ids)) {
return $queue_id_to_mailing_id;
}

// run the query
$queue_id_list = implode(',', $queue_ids);
$query = CRM_Core_DAO::executeQuery("
SELECT queue.id AS queue_id,
job.mailing_id AS mailing_id
FROM civicrm_mailing_event_queue queue
LEFT JOIN civicrm_mailing_job job ON queue.job_id = job.id
WHERE queue.id IN ({$queue_id_list})
GROUP BY queue.id");
while ($query->fetch()) {
$queue_id_to_mailing_id[$query->queue_id] = $query->mailing_id;
}

return $queue_id_to_mailing_id;
}
}
50 changes: 50 additions & 0 deletions CRM/Mailingtools/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA Mailingtools Extension |
| Copyright (C) 2018 SYSTOPIA |
| Author: B. Endres ([email protected]) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

/**
* Wrapper for CiviCRM Mailer
*/
class CRM_Mailingtools_Mailer {

/**
* this is the orginal, wrapped mailer
*/
protected $mailer = NULL;

/**
* Check if the deployment of this mailer wrapper is needed
*/
public static function isNeeded() {
$config = CRM_Mailingtools_Config::singleton();
return $config->getSetting('anonymous_open_enabled')
&& $config->getSetting('anonymous_open_url');
}

/**
* construct this mailer wrapping another one
*/
public function __construct($mailer) {
$this->mailer = $mailer;
}

/**
* Send an email via the wrapped mailer,
* mending the URLs contained
*/
function send($recipients, $headers, $body) {
CRM_Mailingtools_AnonymousOpen::modifyEmailBody($body);
$this->mailer->send($recipients, $headers, $body);
}
}
46 changes: 9 additions & 37 deletions mailingtools.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ function mailingtools_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_mailingtools_civix_civicrm_alterSettingsFolders($metaDataFolders);
}

/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/
//function mailingtools_civicrm_entityTypes(&$entityTypes) {
// _mailingtools_civix_civicrm_entityTypes($entityTypes);
//}

/**
* Implementes hook_civicrm_alterMailParams
*
Expand All @@ -148,30 +137,13 @@ function mailingtools_civicrm_alterMailParams(&$params, $context) {
CRM_Mailingtools_InjectHeader::inject_header($params, $context);
}

// --- Functions below this ship commented out. Uncomment as required. ---

/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
function mailingtools_civicrm_preProcess($formName, &$form) {
} // */

/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function mailingtools_civicrm_navigationMenu(&$menu) {
_mailingtools_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_mailingtools_civix_navigationMenu($menu);
} // */
* We will provide our own Mailer (wrapping the original one).
* so we can manipulate the content of outgoing emails
*/
function mailingtools_civicrm_alterMailer(&$mailer, $driver, $params) {
$needed = CRM_Mailingtools_Mailer::isNeeded();
if ($needed) {
$mailer = new CRM_Mailingtools_Mailer($mailer);
}
}

0 comments on commit 65666fd

Please sign in to comment.