Merge pull request #3845 from colemanw/contactID
[civicrm-core.git] / CRM / Utils / Mail.php
index e5e56acabb6f254e216be3262549f8b345fb55bd..82b89406f0252c18faf420141cb5f911e73788fa 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -72,10 +72,7 @@ class CRM_Utils_Mail {
     CRM_Utils_Hook::alterMailParams($params);
 
     // check if any module has aborted mail sending
-    if (
-      CRM_Utils_Array::value('abortMailSend', $params) ||
-      !CRM_Utils_Array::value('toEmail', $params)
-    ) {
+    if (!empty($params['abortMailSend']) || empty($params['toEmail'])) {
       return FALSE;
     }
 
@@ -90,7 +87,7 @@ class CRM_Utils_Mail {
 
     $headers         = array();
     // CRM-10699 support custom email headers
-    if (CRM_Utils_Array::value('headers', $params)) {
+    if (!empty($params['headers'])) {
       $headers = array_merge($headers, $params['headers']);
     }
     $headers['From'] = $params['from'];
@@ -118,7 +115,7 @@ class CRM_Utils_Mail {
     if ($includeMessageId) {
       $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>";
     }
-    if (CRM_Utils_Array::value('autoSubmitted', $params)) {
+    if (!empty($params['autoSubmitted'])) {
       $headers['Auto-Submitted'] = "Auto-Generated";
     }
 
@@ -169,19 +166,18 @@ class CRM_Utils_Mail {
     // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails
     // be included in both $to and $headers['Cc', 'Bcc']
     if (get_class($mailer) != "Mail_mail") {
-        //get emails from headers, since these are 
+        //get emails from headers, since these are
         //combination of name and email addresses.
-        if ( CRM_Utils_Array::value( 'Cc', $headers ) ) {
+        if (!empty($headers['Cc'])) {
             $to[] = CRM_Utils_Array::value( 'Cc', $headers );
         }
-        if ( CRM_Utils_Array::value( 'Bcc', $headers ) ) {
+        if (!empty($headers['Bcc'])) {
             $to[] = CRM_Utils_Array::value( 'Bcc', $headers );
         }
     }
     if (is_object($mailer)) {
-      CRM_Core_Error::ignoreException();
+      $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
       $result = $mailer->send($to, $headers, $message);
-      CRM_Core_Error::setCallback();
       if (is_a($result, 'PEAR_Error')) {
         $message = self::errorMessage($mailer, $result);
         // append error message in case multiple calls are being made to
@@ -196,6 +192,12 @@ class CRM_Utils_Mail {
     return FALSE;
   }
 
+  /**
+   * @param $mailer
+   * @param $result
+   *
+   * @return string
+   */
   static function errorMessage($mailer, $result) {
     $message = '<p>' . ts('An error occurred when CiviCRM attempted to send an email (via %1). If you received this error after submitting on online contribution or event registration - the transaction was completed, but we were unable to send the email receipt.', array(
       1 => 'SMTP')) . '</p>' . '<p>' . ts('The mail library returned the following error message:') . '<br /><span class="font-red"><strong>' . $result->getMessage() . '</strong></span></p>' . '<p>' . ts('This is probably related to a problem in your Outbound Email Settings (Administer CiviCRM &raquo; System Settings &raquo; Outbound Email), OR the FROM email address specifically configured for your contribution page or event. Possible causes are:') . '</p>';
@@ -213,6 +215,11 @@ class CRM_Utils_Mail {
     return $message;
   }
 
+  /**
+   * @param $to
+   * @param $headers
+   * @param $message
+   */
   static function logger(&$to, &$headers, &$message) {
     if (is_array($to)) {
       $toString = implode(', ', $to);
@@ -296,6 +303,12 @@ class CRM_Utils_Mail {
     return FALSE;
   }
 
+  /**
+   * @param $message
+   * @param null $params
+   *
+   * @return mixed
+   */
   static function &setMimeParams(&$message, $params = NULL) {
     static $mimeParams = NULL;
     if (!$params) {
@@ -313,6 +326,13 @@ class CRM_Utils_Mail {
     return $message->get($params);
   }
 
+  /**
+   * @param $name
+   * @param $email
+   * @param bool $useQuote
+   *
+   * @return null|string
+   */
   static function formatRFC822Email($name, $email, $useQuote = FALSE) {
     $result = NULL;
 
@@ -367,5 +387,34 @@ class CRM_Utils_Mail {
 
     return $name;
   }
+
+  /**
+   *
+   * @param string $fileName
+   * @param string $html
+   * @param string $format
+   *
+   * @return array $attachments
+   */
+  static function appendPDF($fileName, $html, $format = NULL) {
+    $pdf_filename = CRM_Core_Config::singleton()->templateCompileDir . CRM_Utils_File::makeFileName($fileName);
+
+    //FIXME : CRM-7894
+    //xmlns attribute is required in XHTML but it is invalid in HTML,
+    //Also the namespace "xmlns=http://www.w3.org/1999/xhtml" is default,
+    //and will be added to the <html> tag even if you do not include it.
+    $html = preg_replace('/(<html)(.+?xmlns=["\'].[^\s]+["\'])(.+)?(>)/', '\1\3\4', $html);
+
+    file_put_contents($pdf_filename, CRM_Utils_PDF_Utils::html2pdf($html,
+      $fileName,
+      TRUE,
+      $format)
+    );
+    return array(
+      'fullPath' => $pdf_filename,
+      'mime_type' => 'application/pdf',
+      'cleanName' => $fileName,
+    );
+  }
 }