| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Utils_Mail { |
| 36 | |
| 37 | /** |
| 38 | * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter |
| 39 | * is an associateive array which holds the values of field needed to send an email. These are: |
| 40 | * |
| 41 | * from : complete from envelope |
| 42 | * toName : name of person to send email |
| 43 | * toEmail : email address to send to |
| 44 | * cc : email addresses to cc |
| 45 | * bcc : email addresses to bcc |
| 46 | * subject : subject of the email |
| 47 | * text : text of the message |
| 48 | * html : html version of the message |
| 49 | * replyTo : reply-to header in the email |
| 50 | * attachments: an associative array of |
| 51 | * fullPath : complete pathname to the file |
| 52 | * mime_type: mime type of the attachment |
| 53 | * cleanName: the user friendly name of the attachmment |
| 54 | * |
| 55 | * @param array $params (by reference) |
| 56 | * |
| 57 | * @access public |
| 58 | * |
| 59 | * @return boolean true if a mail was sent, else false |
| 60 | */ |
| 61 | static function send(&$params) { |
| 62 | $returnPath = CRM_Core_BAO_MailSettings::defaultReturnPath(); |
| 63 | $includeMessageId = CRM_Core_BAO_MailSettings::includeMessageId(); |
| 64 | $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); |
| 65 | $from = CRM_Utils_Array::value('from', $params); |
| 66 | if (!$returnPath) { |
| 67 | $returnPath = self::pluckEmailFromHeader($from); |
| 68 | } |
| 69 | $params['returnPath'] = $returnPath; |
| 70 | |
| 71 | // first call the mail alter hook |
| 72 | CRM_Utils_Hook::alterMailParams($params); |
| 73 | |
| 74 | // check if any module has aborted mail sending |
| 75 | if ( |
| 76 | CRM_Utils_Array::value('abortMailSend', $params) || |
| 77 | !CRM_Utils_Array::value('toEmail', $params) |
| 78 | ) { |
| 79 | return FALSE; |
| 80 | } |
| 81 | |
| 82 | $textMessage = CRM_Utils_Array::value('text', $params); |
| 83 | $htmlMessage = CRM_Utils_Array::value('html', $params); |
| 84 | $attachments = CRM_Utils_Array::value('attachments', $params); |
| 85 | |
| 86 | // CRM-6224 |
| 87 | if (trim(CRM_Utils_String::htmlToText($htmlMessage)) == '') { |
| 88 | $htmlMessage = FALSE; |
| 89 | } |
| 90 | |
| 91 | $headers = array(); |
| 92 | // CRM-10699 support custom email headers |
| 93 | if (CRM_Utils_Array::value('headers', $params)) { |
| 94 | $headers = array_merge($headers, $params['headers']); |
| 95 | } |
| 96 | $headers['From'] = $params['from']; |
| 97 | $headers['To'] = |
| 98 | self::formatRFC822Email( |
| 99 | CRM_Utils_Array::value('toName', $params), |
| 100 | CRM_Utils_Array::value('toEmail', $params), |
| 101 | FALSE |
| 102 | ); |
| 103 | $headers['Cc'] = CRM_Utils_Array::value('cc', $params); |
| 104 | $headers['Bcc'] = CRM_Utils_Array::value('bcc', $params); |
| 105 | $headers['Subject'] = CRM_Utils_Array::value('subject', $params); |
| 106 | $headers['Content-Type'] = $htmlMessage ? 'multipart/mixed; charset=utf-8' : 'text/plain; charset=utf-8'; |
| 107 | $headers['Content-Disposition'] = 'inline'; |
| 108 | $headers['Content-Transfer-Encoding'] = '8bit'; |
| 109 | $headers['Return-Path'] = CRM_Utils_Array::value('returnPath', $params); |
| 110 | |
| 111 | // CRM-11295: Omit reply-to headers if empty; this avoids issues with overzealous mailservers |
| 112 | $replyTo = CRM_Utils_Array::value('replyTo', $params, $from); |
| 113 | |
| 114 | if (!empty($replyTo)) { |
| 115 | $headers['Reply-To'] = $replyTo; |
| 116 | } |
| 117 | $headers['Date'] = date('r'); |
| 118 | if ($includeMessageId) { |
| 119 | $headers['Message-ID'] = '<' . uniqid('civicrm_', TRUE) . "@$emailDomain>"; |
| 120 | } |
| 121 | if (CRM_Utils_Array::value('autoSubmitted', $params)) { |
| 122 | $headers['Auto-Submitted'] = "Auto-Generated"; |
| 123 | } |
| 124 | |
| 125 | //make sure we has to have space, CRM-6977 |
| 126 | foreach (array('From', 'To', 'Cc', 'Bcc', 'Reply-To', 'Return-Path') as $fld) { |
| 127 | if (isset($headers[$fld])) { |
| 128 | $headers[$fld] = str_replace('"<', '" <', $headers[$fld]); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // quote FROM, if comma is detected AND is not already quoted. CRM-7053 |
| 133 | if (strpos($headers['From'], ',') !== FALSE) { |
| 134 | $from = explode(' <', $headers['From']); |
| 135 | $headers['From'] = self::formatRFC822Email( |
| 136 | $from[0], |
| 137 | substr(trim($from[1]), 0, -1), |
| 138 | TRUE |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | require_once 'Mail/mime.php'; |
| 143 | $msg = new Mail_mime("\n"); |
| 144 | if ($textMessage) { |
| 145 | $msg->setTxtBody($textMessage); |
| 146 | } |
| 147 | |
| 148 | if ($htmlMessage) { |
| 149 | $msg->setHTMLBody($htmlMessage); |
| 150 | } |
| 151 | |
| 152 | if (!empty($attachments)) { |
| 153 | foreach ($attachments as $fileID => $attach) { |
| 154 | $msg->addAttachment( |
| 155 | $attach['fullPath'], |
| 156 | $attach['mime_type'], |
| 157 | $attach['cleanName'] |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $message = self::setMimeParams($msg); |
| 163 | $headers = &$msg->headers($headers); |
| 164 | |
| 165 | $to = array($params['toEmail']); |
| 166 | $result = null; |
| 167 | $mailer =& CRM_Core_Config::getMailer( ); |
| 168 | |
| 169 | // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails |
| 170 | // be included in both $to and $headers['Cc', 'Bcc'] |
| 171 | if (get_class($mailer) != "Mail_mail") { |
| 172 | //get emails from headers, since these are |
| 173 | //combination of name and email addresses. |
| 174 | if ( CRM_Utils_Array::value( 'Cc', $headers ) ) { |
| 175 | $to[] = CRM_Utils_Array::value( 'Cc', $headers ); |
| 176 | } |
| 177 | if ( CRM_Utils_Array::value( 'Bcc', $headers ) ) { |
| 178 | $to[] = CRM_Utils_Array::value( 'Bcc', $headers ); |
| 179 | } |
| 180 | } |
| 181 | if (is_object($mailer)) { |
| 182 | CRM_Core_Error::ignoreException(); |
| 183 | $result = $mailer->send($to, $headers, $message); |
| 184 | CRM_Core_Error::setCallback(); |
| 185 | if (is_a($result, 'PEAR_Error')) { |
| 186 | $message = self::errorMessage($mailer, $result); |
| 187 | // append error message in case multiple calls are being made to |
| 188 | // this method in the course of sending a batch of messages. |
| 189 | CRM_Core_Session::setStatus($message, ts('Mailing Error'), 'error'); |
| 190 | return FALSE; |
| 191 | } |
| 192 | // CRM-10699 |
| 193 | CRM_Utils_Hook::postEmailSend($params); |
| 194 | return TRUE; |
| 195 | } |
| 196 | return FALSE; |
| 197 | } |
| 198 | |
| 199 | static function errorMessage($mailer, $result) { |
| 200 | $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( |
| 201 | 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 » System Settings » Outbound Email), OR the FROM email address specifically configured for your contribution page or event. Possible causes are:') . '</p>'; |
| 202 | |
| 203 | if (is_a($mailer, 'Mail_smtp')) { |
| 204 | $message .= '<ul>' . '<li>' . ts('Your SMTP Username or Password are incorrect.') . '</li>' . '<li>' . ts('Your SMTP Server (machine) name is incorrect.') . '</li>' . '<li>' . ts('You need to use a Port other than the default port 25 in your environment.') . '</li>' . '<li>' . ts('Your SMTP server is just not responding right now (it is down for some reason).') . '</li>'; |
| 205 | } |
| 206 | else { |
| 207 | $message .= '<ul>' . '<li>' . ts('Your Sendmail path is incorrect.') . '</li>' . '<li>' . ts('Your Sendmail argument is incorrect.') . '</li>'; |
| 208 | } |
| 209 | |
| 210 | $message .= '<li>' . ts('The FROM Email Address configured for this feature may not be a valid sender based on your email service provider rules.') . '</li>' . '</ul>' . '<p>' . ts('Check <a href="%1">this page</a> for more information.', array( |
| 211 | 1 => CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration', TRUE))) . '</p>'; |
| 212 | |
| 213 | return $message; |
| 214 | } |
| 215 | |
| 216 | static function logger(&$to, &$headers, &$message) { |
| 217 | if (is_array($to)) { |
| 218 | $toString = implode(', ', $to); |
| 219 | $fileName = $to[0]; |
| 220 | } |
| 221 | else { |
| 222 | $toString = $fileName = $to; |
| 223 | } |
| 224 | $content = "To: " . $toString . "\n"; |
| 225 | foreach ($headers as $key => $val) { |
| 226 | $content .= "$key: $val\n"; |
| 227 | } |
| 228 | $content .= "\n" . $message . "\n"; |
| 229 | |
| 230 | if (is_numeric(CIVICRM_MAIL_LOG)) { |
| 231 | $config = CRM_Core_Config::singleton(); |
| 232 | // create the directory if not there |
| 233 | $dirName = $config->configAndLogDir . 'mail' . DIRECTORY_SEPARATOR; |
| 234 | CRM_Utils_File::createDir($dirName); |
| 235 | $fileName = md5(uniqid(CRM_Utils_String::munge($fileName))) . '.txt'; |
| 236 | file_put_contents($dirName . $fileName, |
| 237 | $content |
| 238 | ); |
| 239 | } |
| 240 | else { |
| 241 | file_put_contents(CIVICRM_MAIL_LOG, $content, FILE_APPEND); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get the email address itself from a formatted full name + address string |
| 247 | * |
| 248 | * Ugly but working. |
| 249 | * |
| 250 | * @param string $header the full name + email address string |
| 251 | * |
| 252 | * @return string the plucked email address |
| 253 | * @static |
| 254 | */ |
| 255 | static function pluckEmailFromHeader($header) { |
| 256 | preg_match('/<([^<]*)>$/', $header, $matches); |
| 257 | |
| 258 | if (isset($matches[1])) { |
| 259 | return $matches[1]; |
| 260 | } |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get the Active outBound email |
| 266 | * |
| 267 | * @return boolean true if valid outBound email configuration found, false otherwise |
| 268 | * @access public |
| 269 | * @static |
| 270 | */ |
| 271 | static function validOutBoundMail() { |
| 272 | $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, |
| 273 | 'mailing_backend' |
| 274 | ); |
| 275 | if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { |
| 276 | return TRUE; |
| 277 | } |
| 278 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { |
| 279 | if (!isset($mailingInfo['smtpServer']) || $mailingInfo['smtpServer'] == '' || |
| 280 | $mailingInfo['smtpServer'] == 'YOUR SMTP SERVER' || |
| 281 | ($mailingInfo['smtpAuth'] && ($mailingInfo['smtpUsername'] == '' || $mailingInfo['smtpPassword'] == '')) |
| 282 | ) { |
| 283 | return FALSE; |
| 284 | } |
| 285 | return TRUE; |
| 286 | } |
| 287 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { |
| 288 | if (!$mailingInfo['sendmail_path'] || !$mailingInfo['sendmail_args']) { |
| 289 | return FALSE; |
| 290 | } |
| 291 | return TRUE; |
| 292 | } |
| 293 | elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB) { |
| 294 | return TRUE; |
| 295 | } |
| 296 | return FALSE; |
| 297 | } |
| 298 | |
| 299 | static function &setMimeParams(&$message, $params = NULL) { |
| 300 | static $mimeParams = NULL; |
| 301 | if (!$params) { |
| 302 | if (!$mimeParams) { |
| 303 | $mimeParams = array( |
| 304 | 'text_encoding' => '8bit', |
| 305 | 'html_encoding' => '8bit', |
| 306 | 'head_charset' => 'utf-8', |
| 307 | 'text_charset' => 'utf-8', |
| 308 | 'html_charset' => 'utf-8', |
| 309 | ); |
| 310 | } |
| 311 | $params = $mimeParams; |
| 312 | } |
| 313 | return $message->get($params); |
| 314 | } |
| 315 | |
| 316 | static function formatRFC822Email($name, $email, $useQuote = FALSE) { |
| 317 | $result = NULL; |
| 318 | |
| 319 | $name = trim($name); |
| 320 | |
| 321 | // strip out double quotes if present at the beginning AND end |
| 322 | if (substr($name, 0, 1) == '"' && |
| 323 | substr($name, -1, 1) == '"' |
| 324 | ) { |
| 325 | $name = substr($name, 1, -1); |
| 326 | } |
| 327 | |
| 328 | if (!empty($name)) { |
| 329 | // escape the special characters |
| 330 | $name = str_replace(array('<', '"', '>'), |
| 331 | array('\<', '\"', '\>'), |
| 332 | $name |
| 333 | ); |
| 334 | if (strpos($name, ',') !== FALSE || |
| 335 | $useQuote |
| 336 | ) { |
| 337 | // quote the string if it has a comma |
| 338 | $name = '"' . $name . '"'; |
| 339 | } |
| 340 | |
| 341 | $result = "$name "; |
| 342 | } |
| 343 | |
| 344 | $result .= "<{$email}>"; |
| 345 | return $result; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Takes a string and checks to see if it needs to be escaped / double quoted |
| 350 | * and if so does the needful and return the formatted name |
| 351 | * |
| 352 | * This code has been copied and adapted from ezc/Mail/src/tools.php |
| 353 | */ |
| 354 | static function formatRFC2822Name($name) { |
| 355 | $name = trim($name); |
| 356 | if (!empty($name)) { |
| 357 | // remove the quotes around the name part if they are already there |
| 358 | if (substr($name, 0, 1) == '"' && substr($name, -1) == '"') { |
| 359 | $name = substr($name, 1, -1); |
| 360 | } |
| 361 | |
| 362 | // add slashes to " and \ and surround the name part with quotes |
| 363 | if (strpbrk($name, ",@<>:;'\"") !== FALSE) { |
| 364 | $name = '"' . addcslashes($name, '\\"') . '"'; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return $name; |
| 369 | } |
| 370 | } |
| 371 | |