/** * Build the form * * @access public * @return void */ static function buildQuickForm(&$form) { $toArray = $ccArray = $bccArray = array(); $suppressedEmails = 0; $to = $form->add('text', 'to', ts('To'), '', true); $cc = $form->add('text', 'cc_id', ts('CC')); $bcc = $form->add('text', 'bcc_id', ts('BCC')); $elements = array('cc', 'bcc'); foreach ($elements as $element) { if (${$element}->getValue()) { preg_match_all('!"(.*?)"\\s+<\\s*(.*?)\\s*>!', ${$element}->getValue(), $matches); $elementValues = array(); for ($i = 0; $i < count($matches[0]); $i++) { $name = '"' . $matches[1][$i] . '" <' . $matches[2][$i] . '>'; $elementValues[] = array('name' => $name, 'id' => $matches[0][$i]); } $var = "{$element}Contact"; $form->assign($var, json_encode($elementValues)); } } // when form is submitted recompute contactIds $allToEmails = array(); if ($to->getValue()) { $allToEmails = explode(',', $to->getValue()); $form->_contactIds = array(); foreach ($allToEmails as $value) { list($contactId, $email) = explode('::', $value); $form->_contactIds[] = $contactId; $form->_toContactEmails[] = $email; } } if (is_array($form->_contactIds)) { $returnProperties = array('sort_name' => 1, 'email' => 1, 'do_not_email' => 1, 'on_hold' => 1, 'display_name' => 1, 'preferred_mail_format' => 1); require_once 'CRM/Mailing/BAO/Mailing.php'; list($form->_contactDetails) = CRM_Mailing_BAO_Mailing::getDetails($form->_contactIds, $returnProperties, false, false); // make a copy of all contact details $form->_allContactDetails = $form->_contactDetails; foreach ($form->_contactIds as $key => $contactId) { $value = $form->_contactDetails[$contactId]; if ($value['do_not_email'] || empty($value['email']) || $value['is_deceased'] || $value['on_hold']) { $suppressedEmails++; // unset contact details for contacts that we won't be sending email. This is prevent extra computation // during token evaluation etc. unset($form->_contactDetails[$contactId]); } else { if (empty($form->_toContactEmails)) { $email = $value['email']; } else { $email = $form->_toContactEmails[$key]; } $toArray[] = array('name' => '"' . $value['sort_name'] . '" <' . $email . '>', 'id' => "{$contactId}::{$email}"); } } if (empty($toArray)) { CRM_Core_Error::statusBounce(ts('Selected contact(s) do not have a valid email address, or communication preferences specify DO NOT EMAIL, or they are deceased or Primary email address is On Hold).')); } } $form->assign('toContact', json_encode($toArray)); $form->assign('suppressedEmails', $suppressedEmails); $form->assign('noEmails', $form->_noEmails); $session =& CRM_Core_Session::singleton(); $userID = $session->get('userID'); list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); if (!$fromEmail) { CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address')); } if (!trim($fromDisplayName)) { $fromDisplayName = $fromEmail; } $form->assign('totalSelectedContacts', count($form->_contactIds)); $from = "{$fromDisplayName} <{$fromEmail}>"; $form->_fromEmails = array('0' => $from) + CRM_Core_PseudoConstant::fromEmailAddress(); $form->add('text', 'subject', ts('Subject'), 'size=50 maxlength=254', true); $selectEmails = $form->_fromEmails; foreach (array_keys($selectEmails) as $k) { $selectEmails[$k] = htmlspecialchars($selectEmails[$k]); } $form->add('select', 'fromEmailAddress', ts('From'), $selectEmails, true); require_once "CRM/Mailing/BAO/Mailing.php"; CRM_Mailing_BAO_Mailing::commonCompose($form); // add attachments require_once 'CRM/Core/BAO/File.php'; CRM_Core_BAO_File::buildAttachment($form, null); if ($form->_single) { // also fix the user context stack if ($form->_caseId) { $ccid = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $form->_caseId, 'contact_id', 'case_id'); $url = CRM_Utils_System::url('civicrm/contact/view/case', "&reset=1&action=view&cid={$ccid}&id={$form->_caseId}"); } else { if ($form->_context) { $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1'); } else { $url = CRM_Utils_System::url('civicrm/contact/view', "&show=1&action=browse&cid={$form->_contactIds[0]}&selectedChild=activity"); } } $session->replaceUserContext($url); $form->addDefaultButtons(ts('Send Email'), 'upload', 'cancel'); } else { $form->addDefaultButtons(ts('Send Email'), 'upload'); } $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); }
/** * Get the all From Email Address from database. * * The static array $fromEmailAddress is returned, and if it's * called the first time, DAO is used * to get all the From Email Address * * Note: any database errors will be trapped by the DAO. * * @access public * @static * * @return array - array reference of all From Email Address. */ public static function &fromEmailAddress() { if (!self::$fromEmailAddress) { require_once 'CRM/Core/OptionGroup.php'; self::$fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address'); } return self::$fromEmailAddress; }
static function preProcessFromAddress(&$form) { $form->_single = false; $className = CRM_Utils_System::getClassName($form); if (property_exists($form, '_context') && $form->_context != 'search' && $className == 'CRM_Contact_Form_Task_Email') { $form->_single = true; } $form->_emails = $emails = array(); $session = CRM_Core_Session::singleton(); $contactID = $session->get('userID'); $form->_contactIds = array($contactID); $contactEmails = CRM_Core_BAO_Email::allEmails($contactID); $form->_onHold = array(); $fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name'); foreach ($contactEmails as $emailId => $item) { $email = $item['email']; if (!$email && count($emails) <= 1) { $emails[$emailId] = '"' . $fromDisplayName . '"'; $form->_noEmails = true; } else { if ($email) { if (in_array($email, $emails)) { // CRM-3624 continue; } $emails[$emailId] = '"' . $fromDisplayName . '" <' . $email . '> '; $form->_onHold[$emailId] = $item['on_hold']; } } $form->_emails[$emailId] = $emails[$emailId]; $emails[$emailId] .= $item['locationType']; if ($item['is_primary']) { $emails[$emailId] .= ' ' . ts('(preferred)'); } $emails[$emailId] = htmlspecialchars($emails[$emailId]); } $form->assign('noEmails', $form->_noEmails); if ($form->_noEmails) { CRM_Core_Error::statusBounce(ts('Your user record does not have a valid email address')); } // now add domain from addresses $domainEmails = array(); $domainFrom = CRM_Core_PseudoConstant::fromEmailAddress(); foreach (array_keys($domainFrom) as $k) { $domainEmail = $domainFrom[$k]; $domainEmails[$domainEmail] = htmlspecialchars($domainEmail); $form->_emails[$domainEmail] = $domainEmail; } $form->_fromEmails = CRM_Utils_Array::crmArrayMerge($emails, $domainEmails); }
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 (CRM_Utils_Array::value($key, $formValues) || 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']) { foreach ($composeFields as $key) { if (CRM_Utils_Array::value($key, $formValues)) { $composeParams[$key] = $formValues[$key]; $this->set($key, $formValues[$key]); } } if (CRM_Utils_Array::value('updateTemplate', $composeParams)) { $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_MessageTemplates::add($templateParams); } if (CRM_Utils_Array::value('saveTemplate', $composeParams)) { $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_MessageTemplates::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. $formEmailAddress = CRM_Utils_Array::value($formValues['from_email_address'], CRM_Core_PseudoConstant::fromEmailAddress('from_email_address')); //get the from email address require_once 'CRM/Utils/Mail.php'; $params['from_email'] = CRM_Utils_Mail::pluckEmailFromHeader($formEmailAddress); //get the from Name $params['from_name'] = CRM_Utils_Array::value(1, explode('"', $formEmailAddress)); /* Build the mailing object */ require_once 'CRM/Mailing/BAO/Mailing.php'; CRM_Mailing_BAO_Mailing::create($params, $ids); if ($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'; } else { if ($this->_action == CRM_Core_Action::PROFILE) { $fragment = 'search/builder'; } else { if ($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("Your mailing has been saved. 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); //replace user context to search. $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); CRM_Utils_System::redirect($url); } else { $status = ts("Your mailing has been saved. Click the 'Continue' action to resume working on it."); CRM_Core_Session::setStatus($status); $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1'); CRM_Utils_System::redirect($url); } } }
/** * Get the all From Email Address from database. * * The static array $fromEmailAddress is returned, and if it's * called the first time, DAO is used * to get all the From Email Address * * Note: any database errors will be trapped by the DAO. * * @access public * @static * * @return array - array reference of all From Email Address. */ public static function &fromEmailAddress() { if (!self::$fromEmailAddress) { self::$fromEmailAddress = CRM_Core_OptionGroup::values('from_email_address'); } return self::$fromEmailAddress; }