Skip to content

Instantly share code, notes, and snippets.

@JoeMurray
Created December 21, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeMurray/0d8663e9f034c0b5e23b123e75c9fa07 to your computer and use it in GitHub Desktop.
Save JoeMurray/0d8663e9f034c0b5e23b123e75c9fa07 to your computer and use it in GitHub Desktop.
diff patch to enable 2nd client sender for cividesk SparkPost ext
diff -ruN com.cividesk.email.sparkpost/CRM/Sparkpost/Page/callback.php /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/CRM/Sparkpost/Page/callback.php
--- com.cividesk.email.sparkpost/CRM/Sparkpost/Page/callback.php 2018-11-27 04:35:53.000000000 -0500
+++ /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/CRM/Sparkpost/Page/callback.php 2020-05-17 22:16:36.705126772 -0400
@@ -65,15 +65,16 @@
foreach ($elements as $element) {
if ($element->msys && ($event = $element->msys->message_event)) {
// Sanity checks
+ $client2 = CRM_Sparkpost::isclient2();
if ( !in_array($event->type, array('bounce', 'spam_complaint', 'policy_rejection'))
- || ($event->campaign_id && ($event->campaign_id != CRM_Sparkpost::getSetting('sparkpost_campaign')))
+ || ($event->campaign_id && ($event->campaign_id != CRM_Sparkpost::getSetting('sparkpost_campaign', $client2)))
|| (!$event->rcpt_meta || !($civimail_bounce_id = $event->rcpt_meta->{'X-CiviMail-Bounce'}))
) {
continue;
}
// Extract CiviMail parameters from header value
- $dao = new CRM_Core_DAO_MailSettings;
+ $dao = new CRM_Core_DAO_MailSettings();
$dao->domain_id = CRM_Core_Config::domainID();
$dao->is_default = TRUE;
if ( $dao->find(true) ) {
@@ -116,4 +117,4 @@
}
CRM_Utils_System::civiExit();
}
-}
\ No newline at end of file
+}
diff -ruN com.cividesk.email.sparkpost/CRM/Sparkpost.php /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/CRM/Sparkpost.php
--- com.cividesk.email.sparkpost/CRM/Sparkpost.php 2018-11-27 04:35:53.000000000 -0500
+++ /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/CRM/Sparkpost.php 2020-05-19 02:46:39.323837340 -0400
@@ -23,35 +23,52 @@
*/
class CRM_Sparkpost {
+
+ public static function isclient2($client2 = FALSE) {
+ if ($client2) {
+ return $client2;
+ }
+ $config = CRM_Core_Config::singleton();
+ if (strpos($config->userFrameworkBaseURL, 'client2') !== FALSE) {
+ return TRUE;
+ }
+ return FALSE;
+ }
+
const SPARKPOST_EXTENSION_SETTINGS = 'SparkPost Extension Settings';
// Indicates we need to try sending emails out through an alternate method
const FALLBACK = 1;
- static function setSetting($setting, $value) {
+ static function setSetting($setting, $value, $client2 = FALSE) {
// Encrypt API key before storing in database
if ($setting == 'sparkpost_apiKey') {
$value = CRM_Utils_Crypt::encrypt($value);
}
- return CRM_Core_BAO_Setting::setItem(
- $value,
- CRM_Sparkpost::SPARKPOST_EXTENSION_SETTINGS,
- $setting);
+ $isclient2 = self::isclient2($client2);
+ if ($isclient2) {
+ $setting = 'client2_' . $setting;
+ }
+ return Civi::settings()->set($setting, $value);
}
- static function getSetting($setting = NULL) {
+ static function getSetting($setting = NULL, $client2 = FALSE) {
// Start with the default values for settings
$settings = array(
'sparkpost_useBackupMailer' => false,
);
+ $isclient2 = self::isclient2($client2);
// Merge the settings defined in DB (no more groups in 4.7, so has to be one by one ...)
foreach (array('sparkpost_apiKey', 'sparkpost_useBackupMailer', 'sparkpost_campaign', 'sparkpost_ipPool', 'sparkpost_customCallbackUrl') as $name) {
- $value = CRM_Core_BAO_Setting::getItem(CRM_Sparkpost::SPARKPOST_EXTENSION_SETTINGS, $name);
+ $settingName = $isclient2 ? 'client2_' . $name : $name;
+ $value = Civi::settings()->get($settingName);
if (!is_null($value)) {
$settings[$name] = $value;
}
}
// Decrypt API key before returning
- $settings['sparkpost_apiKey'] = CRM_Utils_Crypt::decrypt($settings['sparkpost_apiKey']);
+ if (!empty($settings['sparkpost_apiKey'])) {
+ $settings['sparkpost_apiKey'] = CRM_Utils_Crypt::decrypt($settings['sparkpost_apiKey']);
+ }
// And finaly returm what was asked for ...
if (!empty($setting)) {
return CRM_Utils_Array::value($setting, $settings);
@@ -65,18 +82,19 @@
* @param $path Method path
* @param $params Method parameters (translated as GET arguments)
* @param $content Method content (translated as POST arguments)
+ * @param $client2 bool are we sending for doug client2 or not.
*
* @see https://developers.sparkpost.com/api/
*/
- static function call($path, $params = array(), $content = array()) {
+ static function call($path, $params = array(), $content = array(), $client2 = FALSE) {
// Get the API key from the settings
- $authorization = CRM_Sparkpost::getSetting('sparkpost_apiKey');
+ $authorization = CRM_Sparkpost::getSetting('sparkpost_apiKey', $client2);
if (empty($authorization)) {
throw new Exception('No API key defined for SparkPost');
}
// Deal with the campaign setting
- if (($path =='transmissions') && ($campaign = CRM_Sparkpost::getSetting('sparkpost_campaign'))) {
+ if (($path =='transmissions') && ($campaign = CRM_Sparkpost::getSetting('sparkpost_campaign', $client2))) {
$content['campaign_id'] = $campaign;
}
@@ -159,4 +177,4 @@
// Return (valid) response
return $response;
}
-}
\ No newline at end of file
+}
diff -ruN com.cividesk.email.sparkpost/Mail/Sparkpost.php /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/Mail/Sparkpost.php
--- com.cividesk.email.sparkpost/Mail/Sparkpost.php 2018-11-27 04:35:53.000000000 -0500
+++ /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/Mail/Sparkpost.php 2020-05-19 02:56:56.703298688 -0400
@@ -44,7 +44,7 @@
/**
* Send an email
*/
- function send($recipients, $headers, $body) {
if (defined('CIVICRM_MAIL_LOG')) {
CRM_Utils_Mail::logger($recipients, $headers, $body);
if(!defined('CIVICRM_MAIL_LOG_AND SEND')) {
@@ -54,7 +54,7 @@
// Has the SparkPost service failed before in this mailing?
if (Mail_Sparkpost::$unavailable) {
- if (CRM_Sparkpost::getSetting('sparkpost_useBackupMailer') && $this->backupMailer) {
+ if (CRM_Sparkpost::getSetting('sparkpost_useBackupMailer', $isclient2) && $this->backupMailer) {
return $this->backupMailer->send($recipients, $headers, $body);
}
return new PEAR_Error("The SparkPost service is unavailable due to a sending error, and the backup mailer is not enabled or not configured.");
@@ -70,7 +70,11 @@
return $headerElements;
}
list($from, $textHeaders) = $headerElements;
-
+ // if the From address has client2 in it use the client2 settings.
+ $client2 = FALSE;
+ if (strpos($from, 'client2') !== FALSE) {
+ $client2 = TRUE;
+ }
// Default options: do not track opens and clicks as CiviCRM does it
$request_body = array(
'options' => array(
@@ -79,7 +83,7 @@
)
);
// Should we send via a dedicated IP pool?
- $ip_pool = CRM_Sparkpost::getSetting('sparkpost_ipPool');
+ $ip_pool = CRM_Sparkpost::getSetting('sparkpost_ipPool', $client2);
if (!empty($ip_pool)) {
$request_body['options']['ip_pool'] = $ip_pool;
}
@@ -101,12 +105,12 @@
);
try {
- $result = CRM_Sparkpost::call('transmissions', array(), $request_body);
+ $result = CRM_Sparkpost::call('transmissions', array(), $request_body, $client2);
} catch (Exception $e) {
if ($e->getCode() == CRM_Sparkpost::FALLBACK) {
// Let's redirect this and all further sends to the backup mailer
Mail_Sparkpost::$unavailable = true;
- return $this->send($recipients, $headers, $body);
+ return $this->send($recipients, $headers, $body, $client2);
}
return new PEAR_Error($e->getMessage());
}
diff -ruN com.cividesk.email.sparkpost/settings/Sparkpost.setting.php /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/settings/Sparkpost.setting.php
--- com.cividesk.email.sparkpost/settings/Sparkpost.setting.php 2018-11-27 04:35:53.000000000 -0500
+++ /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/settings/Sparkpost.setting.php 2020-05-19 02:44:15.040536455 -0400
@@ -43,4 +43,43 @@
'description' => 'Use backup mailer?',
'help_text' => 'The backup mailer will be used if Sparkpost cannot send emails (unverified sending domain, sending limits exceeded, ...).',
),
+ 'client2_sparkpost_apiKey' => array(
+ 'group_name' => CRM_Sparkpost::SPARKPOST_EXTENSION_SETTINGS,
+ 'group' => 'com.cividesk.email.sparkpost',
+ 'name' => 'sparkpost_apiKey',
+ 'type' => 'String',
+ 'html_type' => 'password',
+ 'default' => null,
+ 'add' => '4.4',
+ 'is_domain' => 1,
+ 'is_contact' => 0,
+ 'description' => 'SparkPost REST API key',
+ 'help_text' => 'You can create API keys at: https://app.sparkpost.com/account/credentials',
+ ),
+ 'client2_sparkpost_customCallbackUrl' => array(
+ 'group_name' => CRM_Sparkpost::SPARKPOST_EXTENSION_SETTINGS,
+ 'group' => 'com.cividesk.email.sparkpost',
+ 'name' => 'sparkpost_customCallbackUrl',
+ 'type' => 'String',
+ 'html_type' => 'text',
+ 'default' => null,
+ 'add' => '4.4',
+ 'is_domain' => 1,
+ 'is_contact' => 0,
+ 'description' => 'A custom callback URL. Useful if your site is behind a proxy (like CiviProxy)',
+ 'help_text' => 'A custom callback URL is useful when your site is behind a proxy (like CiviProxy)',
+ ),
+ 'client2_sparkpost_useBackupMailer' => array(
+ 'group_name' => CRM_Sparkpost::SPARKPOST_EXTENSION_SETTINGS,
+ 'group' => 'com.cividesk.email.sparkpost',
+ 'name' => 'sparkpost_useBackupMailer',
+ 'type' => 'Boolean',
+ 'html_type' => 'radio',
+ 'default' => FALSE,
+ 'add' => '4.4',
+ 'is_domain' => 1,
+ 'is_contact' => 0,
+ 'description' => 'Use backup mailer?',
+ 'help_text' => 'The backup mailer will be used if Sparkpost cannot send emails (unverified sending domain, sending limits exceeded, ...).',
+ ),
);
diff -ruN com.cividesk.email.sparkpost/sparkpost.php /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/sparkpost.php
--- com.cividesk.email.sparkpost/sparkpost.php 2018-11-27 04:35:53.000000000 -0500
+++ /home/client1.jmaconsulting.biz/htdocs/sites/all/extensions/com.cividesk.email.sparkpost/sparkpost.php 2020-05-20 03:55:58.375488442 -0400
@@ -237,4 +237,11 @@
CRM_Core_Session::setStatus($e->getMessage(), "Sparkpost error", 'error');
}
}
-}
\ No newline at end of file
+}
+
+function sparkpost_civicrm_alterMailParams(&$params, $context) {
+ if (stripos($params['From'], 'client2')) {
+ $params['html'] = str_replace('client1.jmaconsulting.biz', 'client2.jmaconsulting.biz', $params['html']);
+ $params['text'] = str_replace('client1.jmaconsulting.biz', 'client2.jmaconsulting.biz', $params['text']);
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment