Example #1
0
 public function testHookAlterMailer()
 {
     $test = $this;
     $mockMailer = new CRM_Utils_FakeObject(array('send' => function ($recipients, $headers, $body) use($test) {
         $test->calls['send']++;
         $test->assertEquals(array('*****@*****.**'), $recipients);
         $test->assertEquals('Subject Example', $headers['Subject']);
     }));
     CRM_Utils_Hook::singleton()->setHook('civicrm_alterMailer', function (&$mailer, $driver, $params) use($test, $mockMailer) {
         $test->calls['civicrm_alterMailer']++;
         $test->assertTrue(is_string($driver) && !empty($driver));
         $test->assertTrue(is_array($params));
         $test->assertTrue(is_callable(array($mailer, 'send')));
         $mailer = $mockMailer;
     });
     $params = array();
     $params['groupName'] = 'CRM_Core_Config_MailerTest';
     $params['from'] = 'From Example <*****@*****.**>';
     $params['toName'] = 'To Example';
     $params['toEmail'] = '*****@*****.**';
     $params['subject'] = 'Subject Example';
     $params['text'] = 'Example text';
     $params['html'] = '<p>Example HTML</p>';
     CRM_Utils_Mail::send($params);
     $this->assertEquals(1, $this->calls['civicrm_alterMailer']);
     $this->assertEquals(1, $this->calls['send']);
     // once more, just to make sure the hooks are called right #times
     CRM_Utils_Mail::send($params);
     CRM_Utils_Mail::send($params);
     $this->assertEquals(1, $this->calls['civicrm_alterMailer']);
     $this->assertEquals(3, $this->calls['send']);
 }
Example #2
0
 /**
  * test case for add( )
  * test with empty params.
  */
 function testFormatRFC822()
 {
     $values = array(array('name' => "Test User", 'email' => "*****@*****.**", 'result' => "Test User <*****@*****.**>"), array('name' => '"Test User"', 'email' => "*****@*****.**", 'result' => "Test User <*****@*****.**>"), array('name' => "User, Test", 'email' => "*****@*****.**", 'result' => '"User, Test" <*****@*****.**>'), array('name' => '"User, Test"', 'email' => "*****@*****.**", 'result' => '"User, Test" <*****@*****.**>'), array('name' => '"Test User"', 'email' => "*****@*****.**", 'result' => '"Test User" <*****@*****.**>', 'useQuote' => TRUE), array('name' => "User, Test", 'email' => "*****@*****.**", 'result' => '"User, Test" <*****@*****.**>', 'useQuote' => TRUE));
     foreach ($values as $value) {
         $result = CRM_Utils_Mail::formatRFC822Email($value['name'], $value['email'], CRM_Utils_Array::value('useQuote', $value, FALSE));
         $this->assertEquals($result, $value['result'], 'Expected encoding does not match');
     }
 }
Example #3
0
 static function sendMailToParents($childID, $subjectTPL, $messageTPL, $templateVars, $additionalCC = null)
 {
     require_once 'SFS/Utils/Relationship.php';
     $parentInfo = array();
     SFS_Utils_Relationship::getParents($childID, $parentInfo, false);
     // make sure we unset the older parents
     for ($count = 1; $count < 5; $count++) {
         $templateVars["parent_{$count}_Name"] = null;
     }
     $count = 1;
     $toDisplayName = $toEmail = $cc = null;
     foreach ($parentInfo as $parent) {
         $templateVars["parent_{$count}_Name"] = $parent['name'];
         if ($parent['email']) {
             if (!$toEmail) {
                 $toDisplayName = $parent['name'];
                 $toEmail = $parent['email'];
             } else {
                 if (!empty($cc)) {
                     $cc .= ", ";
                 }
                 $cc .= $parent['email'];
             }
         }
         $count++;
     }
     if ($additionalCC) {
         if (!empty($cc)) {
             $cc .= ", ";
         }
         $cc .= $additionalCC;
     }
     // return if we dont have a toEmail
     if (!$toEmail) {
         return;
     }
     require_once 'SFS/Utils/Query.php';
     list($templateVars['childName'], $templateVars['childEmail']) = SFS_Utils_Query::getNameAndEmail($childID);
     $template = CRM_Core_Smarty::singleton();
     $template->assign($templateVars);
     require_once 'CRM/Utils/Mail.php';
     require_once 'CRM/Utils/String.php';
     $params = array('from' => self::SFS_FROM_EMAIL, 'toName' => $toDisplayName, 'toEmail' => $toEmail, 'subject' => $template->fetch($subjectTPL), 'text' => $template->fetch($messageTPL), 'cc' => $cc, 'bcc' => self::SFS_BCC_EMAIL);
     CRM_Utils_Mail::send($params);
 }
 /**
  * @param $self
  */
 public static function commonBuildQuickForm($self)
 {
     $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $self);
     if (!$contactId) {
         $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, NULL, $_REQUEST);
     }
     $urlParams = "action=add&reset=1&cid={$contactId}&selectedChild=activity&atype=";
     $activityTypes = $urls = array();
     $emailTypeId = CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name');
     $letterTypeId = CRM_Core_OptionGroup::getValue('activity_type', 'Print PDF Letter', 'name');
     $SMSId = CRM_Core_OptionGroup::getValue('activity_type', 'Text Message (SMS)', 'label');
     if (CRM_Utils_Mail::validOutBoundMail() && $contactId) {
         list($name, $email, $doNotEmail, $onHold, $isDeseased) = CRM_Contact_BAO_Contact::getContactDetails($contactId);
         if (!$doNotEmail && $email && !$isDeseased) {
             $activityTypes = array($emailTypeId => ts('Send an Email'));
         }
     }
     if ($contactId && CRM_SMS_BAO_Provider::activeProviderCount()) {
         // Check for existence of a mobile phone and ! do not SMS privacy setting
         $mobileTypeID = CRM_Core_OptionGroup::getValue('phone_type', 'Mobile', 'name');
         list($name, $phone, $doNotSMS) = CRM_Contact_BAO_Contact_Location::getPhoneDetails($contactId, $mobileTypeID);
         if (!$doNotSMS && $phone) {
             $sendSMS = array($SMSId => ts('Send SMS'));
             $activityTypes += $sendSMS;
         }
     }
     // this returns activity types sorted by weight
     $otherTypes = CRM_Core_PseudoConstant::activityType(FALSE);
     $activityTypes += $otherTypes;
     foreach (array_keys($activityTypes) as $typeId) {
         if ($typeId == $emailTypeId) {
             $urls[$typeId] = CRM_Utils_System::url('civicrm/activity/email/add', "{$urlParams}{$typeId}", FALSE, NULL, FALSE);
         } elseif ($typeId == $SMSId) {
             $urls[$typeId] = CRM_Utils_System::url('civicrm/activity/sms/add', "{$urlParams}{$typeId}", FALSE, NULL, FALSE);
         } elseif ($typeId == $letterTypeId) {
             $urls[$typeId] = CRM_Utils_System::url('civicrm/activity/pdf/add', "{$urlParams}{$typeId}", FALSE, NULL, FALSE);
         } else {
             $urls[$typeId] = CRM_Utils_System::url('civicrm/activity/add', "{$urlParams}{$typeId}", FALSE, NULL, FALSE);
         }
     }
     $self->assign('activityTypes', $activityTypes);
     $self->assign('urls', $urls);
     $self->assign('suppressForm', TRUE);
 }
Example #5
0
 /**
  * Basic send.
  */
 public function testSend()
 {
     $contact_params_1 = array('first_name' => substr(sha1(rand()), 0, 7), 'last_name' => 'Anderson', 'email' => substr(sha1(rand()), 0, 7) . '@example.org', 'contact_type' => 'Individual');
     $contact_id_1 = $this->individualCreate($contact_params_1);
     $contact_params_2 = array('first_name' => substr(sha1(rand()), 0, 7), 'last_name' => 'Xylophone', 'email' => substr(sha1(rand()), 0, 7) . '@example.org', 'contact_type' => 'Individual');
     $contact_id_2 = $this->individualCreate($contact_params_2);
     $subject = 'Test spool';
     $params = array('from' => CRM_Utils_Mail::formatRFC822Email($contact_params_1['first_name'] . " " . $contact_params_1['last_name'], $contact_params_1['email']), 'toName' => $contact_params_2['first_name'] . " " . $contact_params_2['last_name'], 'toEmail' => $contact_params_2['email'], 'subject' => $subject, 'text' => self::$bodytext, 'html' => "<p>\n" . self::$bodytext . '</p>');
     CRM_Utils_Mail::send($params);
     $mail = $this->_mut->getMostRecentEmail('raw');
     $this->assertContains("Subject: {$subject}", $mail);
     $this->assertContains(self::$bodytext, $mail);
     $mail = $this->_mut->getMostRecentEmail('ezc');
     $this->assertEquals($subject, $mail->subject);
     $this->assertContains($contact_params_1['email'], $mail->from->email, 'From address incorrect.');
     $this->assertContains($contact_params_2['email'], $mail->to[0]->email, 'Recipient incorrect.');
     $context = new ezcMailPartWalkContext(array(get_class($this), 'mailWalkCallback'));
     $mail->walkParts($context, $mail);
 }
 /**
  * Take the signature form and send an email to the recipient.
  *
  * @param CRM_Campaign_Form_Petition_Signature $form
  *   The petition form.
  */
 public function processSignature($form)
 {
     // Get the message.
     $messageField = $this->findMessageField();
     if ($messageField === FALSE) {
         return;
     }
     $message = empty($form->_submitValues[$messageField]) ? $this->petitionEmailVal[$this->fields['Default_Message']] : $form->_submitValues[$messageField];
     // If message is left empty and no default message, don't send anything.
     if (empty($message)) {
         return;
     }
     // Setup email message:
     $mailParams = array('groupName' => 'Activity Email Sender', 'from' => $this->getSenderLine($form->_contactId), 'toName' => $this->petitionEmailVal[$this->fields['Recipient_Name']], 'toEmail' => $this->petitionEmailVal[$this->fields['Recipient_Email']], 'subject' => $this->petitionEmailVal[$this->fields['Subject']], 'text' => $message);
     if (!CRM_Utils_Mail::send($mailParams)) {
         CRM_Core_Session::setStatus(ts('Error sending message to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])));
     } else {
         CRM_Core_Session::setStatus(ts('Message sent successfully to %1', array('domain' => 'com.aghstrategies.petitionemail', 1 => $mailParams['toName'])));
     }
     parent::processSignature($form);
 }
 public function buildQuickForm()
 {
     $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
     $urlParams = "action=add&reset=1&cid={$contactId}&selectedChild=activity&atype=";
     $url = CRM_Utils_System::url('civicrm/contact/view/activity', $urlParams, false, null, false);
     $activityTypes = array();
     require_once 'CRM/Utils/Mail.php';
     if (CRM_Utils_Mail::validOutBoundMail() && $contactId) {
         require_once 'CRM/Contact/BAO/Contact.php';
         list($name, $email, $doNotEmail, $onHold, $isDeseased) = CRM_Contact_BAO_Contact::getContactDetails($contactId);
         if (!$doNotEmail && $email && !$isDeseased) {
             $activityTypes = array('3' => ts('Send an Email'));
         }
     }
     // this returns activity types sorted by weight
     $otherTypes = CRM_Core_PseudoConstant::activityType(false);
     $activityTypes += $otherTypes;
     $this->assign('activityTypes', $activityTypes);
     $this->assign('url', $url);
     $this->assign('suppressForm', true);
 }
Example #8
0
 public function buildQuickForm()
 {
     $this->applyFilter('__ALL__', 'trim');
     $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
     $urlParams = "action=add&reset=1&cid={$contactId}&selectedChild=activity&atype=";
     $url = CRM_Utils_System::url('civicrm/contact/view/activity', $urlParams, false, null, false);
     $activityType = CRM_Core_PseudoConstant::activityType(false);
     $this->assign('emailSetting', false);
     require_once 'CRM/Utils/Mail.php';
     if (CRM_Utils_Mail::validOutBoundMail() && $contactId) {
         $this->assign('emailSetting', true);
         require_once 'CRM/Contact/BAO/Contact.php';
         list($name, $email, $doNotEmail, $onHold, $isDeseased) = CRM_Contact_BAO_Contact::getContactDetails($contactId);
         if (!$doNotEmail && $email && !$isDeseased) {
             $activityType += array('3' => ts('Send an Email'));
         }
     }
     $this->applyFilter('__ALL__', 'trim');
     $this->add('select', 'other_activity', ts('Other Activities'), array('' => ts('- new activity -')) + $activityType, false, array('onchange' => "if (this.value) window.location='{$url}'+ this.value; else return false"));
     $this->assign('suppressForm', true);
 }
Example #9
0
function civicrm_api3_speakcivi_sendconfirm($params)
{
    $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                      FROM civicrm_msg_template mt
                      WHERE mt.id = %1 AND mt.is_default = 1';
    $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
    $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
    $dao->fetch();
    if (!$dao->N) {
        CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
    }
    $confirmation_block_html = '';
    $confirmation_block_text = '';
    $language = $params['language'];
    if ($params['confirmation_block']) {
        $cgid = $params['contact_id'];
        $aid = $params['activity_id'];
        $campaign_id = $params['campaign_id'];
        $hash = sha1(CIVICRM_SITE_KEY . $cgid);
        $url_confirm_and_keep = CRM_Utils_System::url('civicrm/speakcivi/confirm', "id={$cgid}&aid={$aid}&cid={$campaign_id}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign=speakout_confirm", true);
        $url_confirm_and_not_receive = CRM_Utils_System::url('civicrm/speakcivi/optout', "id={$cgid}&aid={$aid}&cid={$campaign_id}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign=speakout_optout", true);
        $template = CRM_Core_Smarty::singleton();
        $template->assign('url_confirm_and_keep', $url_confirm_and_keep);
        $template->assign('url_confirm_and_not_receive', $url_confirm_and_not_receive);
        $confirmation_block_html = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $language . '.html.tpl');
        $confirmation_block_text = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $language . '.text.tpl');
        $params['subject'] = getSubjectConfirm($language);
    } else {
        $params['subject'] = getSubjectImpact($language);
    }
    $params['html'] = str_replace("#CONFIRMATION_BLOCK", $confirmation_block_html, $dao->html);
    $params['text'] = str_replace("#CONFIRMATION_BLOCK", $confirmation_block_text, $dao->text);
    $params['format'] = $dao->format;
    $dao->free();
    $sent = CRM_Utils_Mail::send($params);
    return civicrm_api3_create_success($sent, $params);
}
 /**
  * Implements Mail::send() function using the sendmail
  * command-line binary.
  *
  * @param mixed $recipients Either a comma-seperated list of recipients
  *              (RFC822 compliant), or an array of recipients,
  *              each RFC822 valid. This may contain recipients not
  *              specified in the headers, for Bcc:, resending
  *              messages, etc.
  *
  * @param array $headers The array of headers to send with the mail, in an
  *              associative array, where the array key is the
  *              header name (ie, 'Subject'), and the array value
  *              is the header value (ie, 'test'). The header
  *              produced from those values would be 'Subject:
  *              test'.
  *
  * @param string $body The full text of the message body, including any
  *               Mime parts, etc.
  *
  * @return mixed Returns true on success, or a PEAR_Error
  *               containing a descriptive error message on
  *               failure.
  * @access public
  */
 function send($recipients, $headers, $body)
 {
     if (defined('CIVICRM_MAIL_LOG')) {
         require_once 'CRM/Utils/Mail.php';
         CRM_Utils_Mail::logger($recipients, $headers, $body);
         return true;
     }
     if (!is_array($headers)) {
         return PEAR::raiseError('$headers must be an array');
     }
     $result = $this->_sanitizeHeaders($headers);
     if (is_a($result, 'PEAR_Error')) {
         return $result;
     }
     $recipients = $this->parseRecipients($recipients);
     if (is_a($recipients, 'PEAR_Error')) {
         return $recipients;
     }
     $recipients = implode(' ', array_map('escapeshellarg', $recipients));
     $headerElements = $this->prepareHeaders($headers);
     if (is_a($headerElements, 'PEAR_Error')) {
         return $headerElements;
     }
     list($from, $text_headers) = $headerElements;
     /* Since few MTAs are going to allow this header to be forged
      * unless it's in the MAIL FROM: exchange, we'll use
      * Return-Path instead of From: if it's set. */
     if (!empty($headers['Return-Path'])) {
         $from = $headers['Return-Path'];
     }
     if (!isset($from)) {
         return PEAR::raiseError('No from address given.');
     } elseif (strpos($from, ' ') !== false || strpos($from, ';') !== false || strpos($from, '&') !== false || strpos($from, '`') !== false) {
         return PEAR::raiseError('From address specified with dangerous characters.');
     }
     $from = escapeshellarg($from);
     // Security bug #16200
     $mail = @popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f{$from} -- {$recipients}", 'w');
     if (!$mail) {
         return PEAR::raiseError('Failed to open sendmail [' . $this->sendmail_path . '] for execution.');
     }
     // Write the headers following by two newlines: one to end the headers
     // section and a second to separate the headers block from the body.
     fputs($mail, $text_headers . $this->sep . $this->sep);
     fputs($mail, $body);
     $result = pclose($mail);
     if (version_compare(phpversion(), '4.2.3') == -1) {
         // With older php versions, we need to shift the pclose
         // result to get the exit code.
         $result = $result >> 8 & 0xff;
     }
     if ($result != 0) {
         return PEAR::raiseError('sendmail returned error code ' . $result, $result);
     }
     return true;
 }
Example #11
0
 /**
  * Ask a contact for subscription confirmation (opt-in)
  *
  * @param string $email
  *   The email address.
  *
  * @return void
  */
 public function send_confirm_request($email)
 {
     $config = CRM_Core_Config::singleton();
     $domain = CRM_Core_BAO_Domain::getDomain();
     //get the default domain email address.
     list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
     $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $confirm = implode($config->verpSeparator, array($localpart . 'c', $this->contact_id, $this->id, $this->hash)) . "@{$emailDomain}";
     $group = new CRM_Contact_BAO_Group();
     $group->id = $this->group_id;
     $group->find(TRUE);
     $component = new CRM_Mailing_BAO_Component();
     $component->is_default = 1;
     $component->is_active = 1;
     $component->component_type = 'Subscribe';
     $component->find(TRUE);
     $headers = array('Subject' => $component->subject, 'From' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", 'To' => $email, 'Reply-To' => $confirm, 'Return-Path' => "do-not-reply@{$emailDomain}");
     $url = CRM_Utils_System::url('civicrm/mailing/confirm', "reset=1&cid={$this->contact_id}&sid={$this->id}&h={$this->hash}", TRUE);
     $html = $component->body_html;
     if ($component->body_text) {
         $text = $component->body_text;
     } else {
         $text = CRM_Utils_String::htmlToText($component->body_html);
     }
     $bao = new CRM_Mailing_BAO_Mailing();
     $bao->body_text = $text;
     $bao->body_html = $html;
     $tokens = $bao->getTokens();
     $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
     $html = CRM_Utils_Token::replaceSubscribeTokens($html, $group->title, $url, TRUE);
     $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
     $text = CRM_Utils_Token::replaceSubscribeTokens($text, $group->title, $url, FALSE);
     // render the &amp; entities in text mode, so that the links work
     $text = str_replace('&amp;', '&', $text);
     $message = new Mail_mime("\n");
     $message->setHTMLBody($html);
     $message->setTxtBody($text);
     $b = CRM_Utils_Mail::setMimeParams($message);
     $h = $message->headers($headers);
     CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 's', $this->contact_id, $this->id, $this->hash);
     $mailer = \Civi::service('pear_mail');
     if (is_object($mailer)) {
         $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         $mailer->send($email, $h, $b);
         unset($errorScope);
     }
 }
Example #12
0
function civicrm_api3_speakcivi_sendconfirm($params)
{
    $confirmationBlock = $params['confirmation_block'];
    $contactId = $params['contact_id'];
    $campaignId = $params['campaign_id'];
    $activityId = $params['activity_id'];
    $campaignObj = new CRM_Speakcivi_Logic_Campaign($campaignId);
    $locale = $campaignObj->getLanguage();
    $params['from'] = $campaignObj->getSenderMail();
    $params['format'] = null;
    if ($confirmationBlock) {
        $params['subject'] = $campaignObj->getSubjectNew();
        $message = $campaignObj->getMessageNew();
    } else {
        $params['subject'] = $campaignObj->getSubjectCurrent();
        $message = $campaignObj->getMessageCurrent();
    }
    if (!$message) {
        if ($confirmationBlock) {
            $message = CRM_Speakcivi_Tools_Dictionary::getMessageNew($locale);
            $campaignObj->setCustomFieldBySQL($campaignId, $campaignObj->fieldMessageNew, $message);
        } else {
            $message = CRM_Speakcivi_Tools_Dictionary::getMessageCurrent($locale);
            $campaignObj->setCustomFieldBySQL($campaignId, $campaignObj->fieldMessageCurrent, $message);
        }
    }
    $contact = array();
    $params_contact = array('id' => $contactId, 'sequential' => 1);
    $result = civicrm_api3('Contact', 'get', $params_contact);
    if ($result['count'] == 1) {
        $contact = $result['values'][0];
    }
    /* CONFIRMATION_BLOCK */
    $hash = sha1(CIVICRM_SITE_KEY . $contactId);
    $utm_content = 'version_' . $contactId % 2;
    $utm_campaign = $campaignObj->getUtmCampaign();
    $url_confirm_and_keep = CRM_Utils_System::url('civicrm/speakcivi/confirm', "id={$contactId}&aid={$activityId}&cid={$campaignId}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign={$utm_campaign}&utm_content={$utm_content}", true);
    $url_confirm_and_not_receive = CRM_Utils_System::url('civicrm/speakcivi/optout', "id={$contactId}&aid={$activityId}&cid={$campaignId}&hash={$hash}&utm_source=civicrm&utm_medium=email&utm_campaign={$utm_campaign}&utm_content={$utm_content}", true);
    $template = CRM_Core_Smarty::singleton();
    $template->assign('url_confirm_and_keep', $url_confirm_and_keep);
    $template->assign('url_confirm_and_not_receive', $url_confirm_and_not_receive);
    $locales = getLocale($locale);
    $confirmation_block_html = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['html'] . '.html.tpl');
    $confirmation_block_text = $template->fetch('../templates/CRM/Speakcivi/Page/ConfirmationBlock.' . $locales['text'] . '.text.tpl');
    /* SHARING_BLOCK */
    $template->clearTemplateVars();
    $template->assign('url_campaign', $campaignObj->getUrlCampaign());
    $template->assign('url_campaign_fb', prepareCleanUrl($campaignObj->getUrlCampaign()));
    $template->assign('utm_campaign', $campaignObj->getUtmCampaign());
    $template->assign('share_facebook', CRM_Speakcivi_Tools_Dictionary::getShareFacebook($locale));
    $template->assign('share_twitter', CRM_Speakcivi_Tools_Dictionary::getShareTwitter($locale));
    $template->assign('twitter_share_text', urlencode($campaignObj->getTwitterShareText()));
    $sharing_block_html = $template->fetch('../templates/CRM/Speakcivi/Page/SharingBlock.html.tpl');
    $template->clearTemplateVars();
    $template->assign('contact', $contact);
    $message = $template->fetch('string:' . $message);
    $message_html = str_replace("#CONFIRMATION_BLOCK", html_entity_decode($confirmation_block_html), $message);
    $message_text = str_replace("#CONFIRMATION_BLOCK", html_entity_decode($confirmation_block_text), $message);
    $message_html = str_replace("#SHARING_BLOCK", html_entity_decode($sharing_block_html), $message_html);
    $message_text = str_replace("#SHARING_BLOCK", html_entity_decode($sharing_block_html), $message_text);
    $params['html'] = $message_html;
    $params['text'] = convertHtmlToText($message_text);
    $sent = CRM_Utils_Mail::send($params);
    return civicrm_api3_create_success($sent, $params);
}
Example #13
0
 /**
  * Send pdf by email.
  *
  * @param array $contact
  * @param string $html
  *
  * @param $is_pdf
  * @param array $format
  * @param array $params
  *
  * @return bool
  */
 public static function emailLetter($contact, $html, $is_pdf, $format = array(), $params = array())
 {
     try {
         if (empty($contact['email'])) {
             return FALSE;
         }
         $mustBeEmpty = array('do_not_email', 'is_deceased', 'on_hold');
         foreach ($mustBeEmpty as $emptyField) {
             if (!empty($contact[$emptyField])) {
                 return FALSE;
             }
         }
         $defaults = array('toName' => $contact['display_name'], 'toEmail' => $contact['email'], 'text' => '', 'html' => $html);
         if (empty($params['from'])) {
             $emails = CRM_Core_BAO_Email::getFromEmail();
             $emails = array_keys($emails);
             $defaults['from'] = array_pop($emails);
         }
         if (!empty($params['subject'])) {
             $defaults['subject'] = $params['subject'];
         } else {
             $defaults['subject'] = ts('Thank you for your contribution/s');
         }
         if ($is_pdf) {
             $defaults['html'] = ts('Please see attached');
             $defaults['attachments'] = array(CRM_Utils_Mail::appendPDF('ThankYou.pdf', $html, $format));
         }
         $params = array_merge($defaults);
         return CRM_Utils_Mail::send($params);
     } catch (CRM_Core_Exception $e) {
         return FALSE;
     }
 }
Example #14
0
 /**
  * Global form rule.
  *
  * @param array $fields
  *   The input form values.
  * @param array $files
  *   The uploaded files if any.
  * @param array $self
  *   Current form object.
  *
  * @return array
  *   array of errors / empty array.
  */
 public static function formRule($fields, $files, $self)
 {
     $errors = array();
     if ($self->_gName == 'case_status' && empty($fields['grouping'])) {
         $errors['grouping'] = ts('Status class is a required field');
     }
     if (in_array($self->_gName, array('email_greeting', 'postal_greeting', 'addressee')) && empty($self->_defaultValues['is_reserved'])) {
         $label = $fields['label'];
         $condition = " AND v.label = '{$label}' ";
         $values = CRM_Core_OptionGroup::values($self->_gName, FALSE, FALSE, FALSE, $condition, 'filter');
         $checkContactOptions = TRUE;
         if ($self->_id && $self->_defaultValues['contactOptions'] == $fields['contactOptions']) {
             $checkContactOptions = FALSE;
         }
         if ($checkContactOptions && in_array($fields['contactOptions'], $values)) {
             $errors['label'] = ts('This Label already exists in the database for the selected contact type.');
         }
     }
     if ($self->_gName == 'from_email_address') {
         $formEmail = CRM_Utils_Mail::pluckEmailFromHeader($fields['label']);
         if (!CRM_Utils_Rule::email($formEmail)) {
             $errors['label'] = ts('Please enter a valid email address.');
         }
         $formName = explode('"', $fields['label']);
         if (empty($formName[1]) || count($formName) != 3) {
             $errors['label'] = ts('Please follow the proper format for From Email Address');
         }
     }
     $dataType = self::getOptionGroupDataType($self->_gName);
     if ($dataType && $self->_gName !== 'activity_type') {
         $validate = CRM_Utils_Type::validate($fields['value'], $dataType, FALSE);
         if (!$validate) {
             CRM_Core_Session::setStatus(ts('Data Type of the value field for this option value does not match ' . $dataType), ts('Value field Data Type mismatch'));
         }
     }
     return $errors;
 }
Example #15
0
 /**
  * Confirm a pending subscription
  *
  * @param int $contact_id       The id of the contact
  * @param int $subscribe_id     The id of the subscription event
  * @param string $hash          The hash
  * @return boolean              True on success
  * @access public
  * @static
  */
 public static function confirm($contact_id, $subscribe_id, $hash)
 {
     require_once 'CRM/Mailing/Event/BAO/Subscribe.php';
     $se =& CRM_Mailing_Event_BAO_Subscribe::verify($contact_id, $subscribe_id, $hash);
     if (!$se) {
         return false;
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $ce =& new CRM_Mailing_Event_BAO_Confirm();
     $ce->event_subscribe_id = $se->id;
     $ce->time_stamp = date('YmdHis');
     $ce->save();
     require_once 'CRM/Contact/BAO/GroupContact.php';
     CRM_Contact_BAO_GroupContact::updateGroupMembershipStatus($contact_id, $se->group_id, 'Email', $ce->id);
     $transaction->commit();
     $config =& CRM_Core_Config::singleton();
     require_once 'CRM/Core/BAO/Domain.php';
     $domain =& CRM_Core_BAO_Domain::getDomain();
     list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail();
     require_once 'CRM/Contact/BAO/Contact/Location.php';
     list($display_name, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id);
     require_once 'CRM/Contact/DAO/Group.php';
     $group =& new CRM_Contact_DAO_Group();
     $group->id = $se->group_id;
     $group->find(true);
     require_once 'CRM/Mailing/BAO/Component.php';
     $component =& new CRM_Mailing_BAO_Component();
     $component->is_default = 1;
     $component->is_active = 1;
     $component->component_type = 'Welcome';
     $component->find(true);
     require_once 'CRM/Core/BAO/MailSettings.php';
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $headers = array('Subject' => $component->subject, 'From' => "\"{$domainEmailName}\" <do-not-reply@{$emailDomain}>", 'To' => $email, 'Reply-To' => "do-not-reply@{$emailDomain}", 'Return-Path' => "do-not-reply@{$emailDomain}");
     $html = $component->body_html;
     if ($component->body_text) {
         $text = $component->body_text;
     } else {
         $text = CRM_Utils_String::htmlToText($component->body_html);
     }
     require_once 'CRM/Mailing/BAO/Mailing.php';
     $bao =& new CRM_Mailing_BAO_Mailing();
     $bao->body_text = $text;
     $bao->body_html = $html;
     $tokens = $bao->getTokens();
     require_once 'CRM/Utils/Token.php';
     $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, true, $tokens['html']);
     $html = CRM_Utils_Token::replaceWelcomeTokens($html, $group->title, true);
     $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, false, $tokens['text']);
     $text = CRM_Utils_Token::replaceWelcomeTokens($text, $group->title, false);
     // we need to wrap Mail_mime because PEAR is apparently unable to fix
     // a six-year-old bug (PEAR bug #30) in Mail_mime::_encodeHeaders()
     // this fixes CRM-5466
     require_once 'CRM/Utils/Mail/FixedMailMIME.php';
     $message =& new CRM_Utils_Mail_FixedMailMIME("\n");
     $message->setHTMLBody($html);
     $message->setTxtBody($text);
     $b =& CRM_Utils_Mail::setMimeParams($message);
     $h =& $message->headers($headers);
     $mailer =& $config->getMailer();
     require_once 'CRM/Mailing/BAO/Mailing.php';
     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Core_Error', 'nullHandler'));
     if (is_object($mailer)) {
         $mailer->send($email, $h, $b);
         CRM_Core_Error::setCallback();
     }
     return $group->title;
 }
Example #16
0
 function setDefaultValues()
 {
     require_once 'CRM/Core/BAO/Domain.php';
     $defaults = array();
     $params = array();
     $locParams = array();
     if (isset($this->_id)) {
         $params['id'] = $this->_id;
         CRM_Core_BAO_Domain::retrieve($params, $domainDefaults);
         //get the default domain from email address. fix CRM-3552
         require_once 'CRM/Utils/Mail.php';
         require_once 'CRM/Core/OptionValue.php';
         $optionValues = array();
         $grpParams['name'] = 'from_email_address';
         CRM_Core_OptionValue::getValues($grpParams, $optionValues);
         foreach ($optionValues as $Id => $value) {
             if ($value['is_default'] && $value['is_active']) {
                 $this->_fromEmailId = $Id;
                 $domainDefaults['email_name'] = CRM_Utils_Array::value(1, explode('"', $value['label']));
                 $domainDefaults['email_address'] = CRM_Utils_Mail::pluckEmailFromHeader($value['label']);
                 break;
             }
         }
         unset($params['id']);
         $locParams = $params + array('entity_id' => $this->_id, 'entity_table' => 'civicrm_domain');
         require_once 'CRM/Core/BAO/Location.php';
         $defaults = CRM_Core_BAO_Location::getValues($locParams);
         $config = CRM_Core_Config::singleton();
         if (!isset($defaults['address'][1]['country_id'])) {
             $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
         }
         if (!empty($defaults['address'])) {
             foreach ($defaults['address'] as $key => $value) {
                 CRM_Contact_Form_Edit_Address::fixStateSelect($this, "address[{$key}][country_id]", "address[{$key}][state_province_id]", CRM_Utils_Array::value('country_id', $value, $config->defaultContactCountry));
             }
         }
     }
     $defaults = array_merge($defaults, $domainDefaults);
     return $defaults;
 }
Example #17
0
 /**
  * Send the message to a specific contact.
  *
  * @param string $from
  *   The name and email of the sender.
  * @param int $fromID
  * @param int $toID
  *   The contact id of the recipient.
  * @param string $subject
  *   The subject of the message.
  * @param $text_message
  * @param $html_message
  * @param string $emailAddress
  *   Use this 'to' email address instead of the default Primary address.
  * @param int $activityID
  *   The activity ID that tracks the message.
  * @param null $attachments
  * @param null $cc
  * @param null $bcc
  *
  * @return bool
  *   TRUE if successful else FALSE.
  */
 public static function sendMessage($from, $fromID, $toID, &$subject, &$text_message, &$html_message, $emailAddress, $activityID, $attachments = NULL, $cc = NULL, $bcc = NULL)
 {
     list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($toID);
     if ($emailAddress) {
         $toEmail = trim($emailAddress);
     }
     // make sure both email addresses are valid
     // and that the recipient wants to receive email
     if (empty($toEmail) or $toDoNotEmail) {
         return FALSE;
     }
     if (!trim($toDisplayName)) {
         $toDisplayName = $toEmail;
     }
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
     // create the params array
     $mailParams = array('groupName' => 'Activity Email Sender', 'from' => $from, 'toName' => $toDisplayName, 'toEmail' => $toEmail, 'subject' => $subject, 'cc' => $cc, 'bcc' => $bcc, 'text' => $text_message, 'html' => $html_message, 'attachments' => $attachments);
     if (!CRM_Utils_Mail::send($mailParams)) {
         return FALSE;
     }
     // add activity target record for every mail that is send
     $activityTargetParams = array('activity_id' => $activityID, 'contact_id' => $toID, 'record_type_id' => $targetID);
     CRM_Activity_BAO_ActivityContact::create($activityTargetParams);
     return TRUE;
 }
 /**
  * @param $contactId
  * @param $to
  * @param $scheduleID
  * @param $from
  * @param $tokenParams
  *
  * @return bool|null
  * @throws CRM_Core_Exception
  */
 static function sendReminder($contactId, $to, $scheduleID, $from, $tokenParams)
 {
     $email = $to['email'];
     $phoneNumber = $to['phone'];
     $schedule = new CRM_Core_DAO_ActionSchedule();
     $schedule->id = $scheduleID;
     $domain = CRM_Core_BAO_Domain::getDomain();
     $result = NULL;
     $hookTokens = array();
     if ($schedule->find(TRUE)) {
         $body_text = $schedule->body_text;
         $body_html = $schedule->body_html;
         $sms_body_text = $schedule->sms_body_text;
         $body_subject = $schedule->subject;
         if (!$body_text) {
             $body_text = CRM_Utils_String::htmlToText($body_html);
         }
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             return NULL;
         }
         // merge activity tokens with contact array
         $contact = array_merge($contact, $tokenParams);
         //CRM-5734
         CRM_Utils_Hook::tokenValues($contact, $contactId);
         CRM_Utils_Hook::tokens($hookTokens);
         $categories = array_keys($hookTokens);
         $type = array('body_html' => 'html', 'body_text' => 'text', 'sms_body_text' => 'text');
         foreach ($type as $bodyType => $value) {
             $dummy_mail = new CRM_Mailing_BAO_Mailing();
             if ($bodyType == 'sms_body_text') {
                 $dummy_mail->body_text = ${$bodyType};
             } else {
                 $dummy_mail->{${$bodyType}} = ${$bodyType};
             }
             $tokens = $dummy_mail->getTokens();
             if (${$bodyType}) {
                 CRM_Utils_Token::replaceGreetingTokens(${$bodyType}, NULL, $contact['contact_id']);
                 ${$bodyType} = CRM_Utils_Token::replaceDomainTokens(${$bodyType}, $domain, TRUE, $tokens[$value], TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceContactTokens(${$bodyType}, $contact, FALSE, $tokens[$value], FALSE, TRUE);
                 ${$bodyType} = CRM_Utils_Token::replaceComponentTokens(${$bodyType}, $contact, $tokens[$value], TRUE, FALSE);
                 ${$bodyType} = CRM_Utils_Token::replaceHookTokens(${$bodyType}, $contact, $categories, TRUE);
             }
         }
         $html = $body_html;
         $text = $body_text;
         $sms_text = $sms_body_text;
         $smarty = CRM_Core_Smarty::singleton();
         foreach (array('text', 'html', 'sms_text') as $elem) {
             ${$elem} = $smarty->fetch("string:{${$elem}}");
         }
         $matches = array();
         preg_match_all('/(?<!\\{|\\\\)\\{(\\w+\\.\\w+)\\}(?!\\})/', $body_subject, $matches, PREG_PATTERN_ORDER);
         $subjectToken = NULL;
         if ($matches[1]) {
             foreach ($matches[1] as $token) {
                 list($type, $name) = preg_split('/\\./', $token, 2);
                 if ($name) {
                     if (!isset($subjectToken[$type])) {
                         $subjectToken[$type] = array();
                     }
                     $subjectToken[$type][] = $name;
                 }
             }
         }
         $messageSubject = CRM_Utils_Token::replaceContactTokens($body_subject, $contact, FALSE, $subjectToken);
         $messageSubject = CRM_Utils_Token::replaceDomainTokens($messageSubject, $domain, TRUE, $subjectToken);
         $messageSubject = CRM_Utils_Token::replaceComponentTokens($messageSubject, $contact, $subjectToken, TRUE);
         $messageSubject = CRM_Utils_Token::replaceHookTokens($messageSubject, $contact, $categories, TRUE);
         $messageSubject = $smarty->fetch("string:{$messageSubject}");
         if ($schedule->mode == 'SMS' or $schedule->mode == 'User_Preference') {
             $session = CRM_Core_Session::singleton();
             $userID = $session->get('userID') ? $session->get('userID') : $contactId;
             $smsParams = array('To' => $phoneNumber, 'provider_id' => $schedule->sms_provider_id, 'activity_subject' => $messageSubject);
             $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS', 'name');
             $activityParams = array('source_contact_id' => $userID, 'activity_type_id' => $activityTypeID, 'activity_date_time' => date('YmdHis'), 'subject' => $messageSubject, 'details' => $sms_text, 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', 'Completed', 'name'));
             $activity = CRM_Activity_BAO_Activity::create($activityParams);
             CRM_Activity_BAO_Activity::sendSMSMessage($contactId, $sms_text, $html, $smsParams, $activity->id, $userID);
         }
         if ($schedule->mode == 'Email' or $schedule->mode == 'User_Preference') {
             // set up the parameters for CRM_Utils_Mail::send
             $mailParams = array('groupName' => 'Scheduled Reminder Sender', 'from' => $from, 'toName' => $contact['display_name'], 'toEmail' => $email, 'subject' => $messageSubject, 'entity' => 'action_schedule', 'entity_id' => $scheduleID);
             if (!$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
                 // render the &amp; entities in text mode, so that the links work
                 $mailParams['text'] = str_replace('&amp;', '&', $text);
             }
             if ($html && ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
                 $mailParams['html'] = $html;
             }
             $result = CRM_Utils_Mail::send($mailParams);
         }
     }
     $schedule->free();
     return $result;
 }
Example #19
0
 public function postProcess()
 {
     $params = $ids = array();
     $uploadParams = array('header_id', 'footer_id', 'subject', 'from_name', 'from_email');
     $fileType = array('textFile', 'htmlFile');
     $formValues = $this->controller->exportValues($this->_name);
     foreach ($uploadParams as $key) {
         if (!empty($formValues[$key]) || in_array($key, array('header_id', 'footer_id'))) {
             $params[$key] = $formValues[$key];
             $this->set($key, $formValues[$key]);
         }
     }
     if (!$formValues['upload_type']) {
         foreach ($fileType as $key) {
             $contents = NULL;
             if (isset($formValues[$key]) && !empty($formValues[$key])) {
                 $contents = file_get_contents($formValues[$key]['name']);
                 $this->set($key, $formValues[$key]['name']);
             }
             if ($contents) {
                 $params['body_' . substr($key, 0, 4)] = $contents;
             } else {
                 $params['body_' . substr($key, 0, 4)] = 'NULL';
             }
         }
     } else {
         $text_message = $formValues['text_message'];
         $params['body_text'] = $text_message;
         $this->set('textFile', $params['body_text']);
         $this->set('text_message', $params['body_text']);
         $html_message = $formValues['html_message'];
         // dojo editor does some html conversion when tokens are
         // inserted as links. Hence token replacement fails.
         // this is hack to revert html conversion for { to %7B and
         // } to %7D by dojo editor
         $html_message = str_replace('%7B', '{', str_replace('%7D', '}', $html_message));
         $params['body_html'] = $html_message;
         $this->set('htmlFile', $params['body_html']);
         $this->set('html_message', $params['body_html']);
     }
     $params['name'] = $this->get('name');
     $session = CRM_Core_Session::singleton();
     $params['contact_id'] = $session->get('userID');
     $composeFields = array('template', 'saveTemplate', 'updateTemplate', 'saveTemplateName');
     $msgTemplate = NULL;
     //mail template is composed
     if ($formValues['upload_type']) {
         $composeParams = array();
         foreach ($composeFields as $key) {
             if (!empty($formValues[$key])) {
                 $composeParams[$key] = $formValues[$key];
                 $this->set($key, $formValues[$key]);
             }
         }
         if (!empty($composeParams['updateTemplate'])) {
             $templateParams = array('msg_text' => $text_message, 'msg_html' => $html_message, 'msg_subject' => $params['subject'], 'is_active' => TRUE);
             $templateParams['id'] = $formValues['template'];
             $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
         }
         if (!empty($composeParams['saveTemplate'])) {
             $templateParams = array('msg_text' => $text_message, 'msg_html' => $html_message, 'msg_subject' => $params['subject'], 'is_active' => TRUE);
             $templateParams['msg_title'] = $composeParams['saveTemplateName'];
             $msgTemplate = CRM_Core_BAO_MessageTemplate::add($templateParams);
         }
         if (isset($msgTemplate->id)) {
             $params['msg_template_id'] = $msgTemplate->id;
         } else {
             $params['msg_template_id'] = CRM_Utils_Array::value('template', $formValues);
         }
         $this->set('template', $params['msg_template_id']);
     }
     CRM_Core_BAO_File::formatAttachment($formValues, $params, 'civicrm_mailing', $this->_mailingID);
     $ids['mailing_id'] = $this->_mailingID;
     //handle mailing from name & address.
     $fromEmailAddress = CRM_Utils_Array::value($formValues['from_email_address'], CRM_Core_OptionGroup::values('from_email_address'));
     //get the from email address
     $params['from_email'] = CRM_Utils_Mail::pluckEmailFromHeader($fromEmailAddress);
     //get the from Name
     $params['from_name'] = CRM_Utils_Array::value(1, explode('"', $fromEmailAddress));
     //Add Reply-To to headers
     if (!empty($formValues['reply_to_address'])) {
         $replyToEmail = CRM_Core_OptionGroup::values('from_email_address');
         $params['replyto_email'] = CRM_Utils_Array::value($formValues['reply_to_address'], $replyToEmail);
     }
     /* Build the mailing object */
     CRM_Mailing_BAO_Mailing::create($params, $ids);
     if (isset($this->_submitValues['_qf_Upload_upload_save']) && $this->_submitValues['_qf_Upload_upload_save'] == 'Save & Continue Later') {
         //when user perform mailing from search context
         //redirect it to search result CRM-3711.
         $ssID = $this->get('ssID');
         if ($ssID && $this->_searchBasedMailing) {
             if ($this->_action == CRM_Core_Action::BASIC) {
                 $fragment = 'search';
             } elseif ($this->_action == CRM_Core_Action::PROFILE) {
                 $fragment = 'search/builder';
             } elseif ($this->_action == CRM_Core_Action::ADVANCED) {
                 $fragment = 'search/advanced';
             } else {
                 $fragment = 'search/custom';
             }
             $context = $this->get('context');
             if (!CRM_Contact_Form_Search::isSearchContext($context)) {
                 $context = 'search';
             }
             $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
             $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
             if (CRM_Utils_Rule::qfKey($qfKey)) {
                 $urlParams .= "&qfKey={$qfKey}";
             }
             $session = CRM_Core_Session::singleton();
             $draftURL = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
             $status = ts("You can continue later by clicking the 'Continue' action to resume working on it.<br />From <a href='%1'>Draft and Unscheduled Mailings</a>.", array(1 => $draftURL));
             CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
             // Redirect user to search.
             $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
         } else {
             $status = ts("Click the 'Continue' action to resume working on it.");
             $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1');
         }
         CRM_Core_Session::setStatus($status, ts('Mailing Saved'), 'success');
         return $this->controller->setDestination($url);
     }
 }
 /**
  * Send an email from the specified template based on an array of params.
  *
  * @param array $params
  *   A string-keyed array of function params, see function body for details.
  *
  * @return array
  *   Array of four parameters: a boolean whether the email was sent, and the subject, text and HTML templates
  */
 public static function sendTemplate($params)
 {
     $defaults = array('groupName' => NULL, 'valueName' => NULL, 'messageTemplateID' => NULL, 'contactId' => NULL, 'tplParams' => array(), 'from' => NULL, 'toName' => NULL, 'toEmail' => NULL, 'cc' => NULL, 'bcc' => NULL, 'replyTo' => NULL, 'attachments' => NULL, 'isTest' => FALSE, 'PDFFilename' => NULL);
     $params = array_merge($defaults, $params);
     CRM_Utils_Hook::alterMailParams($params, 'messageTemplate');
     if ((!$params['groupName'] || !$params['valueName']) && !$params['messageTemplateID']) {
         CRM_Core_Error::fatal(ts("Message template's option group and/or option value or ID missing."));
     }
     if ($params['messageTemplateID']) {
         // fetch the three elements from the db based on id
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   WHERE mt.id = %1 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['messageTemplateID'], 'String'));
     } else {
         // fetch the three elements from the db based on option_group and option_value names
         $query = 'SELECT msg_subject subject, msg_text text, msg_html html, pdf_format_id format
                   FROM civicrm_msg_template mt
                   JOIN civicrm_option_value ov ON workflow_id = ov.id
                   JOIN civicrm_option_group og ON ov.option_group_id = og.id
                   WHERE og.name = %1 AND ov.name = %2 AND mt.is_default = 1';
         $sqlParams = array(1 => array($params['groupName'], 'String'), 2 => array($params['valueName'], 'String'));
     }
     $dao = CRM_Core_DAO::executeQuery($query, $sqlParams);
     $dao->fetch();
     if (!$dao->N) {
         if ($params['messageTemplateID']) {
             CRM_Core_Error::fatal(ts('No such message template: id=%1.', array(1 => $params['messageTemplateID'])));
         } else {
             CRM_Core_Error::fatal(ts('No such message template: option group %1, option value %2.', array(1 => $params['groupName'], 2 => $params['valueName'])));
         }
     }
     $mailContent = array('subject' => $dao->subject, 'text' => $dao->text, 'html' => $dao->html, 'format' => $dao->format);
     $dao->free();
     CRM_Utils_Hook::alterMailContent($mailContent);
     // add the test banner (if requested)
     if ($params['isTest']) {
         $query = "SELECT msg_subject subject, msg_text text, msg_html html\n                      FROM civicrm_msg_template mt\n                      JOIN civicrm_option_value ov ON workflow_id = ov.id\n                      JOIN civicrm_option_group og ON ov.option_group_id = og.id\n                      WHERE og.name = 'msg_tpl_workflow_meta' AND ov.name = 'test_preview' AND mt.is_default = 1";
         $testDao = CRM_Core_DAO::executeQuery($query);
         $testDao->fetch();
         $mailContent['subject'] = $testDao->subject . $mailContent['subject'];
         $mailContent['text'] = $testDao->text . $mailContent['text'];
         $mailContent['html'] = preg_replace('/<body(.*)$/im', "<body\\1\n{$testDao->html}", $mailContent['html']);
         $testDao->free();
     }
     // replace tokens in the three elements (in subject as if it was the text body)
     $domain = CRM_Core_BAO_Domain::getDomain();
     $hookTokens = array();
     $mailing = new CRM_Mailing_BAO_Mailing();
     $mailing->subject = $mailContent['subject'];
     $mailing->body_text = $mailContent['text'];
     $mailing->body_html = $mailContent['html'];
     $tokens = $mailing->getTokens();
     CRM_Utils_Hook::tokens($hookTokens);
     $categories = array_keys($hookTokens);
     $contactID = CRM_Utils_Array::value('contactId', $params);
     if ($contactID) {
         $contactParams = array('contact_id' => $contactID);
         $returnProperties = array();
         if (isset($tokens['subject']['contact'])) {
             foreach ($tokens['subject']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         if (isset($tokens['text']['contact'])) {
             foreach ($tokens['text']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         if (isset($tokens['html']['contact'])) {
             foreach ($tokens['html']['contact'] as $name) {
                 $returnProperties[$name] = 1;
             }
         }
         // @todo CRM-17253 don't resolve contact details if there are no tokens
         // effectively comment out this next (performance-expensive) line
         // but unfortunately testing is a bit think on the ground to that needs to
         // be added.
         list($contact) = CRM_Utils_Token::getTokenDetails($contactParams, $returnProperties, FALSE, FALSE, NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contact[$contactID];
     }
     $mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['text'], TRUE);
     $mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], TRUE);
     $mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], TRUE);
     if ($contactID) {
         $mailContent['subject'] = CRM_Utils_Token::replaceContactTokens($mailContent['subject'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $mailContent['text'] = CRM_Utils_Token::replaceContactTokens($mailContent['text'], $contact, FALSE, $tokens['text'], FALSE, TRUE);
         $mailContent['html'] = CRM_Utils_Token::replaceContactTokens($mailContent['html'], $contact, FALSE, $tokens['html'], FALSE, TRUE);
         $contactArray = array($contactID => $contact);
         CRM_Utils_Hook::tokenValues($contactArray, array($contactID), NULL, CRM_Utils_Token::flattenTokens($tokens), 'CRM_Core_BAO_MessageTemplate');
         $contact = $contactArray[$contactID];
         $mailContent['subject'] = CRM_Utils_Token::replaceHookTokens($mailContent['subject'], $contact, $categories, TRUE);
         $mailContent['text'] = CRM_Utils_Token::replaceHookTokens($mailContent['text'], $contact, $categories, TRUE);
         $mailContent['html'] = CRM_Utils_Token::replaceHookTokens($mailContent['html'], $contact, $categories, TRUE);
     }
     // strip whitespace from ends and turn into a single line
     $mailContent['subject'] = "{strip}{$mailContent['subject']}{/strip}";
     // parse the three elements with Smarty
     $smarty = CRM_Core_Smarty::singleton();
     foreach ($params['tplParams'] as $name => $value) {
         $smarty->assign($name, $value);
     }
     foreach (array('subject', 'text', 'html') as $elem) {
         $mailContent[$elem] = $smarty->fetch("string:{$mailContent[$elem]}");
     }
     // send the template, honouring the target user’s preferences (if any)
     $sent = FALSE;
     // create the params array
     $params['subject'] = $mailContent['subject'];
     $params['text'] = $mailContent['text'];
     $params['html'] = $mailContent['html'];
     if ($params['toEmail']) {
         $contactParams = array(array('email', 'LIKE', $params['toEmail'], 0, 1));
         list($contact, $_) = CRM_Contact_BAO_Query::apiQuery($contactParams);
         $prefs = array_pop($contact);
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'HTML') {
             $params['text'] = NULL;
         }
         if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] == 'Text') {
             $params['html'] = NULL;
         }
         $config = CRM_Core_Config::singleton();
         if (isset($params['isEmailPdf']) && $params['isEmailPdf'] == 1) {
             $pdfHtml = CRM_Contribute_BAO_ContributionPage::addInvoicePdfToEmail($params['contributionId'], $params['contactId']);
             if (empty($params['attachments'])) {
                 $params['attachments'] = array();
             }
             $params['attachments'][] = CRM_Utils_Mail::appendPDF('Invoice.pdf', $pdfHtml, $mailContent['format']);
         }
         $pdf_filename = '';
         if ($config->doNotAttachPDFReceipt && $params['PDFFilename'] && $params['html']) {
             if (empty($params['attachments'])) {
                 $params['attachments'] = array();
             }
             $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
             if (isset($params['tplParams']['email_comment'])) {
                 $params['html'] = $params['tplParams']['email_comment'];
                 $params['text'] = strip_tags($params['tplParams']['email_comment']);
             }
         }
         $sent = CRM_Utils_Mail::send($params);
         if ($pdf_filename) {
             unlink($pdf_filename);
         }
     }
     return array($sent, $mailContent['subject'], $mailContent['text'], $mailContent['html']);
 }
 /**
  * Process the form submission.
  */
 function postProcess()
 {
     $formValues = $this->exportValues();
     $buttonName = $this->controller->getButtonName();
     if ($buttonName == $this->_testButtonName) {
         $session = CRM_Core_Session::singleton();
         $userID = $session->get('userID');
         list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID);
         //get the default domain email address.CRM-4250
         list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
         if (!$domainEmailAddress || $domainEmailAddress == '*****@*****.**') {
             $fixUrl = CRM_Utils_System::url("civicrm/admin/domain", 'action=update&reset=1');
             CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
         }
         if (!$toEmail) {
             CRM_Core_Error::statusBounce(ts('Cannot send a test email because your user record does not have a valid email address.'));
         }
         if (!trim($toDisplayName)) {
             $toDisplayName = $toEmail;
         }
         $testMailStatusMsg = ts('Sending test email. FROM: %1 TO: %2.<br />', array(1 => $domainEmailAddress, 2 => $toEmail));
         $params = array();
         $message = "SMTP settings are correct.";
         $params['host'] = $formValues['smtpServer'];
         $params['port'] = $formValues['smtpPort'];
         if ($formValues['smtpAuth']) {
             $params['username'] = $formValues['smtpUsername'];
             $params['password'] = $formValues['smtpPassword'];
             $params['auth'] = TRUE;
         } else {
             $params['auth'] = FALSE;
         }
         // set the localhost value, CRM-3153, CRM-9332
         $params['localhost'] = $_SERVER['SERVER_NAME'];
         // also set the timeout value, lets set it to 30 seconds
         // CRM-7510, CRM-9332
         $params['timeout'] = 30;
         $mailerName = 'smtp';
         $headers = array('From' => '"' . $domainEmailName . '" <' . $domainEmailAddress . '>', 'To' => '"' . $toDisplayName . '"' . "<{$toEmail}>", 'Subject' => "Test for SMTP settings");
         $mailer = Mail::factory($mailerName, $params);
         $config = CRM_Core_Config::singleton();
         if (property_exists($config, 'civiVersion')) {
             $civiVersion = $config->civiVersion;
         } else {
             $civiVersion = CRM_Core_BAO_Domain::version();
         }
         if (version_compare('4.5alpha1', $civiVersion) > 0) {
             CRM_Core_Error::ignoreException();
         } else {
             $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         }
         $result = $mailer->send($toEmail, $headers, $message);
         if (version_compare('4.5alpha1', $civiVersion) > 0) {
             CRM_Core_Error::setCallback();
         } else {
             unset($errorScope);
         }
         if (!is_a($result, 'PEAR_Error')) {
             CRM_Core_Session::setStatus($testMailStatusMsg . ts('Your %1 settings are correct. A test email has been sent to your email address.', array(1 => strtoupper($mailerName))), ts("Mail Sent"), "success");
         } else {
             $message = CRM_Utils_Mail::errorMessage($mailer, $result);
             CRM_Core_Session::setStatus($testMailStatusMsg . ts('Oops. Your %1 settings are incorrect. No test mail has been sent.', array(1 => strtoupper($mailerName))) . $message, ts("Mail Not Sent"), "error");
         }
     }
     // if password is present, encrypt it
     if (!empty($formValues['smtpPassword'])) {
         $formValues['smtpPassword'] = CRM_Utils_Crypt::encrypt($formValues['smtpPassword']);
     }
     CRM_Core_BAO_Setting::setItem($formValues, CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mandrill_smtp_settings');
 }
Example #22
0
 static function mailReport($fileContent, $instanceID = NULL, $outputMode = 'html', $attachments = array())
 {
     if (!$instanceID) {
         return FALSE;
     }
     list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
     $params = array('id' => $instanceID);
     $instanceInfo = array();
     CRM_Core_DAO::commonRetrieve('CRM_Report_DAO_ReportInstance', $params, $instanceInfo);
     $params = array();
     $params['groupName'] = 'Report Email Sender';
     $params['from'] = '"' . $domainEmailName . '" <' . $domainEmailAddress . '>';
     //$domainEmailName;
     $params['toName'] = "";
     $params['toEmail'] = CRM_Utils_Array::value('email_to', $instanceInfo);
     $params['cc'] = CRM_Utils_Array::value('email_cc', $instanceInfo);
     $params['subject'] = CRM_Utils_Array::value('email_subject', $instanceInfo);
     if (empty($instanceInfo['attachments'])) {
         $instanceInfo['attachments'] = array();
     }
     $params['attachments'] = array_merge(CRM_Utils_Array::value('attachments', $instanceInfo), $attachments);
     $params['text'] = '';
     $params['html'] = $fileContent;
     return CRM_Utils_Mail::send($params);
 }
 /**
  * Compose a message.
  *
  * @param int $job_id
  *   ID of the Job associated with this message.
  * @param int $event_queue_id
  *   ID of the EventQueue.
  * @param string $hash
  *   Hash of the EventQueue.
  * @param string $contactId
  *   ID of the Contact.
  * @param string $email
  *   Destination address.
  * @param string $recipient
  *   To: of the recipient.
  * @param bool $test
  *   Is this mailing a test?.
  * @param $contactDetails
  * @param $attachments
  * @param bool $isForward
  *   Is this mailing compose for forward?.
  * @param string $fromEmail
  *   Email address of who is forwardinf it.
  *
  * @param null $replyToEmail
  *
  * @return Mail_mime               The mail object
  */
 public function compose($job_id, $event_queue_id, $hash, $contactId, $email, &$recipient, $test, $contactDetails, &$attachments, $isForward = FALSE, $fromEmail = NULL, $replyToEmail = NULL)
 {
     $config = CRM_Core_Config::singleton();
     $this->getTokens();
     if ($this->_domain == NULL) {
         $this->_domain = CRM_Core_BAO_Domain::getDomain();
     }
     list($verp, $urls, $headers) = $this->getVerpAndUrlsAndHeaders($job_id, $event_queue_id, $hash, $email, $isForward);
     //set from email who is forwarding it and not original one.
     if ($fromEmail) {
         unset($headers['From']);
         $headers['From'] = "<{$fromEmail}>";
     }
     if ($replyToEmail && $fromEmail != $replyToEmail) {
         $headers['Reply-To'] = "{$replyToEmail}";
     }
     if ($contactDetails) {
         $contact = $contactDetails;
     } elseif ($contactId === 0) {
         //anonymous user
         $contact = array();
         CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id);
     } else {
         $params = array(array('contact_id', '=', $contactId, 0, 0));
         list($contact) = CRM_Contact_BAO_Query::apiQuery($params);
         //CRM-4524
         $contact = reset($contact);
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
             CRM_Core_Error::debug_log_message(ts('CiviMail will not send email to a non-existent contact: %1', array(1 => $contactId)));
             // setting this because function is called by reference
             //@todo test not calling function by reference
             $res = NULL;
             return $res;
         }
         // also call the hook to get contact details
         CRM_Utils_Hook::tokenValues($contact, $contactId, $job_id);
     }
     $pTemplates = $this->getPreparedTemplates();
     $pEmails = array();
     foreach ($pTemplates as $type => $pTemplate) {
         $html = $type == 'html' ? TRUE : FALSE;
         $pEmails[$type] = array();
         $pEmail =& $pEmails[$type];
         $template =& $pTemplates[$type]['template'];
         $tokens =& $pTemplates[$type]['tokens'];
         $idx = 0;
         if (!empty($tokens)) {
             foreach ($tokens as $idx => $token) {
                 $token_data = $this->getTokenData($token, $html, $contact, $verp, $urls, $event_queue_id);
                 array_push($pEmail, $template[$idx]);
                 array_push($pEmail, $token_data);
             }
         } else {
             array_push($pEmail, $template[$idx]);
         }
         if (isset($template[$idx + 1])) {
             array_push($pEmail, $template[$idx + 1]);
         }
     }
     $html = NULL;
     if (isset($pEmails['html']) && is_array($pEmails['html']) && count($pEmails['html'])) {
         $html =& $pEmails['html'];
     }
     $text = NULL;
     if (isset($pEmails['text']) && is_array($pEmails['text']) && count($pEmails['text'])) {
         $text =& $pEmails['text'];
     }
     // push the tracking url on to the html email if necessary
     if ($this->open_tracking && $html) {
         array_push($html, "\n" . '<img src="' . $config->userFrameworkResourceURL . "extern/open.php?q={$event_queue_id}\" width='1' height='1' alt='' border='0'>");
     }
     $message = new Mail_mime("\n");
     $useSmarty = defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY ? TRUE : FALSE;
     if ($useSmarty) {
         $smarty = CRM_Core_Smarty::singleton();
         // also add the contact tokens to the template
         $smarty->assign_by_ref('contact', $contact);
     }
     $mailParams = $headers;
     if ($text && ($test || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both' || $contact['preferred_mail_format'] == 'HTML' && !array_key_exists('html', $pEmails))) {
         $textBody = implode('', $text);
         if ($useSmarty) {
             $textBody = $smarty->fetch("string:{$textBody}");
         }
         $mailParams['text'] = $textBody;
     }
     if ($html && ($test || ($contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both'))) {
         $htmlBody = implode('', $html);
         if ($useSmarty) {
             $htmlBody = $smarty->fetch("string:{$htmlBody}");
         }
         $mailParams['html'] = $htmlBody;
     }
     if (empty($mailParams['text']) && empty($mailParams['html'])) {
         // CRM-9833
         // something went wrong, lets log it and return null (by reference)
         CRM_Core_Error::debug_log_message(ts('CiviMail will not send an empty mail body, Skipping: %1', array(1 => $email)));
         $res = NULL;
         return $res;
     }
     $mailParams['attachments'] = $attachments;
     $mailingSubject = CRM_Utils_Array::value('subject', $pEmails);
     if (is_array($mailingSubject)) {
         $mailingSubject = implode('', $mailingSubject);
     }
     $mailParams['Subject'] = $mailingSubject;
     $mailParams['toName'] = CRM_Utils_Array::value('display_name', $contact);
     $mailParams['toEmail'] = $email;
     // Add job ID to mailParams for external email delivery service to utilise
     $mailParams['job_id'] = $job_id;
     CRM_Utils_Hook::alterMailParams($mailParams, 'civimail');
     // CRM-10699 support custom email headers
     if (!empty($mailParams['headers'])) {
         $headers = array_merge($headers, $mailParams['headers']);
     }
     //cycle through mailParams and set headers array
     foreach ($mailParams as $paramKey => $paramValue) {
         //exclude values not intended for the header
         if (!in_array($paramKey, array('text', 'html', 'attachments', 'toName', 'toEmail'))) {
             $headers[$paramKey] = $paramValue;
         }
     }
     if (!empty($mailParams['text'])) {
         $message->setTxtBody($mailParams['text']);
     }
     if (!empty($mailParams['html'])) {
         $message->setHTMLBody($mailParams['html']);
     }
     if (!empty($mailParams['attachments'])) {
         foreach ($mailParams['attachments'] as $fileID => $attach) {
             $message->addAttachment($attach['fullPath'], $attach['mime_type'], $attach['cleanName']);
         }
     }
     //pickup both params from mail params.
     $toName = trim($mailParams['toName']);
     $toEmail = trim($mailParams['toEmail']);
     if ($toName == $toEmail || strpos($toName, '@') !== FALSE) {
         $toName = NULL;
     } else {
         $toName = CRM_Utils_Mail::formatRFC2822Name($toName);
     }
     $headers['To'] = "{$toName} <{$toEmail}>";
     $headers['Precedence'] = 'bulk';
     // Will test in the mail processor if the X-VERP is set in the bounced email.
     // (As an option to replace real VERP for those that can't set it up)
     $headers['X-CiviMail-Bounce'] = $verp['bounce'];
     //CRM-5058
     //token replacement of subject
     $headers['Subject'] = $mailingSubject;
     CRM_Utils_Mail::setMimeParams($message);
     $headers = $message->headers($headers);
     //get formatted recipient
     $recipient = $headers['To'];
     // make sure we unset a lot of stuff
     unset($verp);
     unset($urls);
     unset($params);
     unset($contact);
     unset($ids);
     return $message;
 }
Example #24
0
 /**
  * Implements Mail_mail::send() function using php's built-in mail()
  * command.
  *
  * @param mixed $recipients Either a comma-seperated list of recipients
  *              (RFC822 compliant), or an array of recipients,
  *              each RFC822 valid. This may contain recipients not
  *              specified in the headers, for Bcc:, resending
  *              messages, etc.
  *
  * @param array $headers The array of headers to send with the mail, in an
  *              associative array, where the array key is the
  *              header name (ie, 'Subject'), and the array value
  *              is the header value (ie, 'test'). The header
  *              produced from those values would be 'Subject:
  *              test'.
  *
  * @param string $body The full text of the message body, including any
  *               Mime parts, etc.
  *
  * @return mixed Returns true on success, or a PEAR_Error
  *               containing a descriptive error message on
  *               failure.
  *
  * @access public
  */
 function send($recipients, $headers, $body)
 {
     if (defined('CIVICRM_MAIL_LOG')) {
         require_once 'CRM/Utils/Mail.php';
         CRM_Utils_Mail::logger($recipients, $headers, $body);
         return true;
     }
     $this->_sanitizeHeaders($headers);
     // If we're passed an array of recipients, implode it.
     if (is_array($recipients)) {
         $recipients = implode(', ', $recipients);
     }
     // Get the Subject out of the headers array so that we can
     // pass it as a seperate argument to mail().
     $subject = '';
     if (isset($headers['Subject'])) {
         $subject = $headers['Subject'];
         unset($headers['Subject']);
     }
     /*
      * Also remove the To: header.  The mail() function will add its own
      * To: header based on the contents of $recipients.
      */
     unset($headers['To']);
     // Flatten the headers out.
     $headerElements = $this->prepareHeaders($headers);
     if (PEAR::isError($headerElements)) {
         return $headerElements;
     }
     list(, $text_headers) = $headerElements;
     /*
      * We only use mail()'s optional fifth parameter if the additional
      * parameters have been provided and we're not running in safe mode.
      */
     if (empty($this->_params) || ini_get('safe_mode')) {
         $result = mail($recipients, $subject, $body, $text_headers);
     } else {
         $result = mail($recipients, $subject, $body, $text_headers, $this->_params);
     }
     /*
      * If the mail() function returned failure, we need to create a
      * PEAR_Error object and return it instead of the boolean result.
      */
     if ($result === false) {
         $result = PEAR::raiseError('mail() returned failure');
     }
     return $result;
 }
Example #25
0
 /**
  * Send a reponse email informing the contact of the groups from which he
  * has been unsubscribed.
  *
  * @param string $queue_id      The queue event ID
  * @param array $groups         List of group IDs
  * @param bool $is_domain       Is this domain-level?
  * @param int $job              The job ID
  * @return void
  * @access public
  * @static
  */
 public static function send_unsub_response($queue_id, $groups, $is_domain = false, $job)
 {
     $config =& CRM_Core_Config::singleton();
     $domain =& CRM_Core_BAO_Domain::getDomain();
     $jobTable = CRM_Mailing_BAO_Job::getTableName();
     $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
     $contacts = CRM_Contact_DAO_Contact::getTableName();
     $email = CRM_Core_DAO_Email::getTableName();
     $queue = CRM_Mailing_Event_BAO_Queue::getTableName();
     //get the default domain email address.
     list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
     $dao =& new CRM_Mailing_BAO_Mailing();
     $dao->query("   SELECT * FROM {$mailingTable} \n                        INNER JOIN {$jobTable} ON\n                            {$jobTable}.mailing_id = {$mailingTable}.id \n                        WHERE {$jobTable}.id = {$job}");
     $dao->fetch();
     $component =& new CRM_Mailing_BAO_Component();
     if ($is_domain) {
         $component->id = $dao->optout_id;
     } else {
         $component->id = $dao->unsubscribe_id;
     }
     $component->find(true);
     $html = $component->body_html;
     if ($component->body_text) {
         $text = $component->body_text;
     } else {
         $text = CRM_Utils_String::htmlToText($component->body_html);
     }
     $eq =& new CRM_Core_DAO();
     $eq->query("SELECT     {$contacts}.preferred_mail_format as format,\n                    {$contacts}.id as contact_id,\n                    {$email}.email as email,\n                    {$queue}.hash as hash\n        FROM        {$contacts}\n        INNER JOIN  {$queue} ON {$queue}.contact_id = {$contacts}.id\n        INNER JOIN  {$email} ON {$queue}.email_id = {$email}.id\n        WHERE       {$queue}.id = " . CRM_Utils_Type::escape($queue_id, 'Integer'));
     $eq->fetch();
     if ($groups) {
         foreach ($groups as $key => $value) {
             if (!$value) {
                 unset($groups[$key]);
             }
         }
     }
     $message =& new Mail_Mime("\n");
     list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email);
     $bao =& new CRM_Mailing_BAO_Mailing();
     $bao->body_text = $text;
     $bao->body_html = $html;
     $tokens = $bao->getTokens();
     require_once 'CRM/Utils/Token.php';
     if ($eq->format == 'HTML' || $eq->format == 'Both') {
         $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, true, $tokens['html']);
         $html = CRM_Utils_Token::replaceUnsubscribeTokens($html, $domain, $groups, true, $eq->contact_id, $eq->hash);
         $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, true, $tokens['html']);
         $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, null, $tokens['html']);
         $message->setHTMLBody($html);
     }
     if (!$html || $eq->format == 'Text' || $eq->format == 'Both') {
         $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, false, $tokens['text']);
         $text = CRM_Utils_Token::replaceUnsubscribeTokens($text, $domain, $groups, false, $eq->contact_id, $eq->hash);
         $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, false, $tokens['text']);
         $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, null, $tokens['text']);
         $message->setTxtBody($text);
     }
     require_once 'CRM/Core/BAO/MailSettings.php';
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $headers = array('Subject' => $component->subject, 'From' => "\"{$domainEmailName}\" <do-not-reply@{$emailDomain}>", 'To' => $eq->email, 'Reply-To' => "do-not-reply@{$emailDomain}", 'Return-Path' => "do-not-reply@{$emailDomain}");
     $b =& CRM_Utils_Mail::setMimeParams($message);
     $h =& $message->headers($headers);
     $mailer =& $config->getMailer();
     PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Core_Error', 'nullHandler'));
     if (is_object($mailer)) {
         $mailer->send($eq->email, $h, $b);
         CRM_Core_Error::setCallback();
     }
 }
