Created
May 26, 2021 06:59
-
-
Save hugopeek/ab8d347776d18522aaeb66da1565ba98 to your computer and use it in GitHub Desktop.
MODX - CampaignMonitor hook for FormIt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Clone https://github.com/campaignmonitor/createsend-php into packages/campaignmonitor | |
require_once MODX_BASE_PATH . 'packages/campaignmonitor/csrest_subscribers.php'; | |
//require_once MODX_BASE_PATH . 'packages/campaignmonitor/csrest_general.php'; | |
// API key for CampaignMonitor | |
$auth = array('api_key' => 'YOUR_API_KEY'); | |
// Initialize subscribers | |
$wrap = new CS_REST_Subscribers('YOUR_CLIENT_ID', $auth); | |
// Define prefix | |
$formID = $modx->resource->get('id'); | |
$prefix = 'fb' . $formID . '-'; | |
// Collect input from form fields | |
$name = $hook->getValue($prefix . 'name'); | |
$email = $hook->getValue($prefix . 'email'); | |
$duration = $hook->getValue($prefix . 'duration'); | |
$quantity = $hook->getValue($prefix . 'quantity'); | |
// Format values for CM | |
$duration = str_replace(',','.',$duration); // CM uses . to separate decimals | |
$quantity = number_format($quantity,0,'.',''); | |
// Check if email address is not subscribed already | |
$result = $wrap->get($email, true); | |
//$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] Name: ' . $name); | |
//$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] Email: ' . $email); | |
//$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] Duration: ' . $duration); | |
//$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] Quantity: ' . $quantity); | |
//return true; | |
// Add as subscriber | |
if (!$result->was_successful()) { | |
$addSubscriber = $wrap->add(array( | |
'Name' => $name, | |
'EmailAddress' => $email, | |
'CustomFields' => array( | |
array( | |
'Key' => 'duration', | |
'Value' => $duration | |
), | |
array( | |
'Key' => 'quantity', | |
'Value' => $quantity | |
) | |
), | |
'ConsentToTrack' => 'yes', | |
'Resubscribe' => true | |
)); | |
// Send email to admin on failure | |
if (!$addSubscriber->was_successful()) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] Error adding subscriber: ' . $email); | |
//$modx->log(modX::LOG_LEVEL_ERROR, '[CampaignMonitor] ' . var_dump($result->response)); | |
$message = "This email address could not be added: ${email}"; | |
//$message .= "Error dump: " . var_dump($result->response); | |
$modx->getService('mail', 'mail.modPHPMailer'); | |
$modx->mail->set(modMail::MAIL_BODY,$message); | |
$modx->mail->set(modMail::MAIL_FROM,'mail@YOURWEBSITE.com'); | |
$modx->mail->set(modMail::MAIL_FROM_NAME,'YOUR WEBSITE'); | |
$modx->mail->set(modMail::MAIL_SUBJECT,'[CampaignMonitor] Error adding subscriber'); | |
$modx->mail->address('to','hugo@websitelikethis.com'); | |
$modx->mail->address('reply-to','hugo@websitelikethis.com'); | |
$modx->mail->setHTML(true); | |
if (!$modx->mail->send()) { | |
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo); | |
} | |
$modx->mail->reset(); | |
return false; | |
} | |
} | |
return true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment