From 7b910110329a3639831540a81fd254db0d77a0d3 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 7 Nov 2013 11:01:43 -0500 Subject: [PATCH] Adjusted the directories of swiftmailer. --- extensions/swiftmailer/Mailer.php | 142 ++++++++++++ extensions/swiftmailer/Message.php | 237 +++++++++++++++++++++ extensions/swiftmailer/composer.json | 3 +- extensions/swiftmailer/yii/swiftmailer/Mailer.php | 142 ------------ extensions/swiftmailer/yii/swiftmailer/Message.php | 237 --------------------- 5 files changed, 381 insertions(+), 380 deletions(-) create mode 100644 extensions/swiftmailer/Mailer.php create mode 100644 extensions/swiftmailer/Message.php delete mode 100644 extensions/swiftmailer/yii/swiftmailer/Mailer.php delete mode 100644 extensions/swiftmailer/yii/swiftmailer/Message.php diff --git a/extensions/swiftmailer/Mailer.php b/extensions/swiftmailer/Mailer.php new file mode 100644 index 0000000..1d89290 --- /dev/null +++ b/extensions/swiftmailer/Mailer.php @@ -0,0 +1,142 @@ + array( + * ... + * 'email' => array( + * 'class' => 'yii\swiftmailer\Mailer', + * 'transport' => [ + * 'class' => 'Swift_SmtpTransport', + * 'host' => 'localhost', + * 'username' => 'username', + * 'password' => 'password', + * 'port' => '587', + * 'encryption' => 'tls', + * ], + * ), + * ... + * ), + * ~~~ + * + * @see http://swiftmailer.org + * + * @method Message compose($view = null, array $params = []) creates new message optionally filling up its body via view rendering. + * + * @author Paul Klimov + * @since 2.0 + */ +class Mailer extends BaseMailer +{ + /** + * @var string message default class name. + */ + public $messageClass = 'yii\swiftmailer\Message'; + /** + * @var \Swift_Mailer Swift mailer instance. + */ + private $_swiftMailer; + /** + * @var \Swift_Transport|array Swift transport instance or its array configuration. + */ + private $_transport = []; + + /** + * @return array|\Swift_Mailer Swift mailer instance or array configuration. + */ + public function getSwiftMailer() + { + if (!is_object($this->_swiftMailer)) { + $this->_swiftMailer = $this->createSwiftMailer(); + } + return $this->_swiftMailer; + } + + /** + * @param array|\Swift_Transport $transport + * @throws \yii\base\InvalidConfigException on invalid argument. + */ + public function setTransport($transport) + { + if (!is_array($transport) && !is_object($transport)) { + throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.'); + } + $this->_transport = $transport; + } + + /** + * @return array|\Swift_Transport + */ + public function getTransport() + { + if (!is_object($this->_transport)) { + $this->_transport = $this->createTransport($this->_transport); + } + return $this->_transport; + } + + /** + * @inheritdoc + */ + public function send($message) + { + Yii::trace('Sending email message', __METHOD__); + return ($this->getSwiftMailer()->send($message->getSwiftMessage()) > 0); + } + + /** + * Creates Swift mailer instance. + * @return \Swift_Mailer mailer instance. + */ + protected function createSwiftMailer() + { + return \Swift_Mailer::newInstance($this->getTransport()); + } + + /** + * Creates email transport instance by its array configuration. + * @param array $config transport configuration. + * @throws \yii\base\InvalidConfigException on invalid transport configuration. + * @return \Swift_Transport transport instance. + */ + protected function createTransport(array $config) + { + if (array_key_exists('class', $config)) { + $className = $config['class']; + unset($config['class']); + } else { + $className = 'Swift_MailTransport'; + } + $transport = call_user_func([$className, 'newInstance']); + if (!empty($config)) { + foreach ($config as $name => $value) { + if (property_exists($transport, $name)) { + $transport->$name = $value; + } else { + $setter = 'set' . $name; + if (method_exists($transport, $setter)) { + $transport->$setter($value); + } else { + throw new InvalidConfigException('Setting unknown property: ' . get_class($transport) . '::' . $name); + } + } + } + } + return $transport; + } +} \ No newline at end of file diff --git a/extensions/swiftmailer/Message.php b/extensions/swiftmailer/Message.php new file mode 100644 index 0000000..8997695 --- /dev/null +++ b/extensions/swiftmailer/Message.php @@ -0,0 +1,237 @@ + + * @since 2.0 + */ +class Message extends BaseMessage +{ + /** + * @var \Swift_Message Swift message instance. + */ + private $_swiftMessage; + + /** + * @return \Swift_Message Swift message instance. + */ + public function getSwiftMessage() + { + if (!is_object($this->_swiftMessage)) { + $this->_swiftMessage = $this->createSwiftMessage(); + } + return $this->_swiftMessage; + } + + /** + * @inheritdoc + */ + public function charset($charset) + { + $this->getSwiftMessage()->setCharset($charset); + return $this; + } + + /** + * @inheritdoc + */ + public function from($from) + { + $this->getSwiftMessage()->setFrom($from); + $this->getSwiftMessage()->setReplyTo($from); + return $this; + } + + /** + * @inheritdoc + */ + public function to($to) + { + $this->getSwiftMessage()->setTo($to); + return $this; + } + + /** + * @inheritdoc + */ + public function cc($cc) + { + $this->getSwiftMessage()->setCc($cc); + return $this; + } + + /** + * @inheritdoc + */ + public function bcc($bcc) + { + $this->getSwiftMessage()->setBcc($bcc); + return $this; + } + + /** + * @inheritdoc + */ + public function subject($subject) + { + $this->getSwiftMessage()->setSubject($subject); + return $this; + } + + /** + * @inheritdoc + */ + public function textBody($text) + { + $this->setBody($text, 'text/plain'); + return $this; + } + + /** + * @inheritdoc + */ + public function htmlBody($html) + { + $this->setBody($html, 'text/html'); + return $this; + } + + /** + * Sets the message body. + * If body is already set and its content type matches given one, it will + * be overridden, if content type miss match the multipart message will be composed. + * @param string $body body content. + * @param string $contentType body content type. + */ + protected function setBody($body, $contentType) + { + $message = $this->getSwiftMessage(); + $oldBody = $message->getBody(); + if (empty($oldBody)) { + $parts = $message->getChildren(); + $partFound = false; + foreach ($parts as $key => $part) { + if (!($part instanceof \Swift_Mime_Attachment)) { + /* @var $part \Swift_Mime_MimePart */ + if ($part->getContentType() == $contentType) { + unset($parts[$key]); + $partFound = true; + break; + } + } + } + if ($partFound) { + reset($parts); + $message->setChildren($parts); + $message->addPart($body, $contentType); + } else { + $message->setBody($body, $contentType); + } + } else { + $oldContentType = $message->getContentType(); + if ($oldContentType == $contentType) { + $message->setBody($body, $contentType); + } else { + $message->setBody(null); + $message->setContentType(null); + $message->addPart($oldBody, $oldContentType); + $message->addPart($body, $contentType); + } + } + } + + /** + * @inheritdoc + */ + public function attach($fileName, array $options = []) + { + $attachment = \Swift_Attachment::fromPath($fileName); + if (!empty($options['fileName'])) { + $attachment->setFilename($options['fileName']); + } + if (!empty($options['contentType'])) { + $attachment->setContentType($options['contentType']); + } + $this->getSwiftMessage()->attach($attachment); + return $this; + } + + /** + * @inheritdoc + */ + public function attachContent($content, array $options = []) + { + $attachment = \Swift_Attachment::newInstance($content); + if (!empty($options['fileName'])) { + $attachment->setFilename($options['fileName']); + } + if (!empty($options['contentType'])) { + $attachment->setContentType($options['contentType']); + } + $this->getSwiftMessage()->attach($attachment); + return $this; + } + + /** + * @inheritdoc + */ + public function embed($fileName, array $options = []) + { + $embedFile = \Swift_EmbeddedFile::fromPath($fileName); + if (!empty($options['fileName'])) { + $embedFile->setFilename($options['fileName']); + } + if (!empty($options['contentType'])) { + $embedFile->setContentType($options['contentType']); + } + return $this->getSwiftMessage()->embed($embedFile); + } + + /** + * @inheritdoc + */ + public function embedContent($content, array $options = []) + { + $embedFile = \Swift_EmbeddedFile::newInstance($content); + if (!empty($options['fileName'])) { + $embedFile->setFilename($options['fileName']); + } + if (!empty($options['contentType'])) { + $embedFile->setContentType($options['contentType']); + } + return $this->getSwiftMessage()->embed($embedFile); + } + + /** + * @inheritdoc + */ + public function toString() + { + return $this->getSwiftMessage()->toString(); + } + + /** + * Creates the Swift email message instance. + * @return \Swift_Message email message instance. + */ + protected function createSwiftMessage() + { + return new \Swift_Message(); + } +} \ No newline at end of file diff --git a/extensions/swiftmailer/composer.json b/extensions/swiftmailer/composer.json index bbab7eb..e995b2f 100644 --- a/extensions/swiftmailer/composer.json +++ b/extensions/swiftmailer/composer.json @@ -24,5 +24,6 @@ }, "autoload": { "psr-0": { "yii\\swiftmailer\\": "" } - } + }, + "target-dir": "yii/swiftmailer" } diff --git a/extensions/swiftmailer/yii/swiftmailer/Mailer.php b/extensions/swiftmailer/yii/swiftmailer/Mailer.php deleted file mode 100644 index 1d89290..0000000 --- a/extensions/swiftmailer/yii/swiftmailer/Mailer.php +++ /dev/null @@ -1,142 +0,0 @@ - array( - * ... - * 'email' => array( - * 'class' => 'yii\swiftmailer\Mailer', - * 'transport' => [ - * 'class' => 'Swift_SmtpTransport', - * 'host' => 'localhost', - * 'username' => 'username', - * 'password' => 'password', - * 'port' => '587', - * 'encryption' => 'tls', - * ], - * ), - * ... - * ), - * ~~~ - * - * @see http://swiftmailer.org - * - * @method Message compose($view = null, array $params = []) creates new message optionally filling up its body via view rendering. - * - * @author Paul Klimov - * @since 2.0 - */ -class Mailer extends BaseMailer -{ - /** - * @var string message default class name. - */ - public $messageClass = 'yii\swiftmailer\Message'; - /** - * @var \Swift_Mailer Swift mailer instance. - */ - private $_swiftMailer; - /** - * @var \Swift_Transport|array Swift transport instance or its array configuration. - */ - private $_transport = []; - - /** - * @return array|\Swift_Mailer Swift mailer instance or array configuration. - */ - public function getSwiftMailer() - { - if (!is_object($this->_swiftMailer)) { - $this->_swiftMailer = $this->createSwiftMailer(); - } - return $this->_swiftMailer; - } - - /** - * @param array|\Swift_Transport $transport - * @throws \yii\base\InvalidConfigException on invalid argument. - */ - public function setTransport($transport) - { - if (!is_array($transport) && !is_object($transport)) { - throw new InvalidConfigException('"' . get_class($this) . '::transport" should be either object or array, "' . gettype($transport) . '" given.'); - } - $this->_transport = $transport; - } - - /** - * @return array|\Swift_Transport - */ - public function getTransport() - { - if (!is_object($this->_transport)) { - $this->_transport = $this->createTransport($this->_transport); - } - return $this->_transport; - } - - /** - * @inheritdoc - */ - public function send($message) - { - Yii::trace('Sending email message', __METHOD__); - return ($this->getSwiftMailer()->send($message->getSwiftMessage()) > 0); - } - - /** - * Creates Swift mailer instance. - * @return \Swift_Mailer mailer instance. - */ - protected function createSwiftMailer() - { - return \Swift_Mailer::newInstance($this->getTransport()); - } - - /** - * Creates email transport instance by its array configuration. - * @param array $config transport configuration. - * @throws \yii\base\InvalidConfigException on invalid transport configuration. - * @return \Swift_Transport transport instance. - */ - protected function createTransport(array $config) - { - if (array_key_exists('class', $config)) { - $className = $config['class']; - unset($config['class']); - } else { - $className = 'Swift_MailTransport'; - } - $transport = call_user_func([$className, 'newInstance']); - if (!empty($config)) { - foreach ($config as $name => $value) { - if (property_exists($transport, $name)) { - $transport->$name = $value; - } else { - $setter = 'set' . $name; - if (method_exists($transport, $setter)) { - $transport->$setter($value); - } else { - throw new InvalidConfigException('Setting unknown property: ' . get_class($transport) . '::' . $name); - } - } - } - } - return $transport; - } -} \ No newline at end of file diff --git a/extensions/swiftmailer/yii/swiftmailer/Message.php b/extensions/swiftmailer/yii/swiftmailer/Message.php deleted file mode 100644 index 8997695..0000000 --- a/extensions/swiftmailer/yii/swiftmailer/Message.php +++ /dev/null @@ -1,237 +0,0 @@ - - * @since 2.0 - */ -class Message extends BaseMessage -{ - /** - * @var \Swift_Message Swift message instance. - */ - private $_swiftMessage; - - /** - * @return \Swift_Message Swift message instance. - */ - public function getSwiftMessage() - { - if (!is_object($this->_swiftMessage)) { - $this->_swiftMessage = $this->createSwiftMessage(); - } - return $this->_swiftMessage; - } - - /** - * @inheritdoc - */ - public function charset($charset) - { - $this->getSwiftMessage()->setCharset($charset); - return $this; - } - - /** - * @inheritdoc - */ - public function from($from) - { - $this->getSwiftMessage()->setFrom($from); - $this->getSwiftMessage()->setReplyTo($from); - return $this; - } - - /** - * @inheritdoc - */ - public function to($to) - { - $this->getSwiftMessage()->setTo($to); - return $this; - } - - /** - * @inheritdoc - */ - public function cc($cc) - { - $this->getSwiftMessage()->setCc($cc); - return $this; - } - - /** - * @inheritdoc - */ - public function bcc($bcc) - { - $this->getSwiftMessage()->setBcc($bcc); - return $this; - } - - /** - * @inheritdoc - */ - public function subject($subject) - { - $this->getSwiftMessage()->setSubject($subject); - return $this; - } - - /** - * @inheritdoc - */ - public function textBody($text) - { - $this->setBody($text, 'text/plain'); - return $this; - } - - /** - * @inheritdoc - */ - public function htmlBody($html) - { - $this->setBody($html, 'text/html'); - return $this; - } - - /** - * Sets the message body. - * If body is already set and its content type matches given one, it will - * be overridden, if content type miss match the multipart message will be composed. - * @param string $body body content. - * @param string $contentType body content type. - */ - protected function setBody($body, $contentType) - { - $message = $this->getSwiftMessage(); - $oldBody = $message->getBody(); - if (empty($oldBody)) { - $parts = $message->getChildren(); - $partFound = false; - foreach ($parts as $key => $part) { - if (!($part instanceof \Swift_Mime_Attachment)) { - /* @var $part \Swift_Mime_MimePart */ - if ($part->getContentType() == $contentType) { - unset($parts[$key]); - $partFound = true; - break; - } - } - } - if ($partFound) { - reset($parts); - $message->setChildren($parts); - $message->addPart($body, $contentType); - } else { - $message->setBody($body, $contentType); - } - } else { - $oldContentType = $message->getContentType(); - if ($oldContentType == $contentType) { - $message->setBody($body, $contentType); - } else { - $message->setBody(null); - $message->setContentType(null); - $message->addPart($oldBody, $oldContentType); - $message->addPart($body, $contentType); - } - } - } - - /** - * @inheritdoc - */ - public function attach($fileName, array $options = []) - { - $attachment = \Swift_Attachment::fromPath($fileName); - if (!empty($options['fileName'])) { - $attachment->setFilename($options['fileName']); - } - if (!empty($options['contentType'])) { - $attachment->setContentType($options['contentType']); - } - $this->getSwiftMessage()->attach($attachment); - return $this; - } - - /** - * @inheritdoc - */ - public function attachContent($content, array $options = []) - { - $attachment = \Swift_Attachment::newInstance($content); - if (!empty($options['fileName'])) { - $attachment->setFilename($options['fileName']); - } - if (!empty($options['contentType'])) { - $attachment->setContentType($options['contentType']); - } - $this->getSwiftMessage()->attach($attachment); - return $this; - } - - /** - * @inheritdoc - */ - public function embed($fileName, array $options = []) - { - $embedFile = \Swift_EmbeddedFile::fromPath($fileName); - if (!empty($options['fileName'])) { - $embedFile->setFilename($options['fileName']); - } - if (!empty($options['contentType'])) { - $embedFile->setContentType($options['contentType']); - } - return $this->getSwiftMessage()->embed($embedFile); - } - - /** - * @inheritdoc - */ - public function embedContent($content, array $options = []) - { - $embedFile = \Swift_EmbeddedFile::newInstance($content); - if (!empty($options['fileName'])) { - $embedFile->setFilename($options['fileName']); - } - if (!empty($options['contentType'])) { - $embedFile->setContentType($options['contentType']); - } - return $this->getSwiftMessage()->embed($embedFile); - } - - /** - * @inheritdoc - */ - public function toString() - { - return $this->getSwiftMessage()->toString(); - } - - /** - * Creates the Swift email message instance. - * @return \Swift_Message email message instance. - */ - protected function createSwiftMessage() - { - return new \Swift_Message(); - } -} \ No newline at end of file