Example #26
0
 /**
  * Send a response email informing the contact of the groups from which he.
  * has been unsubscribed.
  *
  * @param string $queue_id
  *   The queue event ID.
  * @param array $groups
  *   List of group IDs.
  * @param bool $is_domain
  *   Is this domain-level?.
  * @param int $job
  *   The job ID.
  */
 public static function send_unsub_response($queue_id, $groups, $is_domain = FALSE, $job)
 {
     $config = CRM_Core_Config::singleton();
     $domain = CRM_Core_BAO_Domain::getDomain();
     $jobObject = new CRM_Mailing_BAO_MailingJob();
     $jobTable = $jobObject->getTableName();
     $mailingObject = new CRM_Mailing_DAO_Mailing();
     $mailingTable = $mailingObject->getTableName();
     $contactsObject = new CRM_Contact_DAO_Contact();
     $contacts = $contactsObject->getTableName();
     $emailObject = new CRM_Core_DAO_Email();
     $email = $emailObject->getTableName();
     $queueObject = new CRM_Mailing_Event_BAO_Queue();
     $queue = $queueObject->getTableName();
     //get the default domain email address.
     list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
     $dao = new CRM_Mailing_BAO_Mailing();
     $dao->query("   SELECT * FROM {$mailingTable}\n                        INNER JOIN {$jobTable} ON\n                            {$jobTable}.mailing_id = {$mailingTable}.id\n                        WHERE {$jobTable}.id = {$job}");
     $dao->fetch();
     $component = new CRM_Mailing_BAO_Component();
     if ($is_domain) {
         $component->id = $dao->optout_id;
     } else {
         $component->id = $dao->unsubscribe_id;
     }
     $component->find(TRUE);
     $html = $component->body_html;
     if ($component->body_text) {
         $text = $component->body_text;
     } else {
         $text = CRM_Utils_String::htmlToText($component->body_html);
     }
     $eq = new CRM_Core_DAO();
     $eq->query("SELECT     {$contacts}.preferred_mail_format as format,\n                    {$contacts}.id as contact_id,\n                    {$email}.email as email,\n                    {$queue}.hash as hash\n        FROM        {$contacts}\n        INNER JOIN  {$queue} ON {$queue}.contact_id = {$contacts}.id\n        INNER JOIN  {$email} ON {$queue}.email_id = {$email}.id\n        WHERE       {$queue}.id = " . CRM_Utils_Type::escape($queue_id, 'Integer'));
     $eq->fetch();
     if ($groups) {
         foreach ($groups as $key => $value) {
             if (!$value) {
                 unset($groups[$key]);
             }
         }
     }
     $message = new Mail_mime("\n");
     list($addresses, $urls) = CRM_Mailing_BAO_Mailing::getVerpAndUrls($job, $queue_id, $eq->hash, $eq->email);
     $bao = new CRM_Mailing_BAO_Mailing();
     $bao->body_text = $text;
     $bao->body_html = $html;
     $tokens = $bao->getTokens();
     if ($eq->format == 'HTML' || $eq->format == 'Both') {
         $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
         $html = CRM_Utils_Token::replaceUnsubscribeTokens($html, $domain, $groups, TRUE, $eq->contact_id, $eq->hash);
         $html = CRM_Utils_Token::replaceActionTokens($html, $addresses, $urls, TRUE, $tokens['html']);
         $html = CRM_Utils_Token::replaceMailingTokens($html, $dao, NULL, $tokens['html']);
         $message->setHTMLBody($html);
     }
     if (!$html || $eq->format == 'Text' || $eq->format == 'Both') {
         $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
         $text = CRM_Utils_Token::replaceUnsubscribeTokens($text, $domain, $groups, FALSE, $eq->contact_id, $eq->hash);
         $text = CRM_Utils_Token::replaceActionTokens($text, $addresses, $urls, FALSE, $tokens['text']);
         $text = CRM_Utils_Token::replaceMailingTokens($text, $dao, NULL, $tokens['text']);
         $message->setTxtBody($text);
     }
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $headers = array('Subject' => $component->subject, 'From' => "\"{$domainEmailName}\" <do-not-reply@{$emailDomain}>", 'To' => $eq->email, 'Reply-To' => "do-not-reply@{$emailDomain}", 'Return-Path' => "do-not-reply@{$emailDomain}");
     CRM_Mailing_BAO_Mailing::addMessageIdHeader($headers, 'u', $job, $queue_id, $eq->hash);
     $b = CRM_Utils_Mail::setMimeParams($message);
     $h = $message->headers($headers);
     $mailer = \Civi::service('pear_mail');
     if (is_object($mailer)) {
         $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         $mailer->send($eq->email, $h, $b);
         unset($errorScope);
     }
 }
 /**
  * Send an alert email.
  *
  * @param $p_eWAY_tran_num
  * @param $p_trxn_out
  * @param $p_trxn_back
  * @param $p_request
  * @param $p_response
  */
 public function send_alert_email($p_eWAY_tran_num, $p_trxn_out, $p_trxn_back, $p_request, $p_response)
 {
     // Initialization call is required to use CiviCRM APIs.
     civicrm_initialize(TRUE);
     list($fromName, $fromEmail) = CRM_Core_BAO_Domain::getNameAndEmail();
     $from = "{$fromName} <{$fromEmail}>";
     $toName = 'Support at eWAY';
     $toEmail = '*****@*****.**';
     $subject = "ALERT: Unique Trxn Number Failure : eWAY Transaction # = [" . $p_eWAY_tran_num . "]";
     $message = "\nTRXN sent out with request   = '{$p_trxn_out}'.\nTRXN sent back with response = '{$p_trxn_back}'.\n\nThis is a ['{$this->_mode}'] transaction.\n\n\nRequest XML =\n---------------------------------------------------------------------------\n{$p_request}\n---------------------------------------------------------------------------\n\n\nResponse XML =\n---------------------------------------------------------------------------\n{$p_response}\n---------------------------------------------------------------------------\n\n\nRegards\n\nThe CiviCRM eWAY Payment Processor Module\n";
     $params = array();
     $params['groupName'] = 'eWay Email Sender';
     $params['from'] = $from;
     $params['toName'] = $toName;
     $params['toEmail'] = $toEmail;
     $params['subject'] = $subject;
     $params['text'] = $message;
     CRM_Utils_Mail::send($params);
 }
 public static function processMandrillCalls($reponse)
 {
     $events = array('open', 'click', 'hard_bounce', 'soft_bounce', 'spam', 'reject');
     $bounceType = array();
     //MTE-17
     $config = CRM_Core_Config::singleton();
     if (property_exists($config, 'civiVersion')) {
         $civiVersion = $config->civiVersion;
     } else {
         $civiVersion = CRM_Core_BAO_Domain::version();
     }
     if (version_compare('4.4alpha1', $civiVersion) > 0) {
         $jobCLassName = 'CRM_Mailing_DAO_Job';
     } else {
         $jobCLassName = 'CRM_Mailing_DAO_MailingJob';
     }
     foreach ($reponse as $value) {
         //changes done to check if email exists in response array
         if (in_array($value['event'], $events) && CRM_Utils_Array::value('email', $value['msg'])) {
             $metaData = CRM_Utils_Array::value('metadata', $value['msg']) ? CRM_Utils_Array::value('CiviCRM_Mandrill_id', $value['msg']['metadata']) : NULL;
             $header = self::extractHeader($metaData);
             $mail = self::getMailing($header, $jobCLassName);
             $contacts = array();
             if ($mail->find(TRUE)) {
                 if ($value['event'] == 'click' && $mail->url_tracking === FALSE || $value['event'] == 'open' && $mail->open_tracking === FALSE) {
                     continue;
                 }
                 $emails = self::retrieveEmailContactId($value['msg']['email']);
                 if (!CRM_Utils_Array::value('contact_id', $emails['email'])) {
                     continue;
                 }
                 $value['mailing_id'] = $mail->id;
                 // IF no activity id in header then create new activity
                 if (empty($header[0])) {
                     self::createActivity($value, NULL, $header);
                 }
                 if (empty($header[2])) {
                     $params = array('job_id' => CRM_Core_DAO::getFieldValue($jobCLassName, $mail->id, 'id', 'mailing_id'), 'contact_id' => $emails['email']['contact_id'], 'email_id' => $emails['email']['id']);
                     $eventQueue = CRM_Mailing_Event_BAO_Queue::create($params);
                     $eventQueueID = $eventQueue->id;
                     $hash = $eventQueue->hash;
                     $jobId = $params['job_id'];
                 } else {
                     $eventQueueID = $header[3];
                     $hash = explode('@', $header[4]);
                     $hash = $hash[0];
                     $jobId = $header[2];
                 }
                 if ($eventQueueID) {
                     $mandrillActivtyParams = array('mailing_queue_id' => $eventQueueID, 'activity_id' => $header[0]);
                     CRM_Mte_BAO_MandrillActivity::create($mandrillActivtyParams);
                 }
                 $msgBody = '';
                 if (!empty($header[0])) {
                     $msgBody = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $header[0], 'details');
                 }
                 $value['mail_body'] = $msgBody;
                 $bType = ucfirst(preg_replace('/_\\w+/', '', $value['event']));
                 $assignedContacts = array();
                 switch ($value['event']) {
                     case 'open':
                         $oe = new CRM_Mailing_Event_BAO_Opened();
                         $oe->event_queue_id = $eventQueueID;
                         $oe->time_stamp = date('YmdHis', $value['ts']);
                         $oe->save();
                         break;
                     case 'click':
                         if (CRM_Utils_Array::value(1, $header) == 'b') {
                             break;
                         }
                         $tracker = new CRM_Mailing_BAO_TrackableURL();
                         $tracker->url = $value['url'];
                         $tracker->mailing_id = $mail->id;
                         if (!$tracker->find(TRUE)) {
                             $tracker->save();
                         }
                         $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
                         $open->event_queue_id = $eventQueueID;
                         $open->trackable_url_id = $tracker->id;
                         $open->time_stamp = date('YmdHis', $value['ts']);
                         $open->save();
                         break;
                     case 'hard_bounce':
                     case 'soft_bounce':
                     case 'spam':
                     case 'reject':
                         if (empty($bounceType)) {
                             CRM_Core_PseudoConstant::populate($bounceType, 'CRM_Mailing_DAO_BounceType', TRUE, 'id', NULL, NULL, NULL, 'name');
                         }
                         //Delete queue in delivered since this email is not successfull
                         $delivered = new CRM_Mailing_Event_BAO_Delivered();
                         $delivered->event_queue_id = $eventQueueID;
                         if ($delivered->find(TRUE)) {
                             $delivered->delete();
                         }
                         $bounceParams = array('time_stamp' => date('YmdHis', $value['ts']), 'event_queue_id' => $eventQueueID, 'bounce_type_id' => $bounceType["Mandrill {$bType}"], 'job_id' => $jobId, 'hash' => $hash);
                         $bounceParams['bounce_reason'] = CRM_Utils_Array::value('bounce_description', $value['msg']);
                         if (empty($bounceParams['bounce_reason'])) {
                             $bounceParams['bounce_reason'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_BounceType', $bounceType["Mandrill {$bType}"], 'description');
                         }
                         CRM_Mailing_Event_BAO_Bounce::create($bounceParams);
                         if (substr($value['event'], -7) == '_bounce') {
                             $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mandrill_smtp_settings');
                             if (CRM_Utils_Array::value('group_id', $mailingBackend)) {
                                 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
                                 $mailBody = ts('The following email failed to be delivered due to a') . " {$bType} Bounce :</br>\nTo: {$value['msg']['email']} </br>\nFrom: {$value['msg']['sender']} </br>\nSubject: {$value['msg']['subject']}</br>\nMessage Body: {$msgBody}";
                                 $mailParams = array('groupName' => 'Mandrill bounce notification', 'from' => '"' . $domainEmailName . '" <' . $domainEmailAddress . '>', 'subject' => ts('Mandrill Bounce Notification'), 'text' => $mailBody, 'html' => $mailBody);
                                 $query = "SELECT ce.email, cc.sort_name, cgc.contact_id FROM civicrm_contact cc\nINNER JOIN civicrm_group_contact cgc ON cgc.contact_id = cc.id\nINNER JOIN civicrm_email ce ON ce.contact_id = cc.id\nWHERE cc.is_deleted = 0 AND cc.is_deceased = 0 AND cgc.group_id = {$mailingBackend['group_id']} AND ce.is_primary = 1 AND ce.email <> %1";
                                 $queryParam = array(1 => array($value['msg']['email'], 'String'));
                                 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
                                 while ($dao->fetch()) {
                                     $mailParams['toName'] = $dao->sort_name;
                                     $mailParams['toEmail'] = $dao->email;
                                     CRM_Utils_Mail::send($mailParams);
                                     $value['assignee_contact_id'][] = $dao->contact_id;
                                 }
                             }
                         }
                         $bType = 'Bounce';
                         break;
                 }
                 // create activity for click and open event
                 if ($value['event'] == 'open' || $value['event'] == 'click' || $bType == 'Bounce') {
                     self::createActivity($value, $bType, $header);
                 }
             }
         }
     }
 }
