From: Tim Otten Date: Tue, 18 Aug 2015 03:50:40 +0000 (-0700) Subject: CRM-16373 - Config - Move static $_mailer to Civi\Core\Container X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=247eb841d63517dbe2e985d0e1065f1ba6567701;p=civicrm-core.git CRM-16373 - Config - Move static $_mailer to Civi\Core\Container --- diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 55a60f5e01..c47361175a 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -133,13 +133,6 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { */ private static $_log = NULL; - /** - * The handle on the mail handler that we are using - * - * @var object - */ - public static $_mail = NULL; - /** * We only need one instance of this object. So we use the singleton * pattern and cache the instance in this variable @@ -403,8 +396,6 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * Initialize the DataObject framework. - * - * @return void */ private function _initDAO() { CRM_Core_DAO::init($this->dsn); @@ -420,8 +411,6 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * Returns the singleton logger for the application. * - * @param - * * @return object */ static public function &getLog() { @@ -535,100 +524,11 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * Retrieve a mailer to send any mail from the application. * - * @param bool $persist - * Open a persistent smtp connection, should speed up mailings. - * @return object + * @return Mail + * @deprecated */ - public static function &getMailer($persist = FALSE) { - if (!isset(self::$_mail)) { - $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, - 'mailing_backend' - ); - - if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || - (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) - ) { - self::$_mail = self::_createMailer('CRM_Mailing_BAO_Spool', array()); - } - elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { - if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { - CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - } - - $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; - $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; - - if ($mailingInfo['smtpAuth']) { - $params['username'] = $mailingInfo['smtpUsername']; - $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); - $params['auth'] = TRUE; - } - else { - $params['auth'] = FALSE; - } - - // set the localhost value, CRM-3153 - $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost'); - - // also set the timeout value, lets set it to 30 seconds - // CRM-7510 - $params['timeout'] = 30; - - // CRM-9349 - $params['persist'] = $persist; - - self::$_mail = self::_createMailer('smtp', $params); - } - elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { - if ($mailingInfo['sendmail_path'] == '' || - !$mailingInfo['sendmail_path'] - ) { - CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - } - $params['sendmail_path'] = $mailingInfo['sendmail_path']; - $params['sendmail_args'] = $mailingInfo['sendmail_args']; - - self::$_mail = self::_createMailer('sendmail', $params); - } - elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { - self::$_mail = self::_createMailer('mail', array()); - } - elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { - self::$_mail = self::_createMailer('mock', array()); - } - elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { - CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - CRM_Core_Session::setStatus(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - } - else { - CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or sendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); - CRM_Core_Error::debug_var('mailing_info', $mailingInfo); - } - } - return self::$_mail; - } - - /** - * Create a new instance of a PEAR Mail driver. - * - * @param string $driver - * 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory(). - * @param array $params - * @return object - * More specifically, a class which implements the "send()" function - */ - public static function _createMailer($driver, $params) { - if ($driver == 'CRM_Mailing_BAO_Spool') { - $mailer = new CRM_Mailing_BAO_Spool($params); - } - else { - $mailer = Mail::factory($driver, $params); - } - CRM_Utils_Hook::alterMail($mailer, $driver, $params); - return $mailer; + public static function getMailer() { + return Civi\Core\Container::singleton()->get('pear_mail'); } /** diff --git a/CRM/Mailing/BAO/MailingJob.php b/CRM/Mailing/BAO/MailingJob.php index 9bbe670b83..66f4b59b44 100644 --- a/CRM/Mailing/BAO/MailingJob.php +++ b/CRM/Mailing/BAO/MailingJob.php @@ -184,9 +184,8 @@ class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob { } // Get the mailer - // make it a persistent connection, CRM-9349 if ($mode === NULL) { - $mailer = $config->getMailer(TRUE); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); } elseif ($mode == 'sms') { $mailer = CRM_SMS_Provider::singleton(array('mailing_id' => $job->mailing_id)); diff --git a/CRM/Mailing/Event/BAO/Forward.php b/CRM/Mailing/Event/BAO/Forward.php index 2f99b11157..ec6487f1d6 100644 --- a/CRM/Mailing/Event/BAO/Forward.php +++ b/CRM/Mailing/Event/BAO/Forward.php @@ -172,7 +172,7 @@ class CRM_Mailing_Event_BAO_Forward extends CRM_Mailing_Event_DAO_Forward { $mailing_obj->find(TRUE); $config = CRM_Core_Config::singleton(); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); $recipient = NULL; $attachments = NULL; diff --git a/CRM/Mailing/Event/BAO/Reply.php b/CRM/Mailing/Event/BAO/Reply.php index 22215bbb9f..eae50e1c98 100644 --- a/CRM/Mailing/Event/BAO/Reply.php +++ b/CRM/Mailing/Event/BAO/Reply.php @@ -201,7 +201,7 @@ class CRM_Mailing_Event_BAO_Reply extends CRM_Mailing_Event_DAO_Reply { CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'r', $eq->job_id, $queue_id, $eq->hash); $config = CRM_Core_Config::singleton(); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); @@ -293,7 +293,7 @@ class CRM_Mailing_Event_BAO_Reply extends CRM_Mailing_Event_DAO_Reply { $h = $message->headers($headers); CRM_Mailing_BAO_Mailing::addMessageIdHeader($h, 'a', $eq->job_id, queue_id, $eq->hash); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); $mailer->send($to, $h, $b); diff --git a/CRM/Mailing/Event/BAO/Resubscribe.php b/CRM/Mailing/Event/BAO/Resubscribe.php index 6ab757d522..ef3711e448 100644 --- a/CRM/Mailing/Event/BAO/Resubscribe.php +++ b/CRM/Mailing/Event/BAO/Resubscribe.php @@ -287,7 +287,7 @@ class CRM_Mailing_Event_BAO_Resubscribe { $b = CRM_Utils_Mail::setMimeParams($message); $h = $message->headers($headers); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); diff --git a/CRM/Mailing/Event/BAO/Subscribe.php b/CRM/Mailing/Event/BAO/Subscribe.php index c3d4b04853..7d0acc4487 100644 --- a/CRM/Mailing/Event/BAO/Subscribe.php +++ b/CRM/Mailing/Event/BAO/Subscribe.php @@ -291,7 +291,7 @@ SELECT civicrm_email.id as email_id $this->id, $this->hash ); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); diff --git a/CRM/Mailing/Event/BAO/Unsubscribe.php b/CRM/Mailing/Event/BAO/Unsubscribe.php index 03ae2bb903..4c105aae50 100755 --- a/CRM/Mailing/Event/BAO/Unsubscribe.php +++ b/CRM/Mailing/Event/BAO/Unsubscribe.php @@ -406,7 +406,7 @@ WHERE email = %2 $b = CRM_Utils_Mail::setMimeParams($message); $h = $message->headers($headers); - $mailer = $config->getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); if (is_object($mailer)) { $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index d2c1b8e88d..37d304e3ed 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -34,6 +34,106 @@ */ class CRM_Utils_Mail { + /** + * Create a new mailer to send any mail from the application. + * + * Note: The mailer is opened in persistent mode. + * + * Note: You probably don't want to call this directly. Get a reference + * to the mailer through the container. + * + * @return Mail + */ + public static function createMailer() { + $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, + 'mailing_backend' + ); + + if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || + (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) + ) { + $mailer = self::_createMailer('CRM_Mailing_BAO_Spool', array()); + } + elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SMTP) { + if ($mailingInfo['smtpServer'] == '' || !$mailingInfo['smtpServer']) { + CRM_Core_Error::debug_log_message(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + CRM_Core_Error::fatal(ts('There is no valid smtp server setting. Click Administer >> System Setting >> Outbound Email to set the SMTP Server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + } + + $params['host'] = $mailingInfo['smtpServer'] ? $mailingInfo['smtpServer'] : 'localhost'; + $params['port'] = $mailingInfo['smtpPort'] ? $mailingInfo['smtpPort'] : 25; + + if ($mailingInfo['smtpAuth']) { + $params['username'] = $mailingInfo['smtpUsername']; + $params['password'] = CRM_Utils_Crypt::decrypt($mailingInfo['smtpPassword']); + $params['auth'] = TRUE; + } + else { + $params['auth'] = FALSE; + } + + // set the localhost value, CRM-3153 + $params['localhost'] = CRM_Utils_Array::value('SERVER_NAME', $_SERVER, 'localhost'); + + // also set the timeout value, lets set it to 30 seconds + // CRM-7510 + $params['timeout'] = 30; + + // CRM-9349 + $params['persist'] = TRUE; + + $mailer = self::_createMailer('smtp', $params); + } + elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_SENDMAIL) { + if ($mailingInfo['sendmail_path'] == '' || + !$mailingInfo['sendmail_path'] + ) { + CRM_Core_Error::debug_log_message(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + CRM_Core_Error::fatal(ts('There is no valid sendmail path setting. Click Administer >> System Setting >> Outbound Email to set the sendmail server.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + } + $params['sendmail_path'] = $mailingInfo['sendmail_path']; + $params['sendmail_args'] = $mailingInfo['sendmail_args']; + + $mailer = self::_createMailer('sendmail', $params); + } + elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MAIL) { + $mailer = self::_createMailer('mail', array()); + } + elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { + $mailer = self::_createMailer('mock', array()); + } + elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { + CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + CRM_Core_Session::setStatus(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + } + else { + CRM_Core_Error::debug_log_message(ts('There is no valid SMTP server Setting Or SendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + CRM_Core_Session::setStatus(ts('There is no valid SMTP server Setting Or sendMail path setting. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')))); + CRM_Core_Error::debug_var('mailing_info', $mailingInfo); + } + return $mailer; + } + + /** + * Create a new instance of a PEAR Mail driver. + * + * @param string $driver + * 'CRM_Mailing_BAO_Spool' or a name suitable for Mail::factory(). + * @param array $params + * @return object + * More specifically, a class which implements the "send()" function + */ + public static function _createMailer($driver, $params) { + if ($driver == 'CRM_Mailing_BAO_Spool') { + $mailer = new CRM_Mailing_BAO_Spool($params); + } + else { + $mailer = Mail::factory($driver, $params); + } + CRM_Utils_Hook::alterMail($mailer, $driver, $params); + return $mailer; + } + /** * Wrapper function to send mail in CiviCRM. Hooks are called from this function. The input parameter * is an associateive array which holds the values of field needed to send an email. These are: @@ -160,7 +260,7 @@ class CRM_Utils_Mail { $to = array($params['toEmail']); $result = NULL; - $mailer =& CRM_Core_Config::getMailer(); + $mailer = \Civi\Core\Container::singleton()->get('pear_mail'); // Mail_smtp and Mail_sendmail mailers require Bcc anc Cc emails // be included in both $to and $headers['Cc', 'Bcc'] diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 627f1a18c9..526ee9463f 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -103,6 +103,9 @@ class Container { )) ->setFactoryClass('CRM_Cxn_BAO_Cxn')->setFactoryMethod('createRegistrationClient'); + $container->setDefinition('pear_mail', new Definition('Mail')) + ->setFactoryClass('CRM_Utils_Mail')->setFactoryMethod('createMailer'); + // Expose legacy singletons as services in the container. $singletons = array( 'resources' => 'CRM_Core_Resources', diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 860a66d2ac..5694673e56 100755 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -397,8 +397,8 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { // FIXME: look at it closer in second stage // initialize the object once db is loaded - CRM_Core_Config::$_mail = NULL; $config = CRM_Core_Config::singleton(); + Civi\Core\Container::singleton(TRUE); // when running unit tests, use mockup user framework $config->setUserFramework('UnitTests');