Browse Source

'email' renamed to 'mail'. 'SwiftMailer' recomposed into extension.

tags/2.0.0-alpha
Paul Klimov 12 years ago
commit
6eec352d2d
  1. 32
      LICENSE.md
  2. 58
      README.md
  3. 28
      composer.json
  4. 143
      yii/swiftmailer/Mailer.php
  5. 126
      yii/swiftmailer/Message.php

32
LICENSE.md

@ -0,0 +1,32 @@
The Yii framework is free software. It is released under the terms of
the following BSD License.
Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii Software LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

58
README.md

@ -0,0 +1,58 @@
Yii 2.0 Public Preview - SwiftMailer Mail Solution
==================================================
Thank you for choosing Yii - a high-performance component-based PHP framework.
If you are looking for a production-ready PHP framework, please use
[Yii v1.1](https://github.com/yiisoft/yii).
Yii 2.0 is still under heavy development. We may make significant changes
without prior notices. **Yii 2.0 is not ready for production use yet.**
[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2)
This is the yii2-swiftmailer extension.
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
php composer.phar require yiisoft/yii2-swiftmailer "*"
```
or add
```json
"yiisoft/yii2-swiftmailer": "*"
```
to the require section of your composer.json.
*Note: You might have to run `php composer.phar selfupdate`*
Usage & Documentation
---------------------
This extension has to be registered prior to usage.
To enable this view renderer add it to the $rendereres property of your view object.
Example:
```php
<?php
// config.php
return [
//....
'components' => [
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
],
],
];
```
For further instructions refer to the related section in the yii guide.

28
composer.json

@ -0,0 +1,28 @@
{
"name": "yiisoft/yii2-swiftmailer",
"description": "The SwiftMailer integration for the Yii framework",
"keywords": ["yii", "swift", "swiftmailer", "mail", "email", "mailer"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2/issues?state=open",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2"
},
"authors": [
{
"name": "Paul Klimov",
"email": "klimov.paul@gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "*",
"swiftmailer/swiftmailer": ">=v5.0"
},
"autoload": {
"psr-0": { "yii\\swiftmailer\\": "" }
}
}

143
yii/swiftmailer/Mailer.php

@ -0,0 +1,143 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\swiftmailer;
use yii\base\InvalidConfigException;
use yii\email\BaseMailer;
/**
* Mailer based on SwiftMailer library.
*
* By default PHP 'mail' function will be used as default email transport.
* You can setup different email transport via [[vendorMailer]] property:
* ~~~
* 'components' => array(
* ...
* 'email' => array(
* 'class' => 'yii\email\swift\Mailer',
* 'transport' => [
* 'class' => 'Swift_SmtpTransport',
* 'host' => 'localhost',
* 'username' => 'username',
* 'password' => 'password',
* 'port' => '587',
* 'encryption' => 'tls',
* ],
* ),
* ...
* ),
* ~~~
*
* @see http://swiftmailer.org
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Mailer extends BaseMailer
{
/**
* @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)
{
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;
}
/**
* Creates the Swift email message instance.
* @return \Swift_Message email message instance.
*/
public function createSwiftMessage()
{
return new \Swift_Message();
}
}

126
yii/swiftmailer/Message.php

@ -0,0 +1,126 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\swiftmailer;
use yii\email\BaseMessage;
/**
* Email message based on SwiftMailer library.
*
* @see http://swiftmailer.org/docs/messages.html
* @see \yii\swiftmailer\Mailer
*
* @method Mailer getMailer() returns mailer instance.
* @property \Swift_Message $swiftMessage vendor message instance.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @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->getMailer()->createSwiftMessage();
}
return $this->_swiftMessage;
}
/**
* @inheritdoc
*/
public function setFrom($from)
{
$this->getSwiftMessage()->setFrom($from);
$this->getSwiftMessage()->setReplyTo($from);
}
/**
* @inheritdoc
*/
public function setTo($to)
{
$this->getSwiftMessage()->setTo($to);
}
/**
* @inheritdoc
*/
public function setCc($cc)
{
$this->getSwiftMessage()->setCc($cc);
}
/**
* @inheritdoc
*/
public function setBcc($bcc)
{
$this->getSwiftMessage()->setBcc($bcc);
}
/**
* @inheritdoc
*/
public function setSubject($subject)
{
$this->getSwiftMessage()->setSubject($subject);
}
/**
* @inheritdoc
*/
public function setText($text)
{
$this->getSwiftMessage()->setBody($text, 'text/plain');
}
/**
* @inheritdoc
*/
public function setHtml($html)
{
$this->getSwiftMessage()->setBody($html, 'text/html');
}
/**
* @inheritdoc
*/
public function addText($text)
{
$this->getSwiftMessage()->addPart($text, 'text/plain');
}
/**
* @inheritdoc
*/
public function addHtml($html)
{
$this->getSwiftMessage()->addPart($html, 'text/html');
}
/**
* @inheritdoc
*/
public function createAttachment($content, $fileName, $contentType = 'application/octet-stream')
{
if (empty($contentType)) {
$contentType = 'application/octet-stream';
}
$attachment = \Swift_Attachment::newInstance($content, $fileName, $contentType);
$this->getSwiftMessage()->attach($attachment);
}
}
Loading…
Cancel
Save