Example #29
0
 /**
  * Confirm a pending subscription
  *
  * @param int $contact_id       The id of the contact
  * @param int $subscribe_id     The id of the subscription event
  * @param string $hash          The hash
  *
  * @return boolean              True on success
  * @access public
  * @static
  */
 public static function confirm($contact_id, $subscribe_id, $hash)
 {
     $se =& CRM_Mailing_Event_BAO_Subscribe::verify($contact_id, $subscribe_id, $hash);
     if (!$se) {
         return FALSE;
     }
     // before we proceed lets just check if this contact is already 'Added'
     // if so, we should ignore this request and hence avoid sending multiple
     // emails - CRM-11157
     $details = CRM_Contact_BAO_GroupContact::getMembershipDetail($contact_id, $se->group_id);
     if ($details && $details->status == 'Added') {
         // This contact is already subscribed
         // lets return the group title
         return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $se->group_id, 'title');
     }
     $transaction = new CRM_Core_Transaction();
     $ce = new CRM_Mailing_Event_BAO_Confirm();
     $ce->event_subscribe_id = $se->id;
     $ce->time_stamp = date('YmdHis');
     $ce->save();
     CRM_Contact_BAO_GroupContact::addContactsToGroup(array($contact_id), $se->group_id, 'Email', 'Added', $ce->id);
     $transaction->commit();
     $config = CRM_Core_Config::singleton();
     $domain = CRM_Core_BAO_Domain::getDomain();
     list($domainEmailName, $_) = CRM_Core_BAO_Domain::getNameAndEmail();
     list($display_name, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id);
     $group = new CRM_Contact_DAO_Group();
     $group->id = $se->group_id;
     $group->find(TRUE);
     $component = new CRM_Mailing_BAO_Component();
     $component->is_default = 1;
     $component->is_active = 1;
     $component->component_type = 'Welcome';
     $component->find(TRUE);
     $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
     $html = $component->body_html;
     if ($component->body_text) {
         $text = $component->body_text;
     } else {
         $text = CRM_Utils_String::htmlToText($component->body_html);
     }
     $bao = new CRM_Mailing_BAO_Mailing();
     $bao->body_text = $text;
     $bao->body_html = $html;
     $tokens = $bao->getTokens();
     $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, TRUE, $tokens['html']);
     $html = CRM_Utils_Token::replaceWelcomeTokens($html, $group->title, TRUE);
     $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, FALSE, $tokens['text']);
     $text = CRM_Utils_Token::replaceWelcomeTokens($text, $group->title, FALSE);
     $mailParams = array('groupName' => 'Mailing Event ' . $component->component_type, 'subject' => $component->subject, 'from' => "\"{$domainEmailName}\" <do-not-reply@{$emailDomain}>", 'toEmail' => $email, 'toName' => $display_name, 'replyTo' => "do-not-reply@{$emailDomain}", 'returnPath' => "do-not-reply@{$emailDomain}", 'html' => $html, 'text' => $text);
     // send - ignore errors because the desired status change has already been successful
     $unused_result = CRM_Utils_Mail::send($mailParams);
     return $group->title;
 }
Example #30
0
 /**
  * These tasks are the core set of tasks that the user can perform
  * on a contact / group of contacts
  *
  * @return array
  *   the set of tasks for a group of contacts
  */
 public static function &taskTitles()
 {
     self::initTasks();
     $titles = array();
     foreach (self::$_tasks as $id => $value) {
         $titles[$id] = $value['title'];
     }
     // hack unset update saved search
     unset($titles[self::SAVE_SEARCH_UPDATE]);
     if (!CRM_Utils_Mail::validOutBoundMail()) {
         unset($titles[self::EMAIL_CONTACTS]);
         unset($titles[self::CREATE_MAILING]);
     }
     // CRM-6806
     if (!CRM_Core_Permission::check('access deleted contacts') || !CRM_Core_Permission::check('delete contacts')) {
         unset($titles[self::DELETE_PERMANENTLY]);
     }
     asort($titles);
     return $titles;
 }