/** * 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 & entities in text mode, so that the links work $text = str_replace('&', '&', $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; }
/** * Ask a contact for subscription confirmation (opt-in) * * @param string $email The email address * @return void * @access public */ function send_confirm_request($email) { $config =& CRM_Core_Config::singleton(); require_once 'CRM/Core/BAO/Domain.php'; $domain =& CRM_Core_BAO_Domain::getCurrentDomain(); require_once 'CRM/Utils/Verp.php'; $confirm = CRM_Utils_Verp::encode(implode($config->verpSeparator, array('confirm', $this->contact_id, $this->id, $this->hash)) . "@{$domain->email_domain}", $email); require_once 'CRM/Contact/BAO/Group.php'; $group =& new CRM_Contact_BAO_Group(); $group->id = $this->group_id; $group->find(true); require_once 'CRM/Mailing/BAO/Component.php'; $component =& new CRM_Mailing_BAO_Component(); $component->domain_id = $domain->id; $component->is_default = 1; $component->is_active = 1; $component->component_type = 'Subscribe'; $component->find(true); $headers = array('Subject' => $component->subject, 'From' => ts('"%1 Administrator" <do-not-reply@%2>', array(1 => $domain->name, 2 => $domain->email_domain)), 'To' => $email, 'Reply-To' => $confirm, 'Return-Path' => "do-not-reply@{$domain->email_domain}"); $html = $component->body_html; require_once 'CRM/Utils/Token.php'; $html = CRM_Utils_Token::replaceDomainTokens($html, $domain, true); $html = CRM_Utils_Token::replaceSubscribeTokens($html, $group->name, true); $text = $component->body_text; $text = CRM_Utils_Token::replaceDomainTokens($text, $domain, false); $text = CRM_Utils_Token::replaceSubscribeTokens($text, $group->name, false); $message =& new Mail_Mime("\n"); $message->setHTMLBody($html); $message->setTxtBody($text); $b = $message->get(); $h = $message->headers($headers); $mailer =& $config->getMailer(); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array('CRM_Mailing_BAO_Mailing', 'catchSMTP')); $mailer->send($email, $h, $b); CRM_Core_Error::setCallback(); }