Browse Source

minor touch up of swiftmailer.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
ed0e44d23b
  1. 7
      extensions/swiftmailer/Mailer.php
  2. 63
      extensions/swiftmailer/README.md
  3. 5
      framework/yii/mail/BaseMailer.php
  4. 2
      framework/yii/mail/BaseMessage.php
  5. 5
      framework/yii/mail/MailerInterface.php
  6. 9
      framework/yii/mail/MessageInterface.php

7
extensions/swiftmailer/Mailer.php

@ -7,9 +7,9 @@
namespace yii\swiftmailer; namespace yii\swiftmailer;
use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\mail\BaseMailer; use yii\mail\BaseMailer;
use Yii;
/** /**
* Mailer based on SwiftMailer library. * Mailer based on SwiftMailer library.
@ -116,13 +116,14 @@ class Mailer extends BaseMailer
*/ */
protected function createTransport(array $config) protected function createTransport(array $config)
{ {
if (array_key_exists('class', $config)) { if (isset($config['class'])) {
$className = $config['class']; $className = $config['class'];
unset($config['class']); unset($config['class']);
} else { } else {
$className = 'Swift_MailTransport'; $className = 'Swift_MailTransport';
} }
$transport = call_user_func([$className, 'newInstance']); /** @var \Swift_MailTransport $transport */
$transport = $className::newInstance();
if (!empty($config)) { if (!empty($config)) {
foreach ($config as $name => $value) { foreach ($config as $name => $value) {
if (property_exists($transport, $name)) { if (property_exists($transport, $name)) {

63
extensions/swiftmailer/README.md

@ -1,17 +1,32 @@
Yii 2.0 Public Preview - SwiftMailer Mail Solution SwiftMailer Extension for Yii 2
================================================== ===============================
Thank you for choosing Yii - a high-performance component-based PHP framework. This extension provides a `SwiftMailer` mail solution for Yii 2.
If you are looking for a production-ready PHP framework, please use To use this extension, simply add the following code in your application configuration:
[Yii v1.1](https://github.com/yiisoft/yii).
Yii 2.0 is still under heavy development. We may make significant changes ```php
without prior notices. **Yii 2.0 is not ready for production use yet.** return [
//....
'components' => [
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
],
],
];
```
You can then send an email as follows:
[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) ```php
Yii::$app->mail->compose('contact/html')
->from('from@domain.com')
->to($form->email)
->subject($form->subject)
->send();
```
This is the yii2-swiftmailer extension. For further instructions refer to the related section in the Yii Definitive Guide.
Installation Installation
@ -20,39 +35,15 @@ Installation
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run Either run
``` ```
php composer.phar require yiisoft/yii2-swiftmailer "*" php composer.phar require yiisoft/yii2-swiftmailer "*"
``` ```
or add or add
```json ```json
"yiisoft/yii2-swiftmailer": "*" "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. to the require section of your composer.json.

5
framework/yii/mail/BaseMailer.php

@ -7,9 +7,9 @@
namespace yii\mail; namespace yii\mail;
use Yii;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use Yii;
use yii\base\ViewContextInterface; use yii\base\ViewContextInterface;
/** /**
@ -46,6 +46,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* @var array configuration, which should be applied by default to any new created * @var array configuration, which should be applied by default to any new created
* email message instance. * email message instance.
* In addition to normal [[Yii::createObject()]] behavior extra config keys are available: * In addition to normal [[Yii::createObject()]] behavior extra config keys are available:
*
* - 'charset' argument for [[MessageInterface::charset()]] * - 'charset' argument for [[MessageInterface::charset()]]
* - 'from' argument for [[MessageInterface::from()]] * - 'from' argument for [[MessageInterface::from()]]
* - 'to' argument for [[MessageInterface::to()]] * - 'to' argument for [[MessageInterface::to()]]
@ -54,7 +55,9 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
* - 'subject' argument for [[MessageInterface::subject()]] * - 'subject' argument for [[MessageInterface::subject()]]
* - 'textBody' argument for [[MessageInterface::textBody()]] * - 'textBody' argument for [[MessageInterface::textBody()]]
* - 'htmlBody' argument for [[MessageInterface::htmlBody()]] * - 'htmlBody' argument for [[MessageInterface::htmlBody()]]
*
* For example: * For example:
*
* ~~~ * ~~~
* array( * array(
* 'charset' => 'UTF-8', * 'charset' => 'UTF-8',

2
framework/yii/mail/BaseMessage.php

@ -7,9 +7,7 @@
namespace yii\mail; namespace yii\mail;
use yii\base\InvalidParamException;
use yii\base\Object; use yii\base\Object;
use yii\helpers\FileHelper;
use Yii; use Yii;
/** /**

5
framework/yii/mail/MailerInterface.php

@ -11,6 +11,7 @@ namespace yii\mail;
* MailerInterface is an interface, which any mailer should apply. * MailerInterface is an interface, which any mailer should apply.
* Mailer creates and sends messages. Also it allows composition of the message * Mailer creates and sends messages. Also it allows composition of the message
* body via view rendering: * body via view rendering:
*
* ~~~php * ~~~php
* Yii::$app->mail->compose('contact/html', ['contactForm' => $form]) * Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
* ->from('from@domain.com') * ->from('from@domain.com')
@ -29,12 +30,16 @@ interface MailerInterface
/** /**
* Creates new message optionally filling up its body via view rendering. * Creates new message optionally filling up its body via view rendering.
* The view to be rendered can be specified in one of the following formats: * The view to be rendered can be specified in one of the following formats:
*
* - path alias (e.g. "@app/mails/contact/body"); * - path alias (e.g. "@app/mails/contact/body");
* - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]]. * - relative path (e.g. "contact"): the actual view file will be resolved by [[\yii\base\ViewContextInterface]].
*
* @param string|array $view view, which should be used to render message body * @param string|array $view view, which should be used to render message body
*
* - if string - the view name or the path alias of the HTML body view file, in this case * - if string - the view name or the path alias of the HTML body view file, in this case
* text body will be composed automatically from html one. * text body will be composed automatically from html one.
* - if array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView'] * - if array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView']
*
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return MessageInterface message instance. * @return MessageInterface message instance.
*/ */

9
framework/yii/mail/MessageInterface.php

@ -11,6 +11,7 @@ namespace yii\mail;
* MessageInterface is an interface, which email message should apply. * MessageInterface is an interface, which email message should apply.
* Together with application component, which matches the [[MailerInterface]], * Together with application component, which matches the [[MailerInterface]],
* it introduces following mail sending syntax: * it introduces following mail sending syntax:
*
* ~~~php * ~~~php
* Yii::$app->mail->compose() * Yii::$app->mail->compose()
* ->from('from@domain.com') * ->from('from@domain.com')
@ -100,8 +101,10 @@ interface MessageInterface
* Attaches existing file to the email message. * Attaches existing file to the email message.
* @param string $fileName full file name * @param string $fileName full file name
* @param array $options options for embed file. Valid options are: * @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type. * - contentType: attached file MIME type.
*
* @return static self reference. * @return static self reference.
*/ */
public function attach($fileName, array $options = []); public function attach($fileName, array $options = []);
@ -110,8 +113,10 @@ interface MessageInterface
* Attach specified content as file for the email message. * Attach specified content as file for the email message.
* @param string $content attachment file content. * @param string $content attachment file content.
* @param array $options options for embed file. Valid options are: * @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type. * - contentType: attached file MIME type.
*
* @return static self reference. * @return static self reference.
*/ */
public function attachContent($content, array $options = []); public function attachContent($content, array $options = []);
@ -121,8 +126,10 @@ interface MessageInterface
* This method should be used when embedding images or other data in a message. * This method should be used when embedding images or other data in a message.
* @param string $fileName file name. * @param string $fileName file name.
* @param array $options options for embed file. Valid options are: * @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type. * - contentType: attached file MIME type.
*
* @return string attachment CID. * @return string attachment CID.
*/ */
public function embed($fileName, array $options = []); public function embed($fileName, array $options = []);
@ -132,8 +139,10 @@ interface MessageInterface
* This method should be used when embedding images or other data in a message. * This method should be used when embedding images or other data in a message.
* @param string $content attachment file content. * @param string $content attachment file content.
* @param array $options options for embed file. Valid options are: * @param array $options options for embed file. Valid options are:
*
* - fileName: name, which should be used to attach file. * - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type. * - contentType: attached file MIME type.
*
* @return string attachment CID. * @return string attachment CID.
*/ */
public function embedContent($content, array $options = []); public function embedContent($content, array $options = []);

Loading…
Cancel
Save