/**
  * Return the domain BAO for the current domain.
  *
  * @param NULL
  * 
  * @return  object  CRM_Core_BAO_Domain object
  * 
  * @access public
  * @static
  */
 function &getCurrentDomain()
 {
     if ($GLOBALS['_CRM_CORE_BAO_DOMAIN']['_domain'] == null) {
         $GLOBALS['_CRM_CORE_BAO_DOMAIN']['_domain'] =& CRM_Core_BAO_Domain::getDomainByID(CRM_Core_Config::domainID());
     }
     return $GLOBALS['_CRM_CORE_BAO_DOMAIN']['_domain'];
 }
 /**
  * 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 boolean $test         Is this mailing a test?
  * @return object               The mail object
  * @access public
  */
 function &compose($job_id, $event_queue_id, $hash, $contactId, $email, &$recipient, $test = false)
 {
     if ($test) {
         $job_id = 'JOB';
         $event_queue_id = 'QUEUE';
         $hash = 'HASH';
     }
     if ($this->_domain == null) {
         require_once 'CRM/Core/BAO/Domain.php';
         $this->_domain =& CRM_Core_BAO_Domain::getDomainByID($this->domain_id);
     }
     /**
      * Inbound VERP keys:
      *  reply:          user replied to mailing
      *  bounce:         email address bounced
      *  unsubscribe:    contact opts out of all target lists for the mailing
      *  optOut:         contact unsubscribes from the domain
      */
     $config =& CRM_Core_Config::singleton();
     foreach (array('reply', 'bounce', 'unsubscribe', 'optOut') as $key) {
         $verp[$key] = implode($config->verpSeparator, array($key, $job_id, $event_queue_id, $hash)) . '@' . $this->_domain->email_domain;
     }
     $urls = array('forward' => CRM_Utils_System::url('civicrm/mailing/forward', "reset=1&jid={$job_id}&qid={$event_queue_id}&h={$hash}", true));
     $headers = array('Reply-To' => CRM_Utils_Verp::encode($verp['reply'], $email), 'Return-Path' => CRM_Utils_Verp::encode($verp['bounce'], $email), 'From' => "\"{$this->from_name}\" <{$this->from_email}>", 'Subject' => $this->subject);
     require_once 'CRM/Utils/Token.php';
     if ($this->html == null || $this->text == null) {
         $this->getHeaderFooter();
         if ($this->body_html) {
             $this->html = $this->header->body_html . '<br />' . $this->body_html . '<br />' . $this->footer->body_html;
             $this->html = CRM_Utils_Token::replaceDomainTokens($this->html, $this->_domain, true);
             $this->html = CRM_Utils_Token::replaceMailingTokens($this->html, $this, true);
         }
         $this->text = $this->header->body_text . "\n" . $this->body_text . "\n" . $this->footer->body_text;
         $this->text = CRM_Utils_Token::replaceDomainTokens($this->text, $this->_domain, false);
         $this->text = CRM_Utils_Token::replaceMailingTokens($this->text, $this, true);
     }
     $html = $this->html;
     $text = $this->text;
     if ($html && !$test && $this->url_tracking) {
         CRM_Mailing_BAO_TrackableURL::scan_and_replace($html, $this->id, $event_queue_id);
         CRM_Mailing_BAO_TrackableURL::scan_and_replace($text, $this->id, $event_queue_id);
     }
     $params = array('contact_id' => $contactId, 'id' => $contactId);
     $contact = array();
     $ids = array();
     CRM_Contact_BAO_Contact::retrieve($params, $contact, $ids);
     $message =& new Mail_Mime("\n");
     /* Do contact-specific token replacement in text mode, and add to the
      * message if necessary */
     if ($test || !$html || $contact['preferred_mail_format'] == 'Text' || $contact['preferred_mail_format'] == 'Both') {
         $text = CRM_Utils_Token::replaceContactTokens($text, $contact, false);
         $text = CRM_Utils_Token::replaceActionTokens($text, $verp, $urls, false);
         // render the &amp; entities in text mode, so that the links work
         $text = str_replace('&amp;', '&', $text);
         $message->setTxtBody($text);
     }
     /* Do contact-specific token replacement in html mode, and add to the
      * message if necessary */
     if ($html && ($test || $contact['preferred_mail_format'] == 'HTML' || $contact['preferred_mail_format'] == 'Both')) {
         $html = CRM_Utils_Token::replaceContactTokens($html, $contact, true);
         $html = CRM_Utils_Token::replaceActionTokens($html, $verp, $urls, true);
         if ($this->open_tracking) {
             $html .= '<img src="' . $config->userFrameworkResourceURL . "extern/open.php?q={$event_queue_id}\" width='1' height='1' alt='' border='0'>";
         }
         $message->setHTMLBody($html);
     }
     $recipient = "\"{$contact['display_name']}\" <{$email}>";
     $headers['To'] = $recipient;
     $mailMimeParams = array('text_encoding' => '8bit', 'html_encoding' => '8bit', 'head_charset' => 'utf-8', 'text_charset' => 'utf-8', 'html_charset' => 'utf-8');
     $message->get($mailMimeParams);
     $message->headers($headers);
     return $message;
 }