mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-19 07:50:39 +08:00
Email components hierarchy created.
SwiftMailer applied as email solution.
This commit is contained in:
180
framework/yii/email/swift/Mailer.php
Normal file
180
framework/yii/email/swift/Mailer.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\email\swift;
|
||||
|
||||
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 string|callback SwiftMailer autoloader callback or path to autoloader file.
|
||||
* If the SwiftMailer classes autoloading is already managed in some other place,
|
||||
* for example via Composer, you should leave this field blank.
|
||||
*/
|
||||
public $autoload;
|
||||
/**
|
||||
* @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 init()
|
||||
{
|
||||
$this->setupSwiftMailerAutoload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the SwiftMailer autoloader, if it is specified.
|
||||
*/
|
||||
protected function setupSwiftMailerAutoload()
|
||||
{
|
||||
if (!class_exists('Swift', false)) {
|
||||
if (empty($this->autoload)) {
|
||||
$this->autoload = __DIR__ . '/autoload.php';
|
||||
}
|
||||
if (is_string($this->autoload)) {
|
||||
if (file_exists($this->autoload)) {
|
||||
require_once($this->autoload);
|
||||
} elseif (function_exists($this->autoload)) {
|
||||
spl_autoload_register($this->autoload);
|
||||
} else {
|
||||
throw new InvalidConfigException('"' . get_class($this) . '::autoload" value "' . $this->autoload . '" is invalid: no such function or file exists.');
|
||||
}
|
||||
} else {
|
||||
spl_autoload_register($this->autoload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
116
framework/yii/email/swift/Message.php
Normal file
116
framework/yii/email/swift/Message.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\email\swift;
|
||||
|
||||
use yii\email\BaseMessage;
|
||||
|
||||
/**
|
||||
* Email message based on SwiftMailer library.
|
||||
*
|
||||
* @see http://swiftmailer.org/docs/messages.html
|
||||
* @see \yii\email\swift\Mailer
|
||||
*
|
||||
* @method \yii\email\swift\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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets message sender.
|
||||
* @param string|array $from sender email address, if array is given,
|
||||
* its first element should be sender email address, second - sender name.
|
||||
*/
|
||||
public function setFrom($from)
|
||||
{
|
||||
if (is_array($from)) {
|
||||
list ($address, $name) = $from;
|
||||
} else {
|
||||
$address = $from;
|
||||
$name = null;
|
||||
}
|
||||
$this->getSwiftMessage()->setFrom($address, $name);
|
||||
$this->getSwiftMessage()->setReplyTo($address, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets message receiver.
|
||||
* @param string|array $to receiver email address, if array is given,
|
||||
* its first element should be receiver email address, second - receiver name.
|
||||
*/
|
||||
public function setTo($to)
|
||||
{
|
||||
if (is_array($to)) {
|
||||
list ($address, $name) = $to;
|
||||
} else {
|
||||
$address = $to;
|
||||
$name = null;
|
||||
}
|
||||
$this->getSwiftMessage()->setTo($address, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets message subject.
|
||||
* @param string $subject message subject
|
||||
*/
|
||||
public function setSubject($subject)
|
||||
{
|
||||
$this->getSwiftMessage()->setSubject($subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets message plain text content.
|
||||
* @param string $text message plain text content.
|
||||
*/
|
||||
public function setText($text)
|
||||
{
|
||||
$this->getSwiftMessage()->setBody($text, 'text/plain');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets message HTML content.
|
||||
* @param string $html message HTML content.
|
||||
*/
|
||||
public function setHtml($html)
|
||||
{
|
||||
$this->getSwiftMessage()->setBody($html, 'text/html');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create file attachment for the email message.
|
||||
* @param string $content - attachment file content.
|
||||
* @param string $fileName - attachment file name.
|
||||
* @param string $contentType - MIME type of the attachment file, by default 'application/octet-stream' will be used.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
9
framework/yii/email/swift/autoload.php
Normal file
9
framework/yii/email/swift/autoload.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* This file attempts to register autoloader for the Swift library, assuming
|
||||
* it is located under the 'vendor' path.
|
||||
*
|
||||
* @var $this \yii\email\swift\Mailer
|
||||
*/
|
||||
$swiftMailerLibPath = Yii::getAlias('@vendor/swiftmailer/swiftmailer/lib');
|
||||
require_once $swiftMailerLibPath . '/swift_required.php';
|
||||
Reference in New Issue
Block a user