From 4b56952b38e94ae9e14b010b389e08fc167ee174 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 2 Apr 2019 09:41:29 +0200 Subject: [PATCH 1/9] Allow swiftmailer 6 (cherry picked from commit 3622f22abc06545ca8d7a482493af8729c2be060) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8e3a7c61b..b4d959ce6 100755 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "php" : ">=5.3.0", - "swiftmailer/swiftmailer": "~5.2" + "swiftmailer/swiftmailer": "~5.2|~6.0" }, "require-dev": { "psr/log": "*" From aeb69bb35451747d6f9703ffb6d9c12aa9aaeb16 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 2 Apr 2019 10:37:43 +0200 Subject: [PATCH 2/9] Allowed optional installation of swiftmailer 6 if plattform >= PHP 7. Submodule for swiftmailer not required as it is also a dependency within composer. (cherry picked from commit ba2dfe3d42dbeeb9236da59d996c2624676b28e7) --- .gitmodules | 4 ---- lib/mailer/sfMailer.class.php | 15 +++++++++++---- lib/vendor/swiftmailer | 1 - 3 files changed, 11 insertions(+), 9 deletions(-) delete mode 160000 lib/vendor/swiftmailer diff --git a/.gitmodules b/.gitmodules index f9e1e7496..ed98e74cd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,3 @@ -[submodule "lib/vendor/swiftmailer"] - path = lib/vendor/swiftmailer - url = https://github.com/swiftmailer/swiftmailer.git - branch = 5.x [submodule "lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine"] path = lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine url = https://github.com/LExpress/doctrine1.git diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php index ee9ce93e8..99b0b836f 100755 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -239,10 +239,17 @@ public function setDeliveryAddress($address) */ public function compose($from = null, $to = null, $subject = null, $body = null) { - return Swift_Message::newInstance() + $msg = null; + + if(version_compare(Swift::VERSION, '6.0.0') >= 0) { + $msg = new Swift_Message($subject); + } else { + $msg = Swift_Message::newInstance($subject); + } + + return $msg ->setFrom($from) ->setTo($to) - ->setSubject($subject) ->setBody($body) ; } @@ -277,12 +284,12 @@ public function sendNextImmediately() /** * Sends the given message. * - * @param Swift_Transport $transport A transport instance + * @param Swift_Message $message A transport instance * @param string[] &$failedRecipients An array of failures by-reference * * @return int|false The number of sent emails */ - public function send(Swift_Mime_Message $message, &$failedRecipients = null) + public function send(Swift_Message $message, &$failedRecipients = null) { if ($this->force) { diff --git a/lib/vendor/swiftmailer b/lib/vendor/swiftmailer deleted file mode 160000 index 4cc928420..000000000 --- a/lib/vendor/swiftmailer +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4cc92842069c2bbc1f28daaaf1d2576ec4dfe153 From 6c4e9672b35a6ce3a44e028ec6a9ec2da76cd300 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Mon, 7 Sep 2020 09:50:18 +0200 Subject: [PATCH 3/9] Swiftmailer6 compat. (cherry picked from commit 2e00caa1c3d9bb921578e3c3341247b219a179f5) --- autoload.php | 5 + lib/mailer/sfMailer.class.php | 308 ++-------------------- lib/mailer/sfMailerBase.class.php | 292 ++++++++++++++++++++ lib/mailer/sfMailerSwiftmailer6.class.php | 56 ++++ 4 files changed, 368 insertions(+), 293 deletions(-) mode change 100755 => 100644 lib/mailer/sfMailer.class.php create mode 100755 lib/mailer/sfMailerBase.class.php create mode 100644 lib/mailer/sfMailerSwiftmailer6.class.php diff --git a/autoload.php b/autoload.php index 3f223a024..62f2f2088 100755 --- a/autoload.php +++ b/autoload.php @@ -3,3 +3,8 @@ require_once __DIR__.'/lib/autoload/sfCoreAutoload.class.php'; sfCoreAutoload::register(); + +// Register sfMailerSwiftmailer6 as default sfMailer, if Swiftmailer ~6.0 has been loaded. +if(version_compare(Swift::VERSION, '6.0.0') >= 0) { + sfConfig::set('sf_factory_mailer', 'sfMailerSwiftmailer6'); +} diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php old mode 100755 new mode 100644 index 99b0b836f..0e351b764 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -1,295 +1,26 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - /** * sfMailer is the main entry point for the mailer system. * - * This class is instanciated by sfContext on demand. + * This class is instanciated by sfContext on demand and compatible to swiftmailer ~5.2 * * @package symfony * @subpackage mailer - * @author Fabien Potencier + * @author Thomas A. Hirsch * @version SVN: $Id$ */ -class sfMailer extends Swift_Mailer +class sfMailer extends sfMailerBase { - const - REALTIME = 'realtime', - SPOOL = 'spool', - SINGLE_ADDRESS = 'single_address', - NONE = 'none'; - - protected - $spool = null, - $logger = null, - $strategy = 'realtime', - $address = '', - $realtimeTransport = null, - $force = false, - $redirectingPlugin = null; - - /** - * Constructor. - * - * Available options: - * - * * charset: The default charset to use for messages - * * logging: Whether to enable logging or not - * * delivery_strategy: The delivery strategy to use - * * spool_class: The spool class (for the spool strategy) - * * spool_arguments: The arguments to pass to the spool constructor - * * delivery_address: The email address to use for the single_address strategy - * * transport: The main transport configuration - * * * class: The main transport class - * * * param: The main transport parameters - * - * @param sfEventDispatcher $dispatcher An event dispatcher instance - * @param array $options An array of options - */ - public function __construct(sfEventDispatcher $dispatcher, $options) - { - // options - $options = array_merge(array( - 'charset' => 'UTF-8', - 'logging' => false, - 'delivery_strategy' => self::REALTIME, - 'transport' => array( - 'class' => 'Swift_MailTransport', - 'param' => array(), - ), - ), $options); - - $constantName = 'sfMailer::'.strtoupper($options['delivery_strategy']); - $this->strategy = defined($constantName) ? constant($constantName) : false; - if (!$this->strategy) - { - throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy'])); - } - - if (sfMailer::NONE == $this->strategy) - { - $options['transport']['class'] = 'Swift_NullTransport'; - } - - // transport - $class = $options['transport']['class']; - $transport = new $class(); - if (isset($options['transport']['param'])) - { - foreach ($options['transport']['param'] as $key => $value) - { - $method = 'set'.ucfirst($key); - if (method_exists($transport, $method)) - { - $transport->$method($value); - } - elseif (method_exists($transport, 'getExtensionHandlers')) - { - foreach ($transport->getExtensionHandlers() as $handler) - { - if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods()))) - { - $transport->$method($value); - } - } - } - } - } - $this->realtimeTransport = $transport; - - if (sfMailer::SPOOL == $this->strategy) - { - if (!isset($options['spool_class'])) - { - throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option'); - } - $arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array(); - - if ($arguments) - { - $r = new ReflectionClass($options['spool_class']); - $this->spool = $r->newInstanceArgs($arguments); - } - else - { - $this->spool = new $options['spool_class']; - } - - $transport = new Swift_SpoolTransport($this->spool); - } - elseif (sfMailer::SINGLE_ADDRESS == $this->strategy) - { - if (!isset($options['delivery_address'])) - { - throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option'); - } - - $this->address = $options['delivery_address']; - - $transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address)); - } - - parent::__construct($transport); - - // logger - if ($options['logging']) - { - $this->logger = new sfMailerMessageLoggerPlugin($dispatcher); - - $transport->registerPlugin($this->logger); - } - - // preferences - Swift_Preferences::getInstance()->setCharset($options['charset']); - - $dispatcher->notify(new sfEvent($this, 'mailer.configure')); - } - - /** - * Gets the realtime transport instance. - * - * @return Swift_Transport The realtime transport instance. - */ - public function getRealtimeTransport() - { - return $this->realtimeTransport; - } - - /** - * Sets the realtime transport instance. - * - * @param Swift_Transport $transport The realtime transport instance. - */ - public function setRealtimeTransport(Swift_Transport $transport) - { - $this->realtimeTransport = $transport; - } - - /** - * Gets the logger instance. - * - * @return sfMailerMessageLoggerPlugin The logger instance. - */ - public function getLogger() - { - return $this->logger; - } - - /** - * Sets the logger instance. - * - * @param sfMailerMessageLoggerPlugin $logger The logger instance. - */ - public function setLogger($logger) - { - $this->logger = $logger; - } - - /** - * Gets the delivery strategy. - * - * @return string The delivery strategy - */ - public function getDeliveryStrategy() - { - return $this->strategy; - } - - /** - * Gets the delivery address. - * - * @return string The delivery address - */ - public function getDeliveryAddress() - { - return $this->address; - } - - /** - * Sets the delivery address. - * - * @param string $address The delivery address - */ - public function setDeliveryAddress($address) - { - $this->address = $address; - - if (sfMailer::SINGLE_ADDRESS == $this->strategy) - { - $this->redirectingPlugin->setRecipient($address); - } - } - - /** - * Creates a new message. - * - * @param string|array $from The from address - * @param string|array $to The recipient(s) - * @param string $subject The subject - * @param string $body The body - * - * @return Swift_Message A Swift_Message instance - */ - public function compose($from = null, $to = null, $subject = null, $body = null) - { - $msg = null; - - if(version_compare(Swift::VERSION, '6.0.0') >= 0) { - $msg = new Swift_Message($subject); - } else { - $msg = Swift_Message::newInstance($subject); - } - - return $msg - ->setFrom($from) - ->setTo($to) - ->setBody($body) - ; - } - - /** - * Sends a message. - * - * @param string|array $from The from address - * @param string|array $to The recipient(s) - * @param string $subject The subject - * @param string $body The body - * - * @return int The number of sent emails - */ - public function composeAndSend($from, $to, $subject, $body) - { - return $this->send($this->compose($from, $to, $subject, $body)); - } - - /** - * Forces the next call to send() to use the realtime strategy. - * - * @return sfMailer The current sfMailer instance - */ - public function sendNextImmediately() - { - $this->force = true; - - return $this; - } - /** * Sends the given message. * - * @param Swift_Message $message A transport instance - * @param string[] &$failedRecipients An array of failures by-reference + * @param Swift_Mime_Message $message A transport instance + * @param string[] &$failedRecipients An array of failures by-reference * * @return int|false The number of sent emails */ - public function send(Swift_Message $message, &$failedRecipients = null) + public function send(Swift_Mime_Message $message, &$failedRecipients = null) { if ($this->force) { @@ -306,27 +37,18 @@ public function send(Swift_Message $message, &$failedRecipients = null) return parent::send($message, $failedRecipients); } + /** - * Sends the current messages in the spool. - * - * The return value is the number of recipients who were accepted for delivery. - * - * @param string[] &$failedRecipients An array of failures by-reference - * - * @return int The number of sent emails + * @inheritDoc */ - public function flushQueue(&$failedRecipients = null) - { - return $this->getSpool()->flushQueue($this->realtimeTransport, $failedRecipients); - } - - public function getSpool() + public function compose($from = null, $to = null, $subject = null, $body = null) { - if (self::SPOOL != $this->strategy) - { - throw new LogicException(sprintf('You can only send messages in the spool if the delivery strategy is "spool" (%s is the current strategy).', $this->strategy)); - } + $msg = Swift_Message::newInstance($subject); - return $this->spool; + return $msg + ->setFrom($from) + ->setTo($to) + ->setBody($body) + ; } } diff --git a/lib/mailer/sfMailerBase.class.php b/lib/mailer/sfMailerBase.class.php new file mode 100755 index 000000000..34a8dcc46 --- /dev/null +++ b/lib/mailer/sfMailerBase.class.php @@ -0,0 +1,292 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * sfMailerBase is the base class of the main entry point for the mailer system sfMailer and sfMailerSwiftmailer6. + * + * This class is instanciated by sfContext on demand. + * + * @package symfony + * @subpackage mailer + * @author Fabien Potencier + * @version SVN: $Id$ + */ +abstract class sfMailerBase extends Swift_Mailer +{ + const + REALTIME = 'realtime', + SPOOL = 'spool', + SINGLE_ADDRESS = 'single_address', + NONE = 'none'; + + protected + $spool = null, + $logger = null, + $strategy = 'realtime', + $address = '', + $realtimeTransport = null, + $force = false, + $redirectingPlugin = null; + + /** + * Constructor. + * + * Available options: + * + * * charset: The default charset to use for messages + * * logging: Whether to enable logging or not + * * delivery_strategy: The delivery strategy to use + * * spool_class: The spool class (for the spool strategy) + * * spool_arguments: The arguments to pass to the spool constructor + * * delivery_address: The email address to use for the single_address strategy + * * transport: The main transport configuration + * * * class: The main transport class + * * * param: The main transport parameters + * + * @param sfEventDispatcher $dispatcher An event dispatcher instance + * @param array $options An array of options + */ + public function __construct(sfEventDispatcher $dispatcher, $options) + { + // options + $options = array_merge(array( + 'charset' => 'UTF-8', + 'logging' => false, + 'delivery_strategy' => self::REALTIME, + 'transport' => array( + 'class' => 'Swift_MailTransport', + 'param' => array(), + ), + ), $options); + + $constantName = 'sfMailerBase::'.strtoupper($options['delivery_strategy']); + $this->strategy = defined($constantName) ? constant($constantName) : false; + if (!$this->strategy) + { + throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy'])); + } + + if (static::NONE == $this->strategy) + { + $options['transport']['class'] = 'Swift_NullTransport'; + } + + // transport + $class = $options['transport']['class']; + $transport = new $class(); + if (isset($options['transport']['param'])) + { + foreach ($options['transport']['param'] as $key => $value) + { + $method = 'set'.ucfirst($key); + if (method_exists($transport, $method)) + { + $transport->$method($value); + } + elseif (method_exists($transport, 'getExtensionHandlers')) + { + foreach ($transport->getExtensionHandlers() as $handler) + { + if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods()))) + { + $transport->$method($value); + } + } + } + } + } + $this->realtimeTransport = $transport; + + if (static::SPOOL == $this->strategy) + { + if (!isset($options['spool_class'])) + { + throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option'); + } + $arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array(); + + if ($arguments) + { + $r = new ReflectionClass($options['spool_class']); + $this->spool = $r->newInstanceArgs($arguments); + } + else + { + $this->spool = new $options['spool_class']; + } + + $transport = new Swift_SpoolTransport($this->spool); + } + elseif (static::SINGLE_ADDRESS == $this->strategy) + { + if (!isset($options['delivery_address'])) + { + throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option'); + } + + $this->address = $options['delivery_address']; + + $transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address)); + } + + parent::__construct($transport); + + // logger + if ($options['logging']) + { + $this->logger = new sfMailerMessageLoggerPlugin($dispatcher); + + $transport->registerPlugin($this->logger); + } + + // preferences + Swift_Preferences::getInstance()->setCharset($options['charset']); + + $dispatcher->notify(new sfEvent($this, 'mailer.configure')); + } + + /** + * Gets the realtime transport instance. + * + * @return Swift_Transport The realtime transport instance. + */ + public function getRealtimeTransport() + { + return $this->realtimeTransport; + } + + /** + * Sets the realtime transport instance. + * + * @param Swift_Transport $transport The realtime transport instance. + */ + public function setRealtimeTransport(Swift_Transport $transport) + { + $this->realtimeTransport = $transport; + } + + /** + * Gets the logger instance. + * + * @return sfMailerMessageLoggerPlugin The logger instance. + */ + public function getLogger() + { + return $this->logger; + } + + /** + * Sets the logger instance. + * + * @param sfMailerMessageLoggerPlugin $logger The logger instance. + */ + public function setLogger($logger) + { + $this->logger = $logger; + } + + /** + * Gets the delivery strategy. + * + * @return string The delivery strategy + */ + public function getDeliveryStrategy() + { + return $this->strategy; + } + + /** + * Gets the delivery address. + * + * @return string The delivery address + */ + public function getDeliveryAddress() + { + return $this->address; + } + + /** + * Sets the delivery address. + * + * @param string $address The delivery address + */ + public function setDeliveryAddress($address) + { + $this->address = $address; + + if (static::SINGLE_ADDRESS == $this->strategy) + { + $this->redirectingPlugin->setRecipient($address); + } + } + + /** + * Creates a new message. + * + * @param string|array $from The from address + * @param string|array $to The recipient(s) + * @param string $subject The subject + * @param string $body The body + * + * @return Swift_Message A Swift_Message instance + */ + public abstract function compose($from = null, $to = null, $subject = null, $body = null); + + /** + * Sends a message. + * + * @param string|array $from The from address + * @param string|array $to The recipient(s) + * @param string $subject The subject + * @param string $body The body + * + * @return int The number of sent emails + */ + public function composeAndSend($from, $to, $subject, $body) + { + return $this->send($this->compose($from, $to, $subject, $body)); + } + + /** + * Forces the next call to send() to use the realtime strategy. + * + * @return static The current sfMailer instance + */ + public function sendNextImmediately() + { + $this->force = true; + + return $this; + } + + /** + * Sends the current messages in the spool. + * + * The return value is the number of recipients who were accepted for delivery. + * + * @param string[] &$failedRecipients An array of failures by-reference + * + * @return int The number of sent emails + */ + public function flushQueue(&$failedRecipients = null) + { + return $this->getSpool()->flushQueue($this->realtimeTransport, $failedRecipients); + } + + public function getSpool() + { + if (self::SPOOL != $this->strategy) + { + throw new LogicException(sprintf('You can only send messages in the spool if the delivery strategy is "spool" (%s is the current strategy).', $this->strategy)); + } + + return $this->spool; + } +} diff --git a/lib/mailer/sfMailerSwiftmailer6.class.php b/lib/mailer/sfMailerSwiftmailer6.class.php new file mode 100644 index 000000000..58612ed9a --- /dev/null +++ b/lib/mailer/sfMailerSwiftmailer6.class.php @@ -0,0 +1,56 @@ + + * @version SVN: $Id$ + */ +class sfMailerSwiftmailer6 extends sfMailerBase +{ + /** + * Sends the given message. + * + * @param Swift_Mime_SimpleMessage $message A transport instance + * @param string[] &$failedRecipients An array of failures by-reference + * + * @return int|false The number of sent emails + */ + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) + { + if ($this->force) + { + $this->force = false; + + if (!$this->realtimeTransport->isStarted()) + { + $this->realtimeTransport->start(); + } + + return $this->realtimeTransport->send($message, $failedRecipients); + } + + return parent::send($message, $failedRecipients); + } + + /** + * @inheritDoc + */ + public function compose($from = null, $to = null, $subject = null, $body = null) + { + $msg = new Swift_Message($subject); + + return $msg + ->setFrom($from) + ->setTo($to) + ->setBody($body) + ; + } +} From 750935fc2e2f860686ea95fbb484df94a949d958 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Mon, 7 Sep 2020 11:00:33 +0200 Subject: [PATCH 4/9] Swiftmailer6 compat. (cherry picked from commit 90c5a425adc11e9b2084507c3dc622abcf854eb6) --- lib/autoload/sfCoreAutoload.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/autoload/sfCoreAutoload.class.php b/lib/autoload/sfCoreAutoload.class.php index 5e1604f79..4f08e8030 100755 --- a/lib/autoload/sfCoreAutoload.class.php +++ b/lib/autoload/sfCoreAutoload.class.php @@ -342,7 +342,9 @@ static public function make() 'sfvarlogger' => 'log/sfVarLogger.class.php', 'sfwebdebuglogger' => 'log/sfWebDebugLogger.class.php', 'sfmailer' => 'mailer/sfMailer.class.php', + 'sfmailerbase' => 'mailer/sfMailerBase.class.php', 'sfmailermessageloggerplugin' => 'mailer/sfMailerMessageLoggerPlugin.class.php', + 'sfmailerswiftmailer6' => 'mailer/sfMailerSwiftmailer6.class.php', 'sfnomailer' => 'mailer/sfNoMailer.class.php', 'sfpearconfig' => 'plugin/sfPearConfig.class.php', 'sfpeardownloader' => 'plugin/sfPearDownloader.class.php', From 30dd4d538b13e7c4b373c2a7bd15bd87cff6da6f Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Mon, 7 Sep 2020 12:03:10 +0200 Subject: [PATCH 5/9] Check if class "Swift" is loaded. --- autoload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload.php b/autoload.php index 62f2f2088..7ce8ea357 100755 --- a/autoload.php +++ b/autoload.php @@ -5,6 +5,6 @@ sfCoreAutoload::register(); // Register sfMailerSwiftmailer6 as default sfMailer, if Swiftmailer ~6.0 has been loaded. -if(version_compare(Swift::VERSION, '6.0.0') >= 0) { +if(class_exists('Swift') && version_compare(Swift::VERSION, '6.0.0') >= 0) { sfConfig::set('sf_factory_mailer', 'sfMailerSwiftmailer6'); } From c5c48182ad69e8d2a4bf3e0d532829a0d08c53c3 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 8 Sep 2020 07:47:23 +0200 Subject: [PATCH 6/9] Another easy way for being compatbiel with swiftmailer 5 and 6! --- autoload.php | 5 - lib/autoload/sfCoreAutoload.class.php | 2 - lib/mailer/sfMailer.class.php | 344 ++++++++++++++++++++-- lib/mailer/sfMailerBase.class.php | 292 ------------------ lib/mailer/sfMailerSwiftmailer6.class.php | 56 ---- 5 files changed, 325 insertions(+), 374 deletions(-) delete mode 100755 lib/mailer/sfMailerBase.class.php delete mode 100644 lib/mailer/sfMailerSwiftmailer6.class.php diff --git a/autoload.php b/autoload.php index 7ce8ea357..3f223a024 100755 --- a/autoload.php +++ b/autoload.php @@ -3,8 +3,3 @@ require_once __DIR__.'/lib/autoload/sfCoreAutoload.class.php'; sfCoreAutoload::register(); - -// Register sfMailerSwiftmailer6 as default sfMailer, if Swiftmailer ~6.0 has been loaded. -if(class_exists('Swift') && version_compare(Swift::VERSION, '6.0.0') >= 0) { - sfConfig::set('sf_factory_mailer', 'sfMailerSwiftmailer6'); -} diff --git a/lib/autoload/sfCoreAutoload.class.php b/lib/autoload/sfCoreAutoload.class.php index 4f08e8030..5e1604f79 100755 --- a/lib/autoload/sfCoreAutoload.class.php +++ b/lib/autoload/sfCoreAutoload.class.php @@ -342,9 +342,7 @@ static public function make() 'sfvarlogger' => 'log/sfVarLogger.class.php', 'sfwebdebuglogger' => 'log/sfWebDebugLogger.class.php', 'sfmailer' => 'mailer/sfMailer.class.php', - 'sfmailerbase' => 'mailer/sfMailerBase.class.php', 'sfmailermessageloggerplugin' => 'mailer/sfMailerMessageLoggerPlugin.class.php', - 'sfmailerswiftmailer6' => 'mailer/sfMailerSwiftmailer6.class.php', 'sfnomailer' => 'mailer/sfNoMailer.class.php', 'sfpearconfig' => 'plugin/sfPearConfig.class.php', 'sfpeardownloader' => 'plugin/sfPearDownloader.class.php', diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php index 0e351b764..c96f38b61 100644 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -1,5 +1,93 @@ = 0) { + class sfMailerBase extends Swift_Mailer + { + /** + * Sends the given message. + * + * @param Swift_Mime_SimpleMessage $message A transport instance + * @param string[] &$failedRecipients An array of failures by-reference + * + * @return int|false The number of sent emails + */ + public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) + { + if ($this->force) + { + $this->force = false; + + if (!$this->realtimeTransport->isStarted()) + { + $this->realtimeTransport->start(); + } + + return $this->realtimeTransport->send($message, $failedRecipients); + } + + return parent::send($message, $failedRecipients); + } + + /** + * @inheritDoc + */ + public function compose($from = null, $to = null, $subject = null, $body = null) + { + $msg = new Swift_Message($subject); + + return $msg + ->setFrom($from) + ->setTo($to) + ->setBody($body) + ; + } + } +} else { + class sfMailerBase extends Swift_Mailer + { + /** + * Sends the given message. + * + * @param Swift_Mime_Message $message A transport instance + * @param string[] &$failedRecipients An array of failures by-reference + * + * @return int|false The number of sent emails + */ + public function send(Swift_Mime_Message $message, &$failedRecipients = null) + { + if ($this->force) + { + $this->force = false; + + if (!$this->realtimeTransport->isStarted()) + { + $this->realtimeTransport->start(); + } + + return $this->realtimeTransport->send($message, $failedRecipients); + } + + return parent::send($message, $failedRecipients); + } + + + /** + * @inheritDoc + */ + public function compose($from = null, $to = null, $subject = null, $body = null) + { + $msg = Swift_Message::newInstance($subject); + + return $msg + ->setFrom($from) + ->setTo($to) + ->setBody($body) + ; + } + } +} + /** * sfMailer is the main entry point for the mailer system. * @@ -12,43 +100,261 @@ */ class sfMailer extends sfMailerBase { + const + REALTIME = 'realtime', + SPOOL = 'spool', + SINGLE_ADDRESS = 'single_address', + NONE = 'none'; + + protected + $spool = null, + $logger = null, + $strategy = 'realtime', + $address = '', + $realtimeTransport = null, + $force = false, + $redirectingPlugin = null; + /** - * Sends the given message. + * Constructor. + * + * Available options: * - * @param Swift_Mime_Message $message A transport instance - * @param string[] &$failedRecipients An array of failures by-reference + * * charset: The default charset to use for messages + * * logging: Whether to enable logging or not + * * delivery_strategy: The delivery strategy to use + * * spool_class: The spool class (for the spool strategy) + * * spool_arguments: The arguments to pass to the spool constructor + * * delivery_address: The email address to use for the single_address strategy + * * transport: The main transport configuration + * * * class: The main transport class + * * * param: The main transport parameters * - * @return int|false The number of sent emails + * @param sfEventDispatcher $dispatcher An event dispatcher instance + * @param array $options An array of options */ - public function send(Swift_Mime_Message $message, &$failedRecipients = null) + public function __construct(sfEventDispatcher $dispatcher, $options) { - if ($this->force) + // options + $options = array_merge(array( + 'charset' => 'UTF-8', + 'logging' => false, + 'delivery_strategy' => self::REALTIME, + 'transport' => array( + 'class' => 'Swift_MailTransport', + 'param' => array(), + ), + ), $options); + + $constantName = 'sfMailerBase::'.strtoupper($options['delivery_strategy']); + $this->strategy = defined($constantName) ? constant($constantName) : false; + if (!$this->strategy) + { + throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy'])); + } + + if (static::NONE == $this->strategy) + { + $options['transport']['class'] = 'Swift_NullTransport'; + } + + // transport + $class = $options['transport']['class']; + $transport = new $class(); + if (isset($options['transport']['param'])) + { + foreach ($options['transport']['param'] as $key => $value) + { + $method = 'set'.ucfirst($key); + if (method_exists($transport, $method)) + { + $transport->$method($value); + } + elseif (method_exists($transport, 'getExtensionHandlers')) + { + foreach ($transport->getExtensionHandlers() as $handler) + { + if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods()))) + { + $transport->$method($value); + } + } + } + } + } + $this->realtimeTransport = $transport; + + if (static::SPOOL == $this->strategy) { - $this->force = false; + if (!isset($options['spool_class'])) + { + throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option'); + } + $arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array(); - if (!$this->realtimeTransport->isStarted()) + if ($arguments) + { + $r = new ReflectionClass($options['spool_class']); + $this->spool = $r->newInstanceArgs($arguments); + } + else { - $this->realtimeTransport->start(); + $this->spool = new $options['spool_class']; } - return $this->realtimeTransport->send($message, $failedRecipients); + $transport = new Swift_SpoolTransport($this->spool); + } + elseif (static::SINGLE_ADDRESS == $this->strategy) + { + if (!isset($options['delivery_address'])) + { + throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option'); + } + + $this->address = $options['delivery_address']; + + $transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address)); + } + + parent::__construct($transport); + + // logger + if ($options['logging']) + { + $this->logger = new sfMailerMessageLoggerPlugin($dispatcher); + + $transport->registerPlugin($this->logger); + } + + // preferences + Swift_Preferences::getInstance()->setCharset($options['charset']); + + $dispatcher->notify(new sfEvent($this, 'mailer.configure')); + } + + /** + * Gets the realtime transport instance. + * + * @return Swift_Transport The realtime transport instance. + */ + public function getRealtimeTransport() + { + return $this->realtimeTransport; + } + + /** + * Sets the realtime transport instance. + * + * @param Swift_Transport $transport The realtime transport instance. + */ + public function setRealtimeTransport(Swift_Transport $transport) + { + $this->realtimeTransport = $transport; + } + + /** + * Gets the logger instance. + * + * @return sfMailerMessageLoggerPlugin The logger instance. + */ + public function getLogger() + { + return $this->logger; + } + + /** + * Sets the logger instance. + * + * @param sfMailerMessageLoggerPlugin $logger The logger instance. + */ + public function setLogger($logger) + { + $this->logger = $logger; + } + + /** + * Gets the delivery strategy. + * + * @return string The delivery strategy + */ + public function getDeliveryStrategy() + { + return $this->strategy; + } + + /** + * Gets the delivery address. + * + * @return string The delivery address + */ + public function getDeliveryAddress() + { + return $this->address; + } + + /** + * Sets the delivery address. + * + * @param string $address The delivery address + */ + public function setDeliveryAddress($address) + { + $this->address = $address; + + if (static::SINGLE_ADDRESS == $this->strategy) + { + $this->redirectingPlugin->setRecipient($address); } + } - return parent::send($message, $failedRecipients); + /** + * Sends a message. + * + * @param string|array $from The from address + * @param string|array $to The recipient(s) + * @param string $subject The subject + * @param string $body The body + * + * @return int The number of sent emails + */ + public function composeAndSend($from, $to, $subject, $body) + { + return $this->send($this->compose($from, $to, $subject, $body)); } + /** + * Forces the next call to send() to use the realtime strategy. + * + * @return static The current sfMailer instance + */ + public function sendNextImmediately() + { + $this->force = true; + + return $this; + } /** - * @inheritDoc + * Sends the current messages in the spool. + * + * The return value is the number of recipients who were accepted for delivery. + * + * @param string[] &$failedRecipients An array of failures by-reference + * + * @return int The number of sent emails */ - public function compose($from = null, $to = null, $subject = null, $body = null) + public function flushQueue(&$failedRecipients = null) { - $msg = Swift_Message::newInstance($subject); + return $this->getSpool()->flushQueue($this->realtimeTransport, $failedRecipients); + } + + public function getSpool() + { + if (self::SPOOL != $this->strategy) + { + throw new LogicException(sprintf('You can only send messages in the spool if the delivery strategy is "spool" (%s is the current strategy).', $this->strategy)); + } - return $msg - ->setFrom($from) - ->setTo($to) - ->setBody($body) - ; + return $this->spool; } } diff --git a/lib/mailer/sfMailerBase.class.php b/lib/mailer/sfMailerBase.class.php deleted file mode 100755 index 34a8dcc46..000000000 --- a/lib/mailer/sfMailerBase.class.php +++ /dev/null @@ -1,292 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -/** - * sfMailerBase is the base class of the main entry point for the mailer system sfMailer and sfMailerSwiftmailer6. - * - * This class is instanciated by sfContext on demand. - * - * @package symfony - * @subpackage mailer - * @author Fabien Potencier - * @version SVN: $Id$ - */ -abstract class sfMailerBase extends Swift_Mailer -{ - const - REALTIME = 'realtime', - SPOOL = 'spool', - SINGLE_ADDRESS = 'single_address', - NONE = 'none'; - - protected - $spool = null, - $logger = null, - $strategy = 'realtime', - $address = '', - $realtimeTransport = null, - $force = false, - $redirectingPlugin = null; - - /** - * Constructor. - * - * Available options: - * - * * charset: The default charset to use for messages - * * logging: Whether to enable logging or not - * * delivery_strategy: The delivery strategy to use - * * spool_class: The spool class (for the spool strategy) - * * spool_arguments: The arguments to pass to the spool constructor - * * delivery_address: The email address to use for the single_address strategy - * * transport: The main transport configuration - * * * class: The main transport class - * * * param: The main transport parameters - * - * @param sfEventDispatcher $dispatcher An event dispatcher instance - * @param array $options An array of options - */ - public function __construct(sfEventDispatcher $dispatcher, $options) - { - // options - $options = array_merge(array( - 'charset' => 'UTF-8', - 'logging' => false, - 'delivery_strategy' => self::REALTIME, - 'transport' => array( - 'class' => 'Swift_MailTransport', - 'param' => array(), - ), - ), $options); - - $constantName = 'sfMailerBase::'.strtoupper($options['delivery_strategy']); - $this->strategy = defined($constantName) ? constant($constantName) : false; - if (!$this->strategy) - { - throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy'])); - } - - if (static::NONE == $this->strategy) - { - $options['transport']['class'] = 'Swift_NullTransport'; - } - - // transport - $class = $options['transport']['class']; - $transport = new $class(); - if (isset($options['transport']['param'])) - { - foreach ($options['transport']['param'] as $key => $value) - { - $method = 'set'.ucfirst($key); - if (method_exists($transport, $method)) - { - $transport->$method($value); - } - elseif (method_exists($transport, 'getExtensionHandlers')) - { - foreach ($transport->getExtensionHandlers() as $handler) - { - if (in_array(strtolower($method), array_map('strtolower', (array) $handler->exposeMixinMethods()))) - { - $transport->$method($value); - } - } - } - } - } - $this->realtimeTransport = $transport; - - if (static::SPOOL == $this->strategy) - { - if (!isset($options['spool_class'])) - { - throw new InvalidArgumentException('For the spool mail delivery strategy, you must also define a spool_class option'); - } - $arguments = isset($options['spool_arguments']) ? $options['spool_arguments'] : array(); - - if ($arguments) - { - $r = new ReflectionClass($options['spool_class']); - $this->spool = $r->newInstanceArgs($arguments); - } - else - { - $this->spool = new $options['spool_class']; - } - - $transport = new Swift_SpoolTransport($this->spool); - } - elseif (static::SINGLE_ADDRESS == $this->strategy) - { - if (!isset($options['delivery_address'])) - { - throw new InvalidArgumentException('For the single_address mail delivery strategy, you must also define a delivery_address option'); - } - - $this->address = $options['delivery_address']; - - $transport->registerPlugin($this->redirectingPlugin = new Swift_Plugins_RedirectingPlugin($this->address)); - } - - parent::__construct($transport); - - // logger - if ($options['logging']) - { - $this->logger = new sfMailerMessageLoggerPlugin($dispatcher); - - $transport->registerPlugin($this->logger); - } - - // preferences - Swift_Preferences::getInstance()->setCharset($options['charset']); - - $dispatcher->notify(new sfEvent($this, 'mailer.configure')); - } - - /** - * Gets the realtime transport instance. - * - * @return Swift_Transport The realtime transport instance. - */ - public function getRealtimeTransport() - { - return $this->realtimeTransport; - } - - /** - * Sets the realtime transport instance. - * - * @param Swift_Transport $transport The realtime transport instance. - */ - public function setRealtimeTransport(Swift_Transport $transport) - { - $this->realtimeTransport = $transport; - } - - /** - * Gets the logger instance. - * - * @return sfMailerMessageLoggerPlugin The logger instance. - */ - public function getLogger() - { - return $this->logger; - } - - /** - * Sets the logger instance. - * - * @param sfMailerMessageLoggerPlugin $logger The logger instance. - */ - public function setLogger($logger) - { - $this->logger = $logger; - } - - /** - * Gets the delivery strategy. - * - * @return string The delivery strategy - */ - public function getDeliveryStrategy() - { - return $this->strategy; - } - - /** - * Gets the delivery address. - * - * @return string The delivery address - */ - public function getDeliveryAddress() - { - return $this->address; - } - - /** - * Sets the delivery address. - * - * @param string $address The delivery address - */ - public function setDeliveryAddress($address) - { - $this->address = $address; - - if (static::SINGLE_ADDRESS == $this->strategy) - { - $this->redirectingPlugin->setRecipient($address); - } - } - - /** - * Creates a new message. - * - * @param string|array $from The from address - * @param string|array $to The recipient(s) - * @param string $subject The subject - * @param string $body The body - * - * @return Swift_Message A Swift_Message instance - */ - public abstract function compose($from = null, $to = null, $subject = null, $body = null); - - /** - * Sends a message. - * - * @param string|array $from The from address - * @param string|array $to The recipient(s) - * @param string $subject The subject - * @param string $body The body - * - * @return int The number of sent emails - */ - public function composeAndSend($from, $to, $subject, $body) - { - return $this->send($this->compose($from, $to, $subject, $body)); - } - - /** - * Forces the next call to send() to use the realtime strategy. - * - * @return static The current sfMailer instance - */ - public function sendNextImmediately() - { - $this->force = true; - - return $this; - } - - /** - * Sends the current messages in the spool. - * - * The return value is the number of recipients who were accepted for delivery. - * - * @param string[] &$failedRecipients An array of failures by-reference - * - * @return int The number of sent emails - */ - public function flushQueue(&$failedRecipients = null) - { - return $this->getSpool()->flushQueue($this->realtimeTransport, $failedRecipients); - } - - public function getSpool() - { - if (self::SPOOL != $this->strategy) - { - throw new LogicException(sprintf('You can only send messages in the spool if the delivery strategy is "spool" (%s is the current strategy).', $this->strategy)); - } - - return $this->spool; - } -} diff --git a/lib/mailer/sfMailerSwiftmailer6.class.php b/lib/mailer/sfMailerSwiftmailer6.class.php deleted file mode 100644 index 58612ed9a..000000000 --- a/lib/mailer/sfMailerSwiftmailer6.class.php +++ /dev/null @@ -1,56 +0,0 @@ - - * @version SVN: $Id$ - */ -class sfMailerSwiftmailer6 extends sfMailerBase -{ - /** - * Sends the given message. - * - * @param Swift_Mime_SimpleMessage $message A transport instance - * @param string[] &$failedRecipients An array of failures by-reference - * - * @return int|false The number of sent emails - */ - public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) - { - if ($this->force) - { - $this->force = false; - - if (!$this->realtimeTransport->isStarted()) - { - $this->realtimeTransport->start(); - } - - return $this->realtimeTransport->send($message, $failedRecipients); - } - - return parent::send($message, $failedRecipients); - } - - /** - * @inheritDoc - */ - public function compose($from = null, $to = null, $subject = null, $body = null) - { - $msg = new Swift_Message($subject); - - return $msg - ->setFrom($from) - ->setTo($to) - ->setBody($body) - ; - } -} From 2ed880f562825ab1c034e34acd6a155e64943e03 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 8 Sep 2020 07:48:23 +0200 Subject: [PATCH 7/9] Reverted sfMailer changes. --- lib/mailer/sfMailer.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php index c96f38b61..c94198caf 100644 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -146,14 +146,14 @@ public function __construct(sfEventDispatcher $dispatcher, $options) ), ), $options); - $constantName = 'sfMailerBase::'.strtoupper($options['delivery_strategy']); + $constantName = 'sfMailer::'.strtoupper($options['delivery_strategy']); $this->strategy = defined($constantName) ? constant($constantName) : false; if (!$this->strategy) { throw new InvalidArgumentException(sprintf('Unknown mail delivery strategy "%s" (should be one of realtime, spool, single_address, or none)', $options['delivery_strategy'])); } - if (static::NONE == $this->strategy) + if (sfMailer::NONE == $this->strategy) { $options['transport']['class'] = 'Swift_NullTransport'; } @@ -184,7 +184,7 @@ public function __construct(sfEventDispatcher $dispatcher, $options) } $this->realtimeTransport = $transport; - if (static::SPOOL == $this->strategy) + if (sfMailer::SPOOL == $this->strategy) { if (!isset($options['spool_class'])) { @@ -204,7 +204,7 @@ public function __construct(sfEventDispatcher $dispatcher, $options) $transport = new Swift_SpoolTransport($this->spool); } - elseif (static::SINGLE_ADDRESS == $this->strategy) + elseif (sfMailer::SINGLE_ADDRESS == $this->strategy) { if (!isset($options['delivery_address'])) { @@ -301,7 +301,7 @@ public function setDeliveryAddress($address) { $this->address = $address; - if (static::SINGLE_ADDRESS == $this->strategy) + if (sfMailer::SINGLE_ADDRESS == $this->strategy) { $this->redirectingPlugin->setRecipient($address); } From 66dd314be9fc98002163df6d3bca7ff0f9525290 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 8 Sep 2020 07:50:39 +0200 Subject: [PATCH 8/9] Even more reverts. --- lib/mailer/sfMailer.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mailer/sfMailer.class.php b/lib/mailer/sfMailer.class.php index c94198caf..8805b41c4 100644 --- a/lib/mailer/sfMailer.class.php +++ b/lib/mailer/sfMailer.class.php @@ -91,11 +91,11 @@ public function compose($from = null, $to = null, $subject = null, $body = null) /** * sfMailer is the main entry point for the mailer system. * - * This class is instanciated by sfContext on demand and compatible to swiftmailer ~5.2 + * This class is instanciated by sfContext on demand. * * @package symfony * @subpackage mailer - * @author Thomas A. Hirsch + * @author Fabien Potencier * @version SVN: $Id$ */ class sfMailer extends sfMailerBase @@ -325,7 +325,7 @@ public function composeAndSend($from, $to, $subject, $body) /** * Forces the next call to send() to use the realtime strategy. * - * @return static The current sfMailer instance + * @return sfMailer The current sfMailer instance */ public function sendNextImmediately() { From b557d34efb56077cb1395866ddd3ecb9ce786ec1 Mon Sep 17 00:00:00 2001 From: "Thomas A. Hirsch" Date: Tue, 8 Sep 2020 09:54:59 +0200 Subject: [PATCH 9/9] It is still possible to keep the submodule approach, as an installation via composer will not install submodules. Yeay! --- .gitmodules | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitmodules b/.gitmodules index ed98e74cd..f9e1e7496 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,7 @@ +[submodule "lib/vendor/swiftmailer"] + path = lib/vendor/swiftmailer + url = https://github.com/swiftmailer/swiftmailer.git + branch = 5.x [submodule "lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine"] path = lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine url = https://github.com/LExpress/doctrine1.git