-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Proposal for #31 #1011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Proposal for #31 #1011
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1a2e73b
Email components hierarchy created as draft.
klimov-paul dc541ad
Class 'BaseMessage' extracted
klimov-paul cbdce0d
VendorMessage virtual property management reworked
klimov-paul a1c702f
Base email message interface composed.
klimov-paul 117b9a0
Class 'yii\email\Message' added.
klimov-paul 5d48d7f
Default configuration for the email messages have been added.
klimov-paul 6c7a0a8
Swift mailer autoloader rewised.
klimov-paul 3f44d5d
VendorMailer::autoloader processing improved.
klimov-paul 53df7a1
Unit test for 'email' styled up
klimov-paul 358e013
'swiftmailer' group excluded from travis
klimov-paul 45d364c
Merge branch 'master' of github.com:yiisoft/yii2 into email-swift
klimov-paul cb0bbb2
Setters at \yii\email\message reworked to return void instead of self…
klimov-paul 94f4b09
Methods 'createAttachment' and 'attachFile' added to \yii\email\BaseM…
klimov-paul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
| /** | ||
| * @link http://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license http://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| namespace yii\email; | ||
|
|
||
| use yii\base\Component; | ||
|
|
||
| /** | ||
| * BaseMailer provides the basic interface for the email mailer application component. | ||
| * It provides the default configuration for the email messages. | ||
| * Particular implementation of mailer should provide implementation for the [[send()]] method. | ||
| * | ||
| * @see BaseMessage | ||
| * | ||
| * @property array $defaultMessageConfig configuration, which should be applied by default to any | ||
| * new created email message instance. | ||
| * | ||
| * @author Paul Klimov <klimov.paul@gmail.com> | ||
| * @since 2.0 | ||
| */ | ||
| abstract class BaseMailer extends Component | ||
| { | ||
| /** | ||
| * @var array configuration, which should be applied by default to any new created | ||
| * email message instance. | ||
| * For example: | ||
| * ~~~ | ||
| * array( | ||
| * 'encoding' => 'UTF-8', | ||
| * 'from' => 'noreply@mydomain.com', | ||
| * 'bcc' => 'email.test@mydomain.com', | ||
| * ) | ||
| * ~~~ | ||
| */ | ||
| private $_defaultMessageConfig = array(); | ||
|
|
||
| /** | ||
| * @param array $defaultMessageConfig default message config | ||
| */ | ||
| public function setDefaultMessageConfig(array $defaultMessageConfig) | ||
| { | ||
| $this->_defaultMessageConfig = $defaultMessageConfig; | ||
| } | ||
|
|
||
| /** | ||
| * @return array default message config | ||
| */ | ||
| public function getDefaultMessageConfig() | ||
| { | ||
| return $this->_defaultMessageConfig; | ||
| } | ||
|
|
||
| /** | ||
| * Sends the given email message. | ||
| * @param object $message email message instance | ||
| * @return boolean whether the message has been sent. | ||
| */ | ||
| abstract public function send($message); | ||
|
|
||
| /** | ||
| * Sends a couple of messages at once. | ||
| * Note: some particular mailers may benefit from sending messages as batch, | ||
| * saving resources, for example on open/close connection operations, | ||
| * they may override this method to create their specific implementation. | ||
| * @param array $messages list of email messages, which should be sent. | ||
| * @return integer number of successfull sends | ||
| */ | ||
| public function sendMultiple(array $messages) { | ||
| $successCount = 0; | ||
| foreach ($messages as $message) { | ||
| if ($this->send($message)) { | ||
| $successCount++; | ||
| } | ||
| } | ||
| return $successCount; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| <?php | ||
| /** | ||
| * @link http://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license http://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| namespace yii\email; | ||
|
|
||
| use yii\base\InvalidParamException; | ||
| use yii\base\Object; | ||
| use yii\helpers\FileHelper; | ||
| use Yii; | ||
|
|
||
| /** | ||
| * BaseMessage represent the single email message. | ||
| * It functionality depends on application component 'email', | ||
| * which should provide the actual email sending functionality as well as | ||
| * default message configuration. | ||
| * | ||
| * @see BaseMailer | ||
| * | ||
| * @property \yii\email\BaseMailer $mailer mailer component instance. This property is read-only. | ||
| * @property string|array $from sender email address, if array is given, its first element should | ||
| * be sender email address, second - sender name. | ||
| * @property string|array $to receiver email address, if array is given, its first element should | ||
| * be receiver email address, second - receiver name. | ||
| * @property string $subject message subject. | ||
| * @property string $text message plain text content. | ||
| * @property string $html message HTML content. | ||
| * | ||
| * @author Paul Klimov <klimov.paul@gmail.com> | ||
| * @since 2.0 | ||
| */ | ||
| abstract class BaseMessage extends Object | ||
| { | ||
| /** | ||
| * @return \yii\email\BaseMailer | ||
| */ | ||
| public function getMailer() | ||
| { | ||
| return Yii::$app->getComponent('email'); | ||
| } | ||
|
|
||
| /** | ||
| * Initializes the object. | ||
| * This method is invoked at the end of the constructor after the object is initialized with the | ||
| * given configuration. | ||
| */ | ||
| public function init() | ||
| { | ||
| Yii::configure($this, $this->getMailer()->getDefaultMessageConfig()); | ||
| } | ||
|
|
||
| /** | ||
| * Sends this email message. | ||
| * @return boolean success. | ||
| */ | ||
| public function send() | ||
| { | ||
| return $this->getMailer()->send($this); | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| abstract public function setFrom($from); | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| abstract public function setTo($to); | ||
|
|
||
| /** | ||
| * Sets message subject. | ||
| * @param string $subject message subject | ||
| */ | ||
| abstract public function setSubject($subject); | ||
|
|
||
| /** | ||
| * Sets message plain text content. | ||
| * @param string $text message plain text content. | ||
| */ | ||
| abstract public function setText($text); | ||
|
|
||
| /** | ||
| * Sets message HTML content. | ||
| * @param string $html message HTML content. | ||
| */ | ||
| abstract public function setHtml($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. | ||
| */ | ||
| abstract public function createAttachment($content, $fileName, $contentType = 'application/octet-stream'); | ||
|
|
||
| /** | ||
| * Attaches existing file to the email message. | ||
| * @param string $fileName full file name | ||
| * @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically. | ||
| * @param string $attachFileName name, which should be used for attachment, if empty file base name will be used. | ||
| * @throws \yii\base\InvalidParamException if given file does not exist. | ||
| */ | ||
| public function attachFile($fileName, $contentType = null, $attachFileName = null) | ||
| { | ||
| if (!file_exists($fileName)) { | ||
| throw new InvalidParamException('Unable to attach file "' . $fileName . '": file does not exists!'); | ||
| } | ||
| if (empty($contentType)) { | ||
| $contentType = FileHelper::getMimeType($fileName); | ||
| } | ||
| if (empty($attachFileName)) { | ||
| $attachFileName = basename($fileName); | ||
| } | ||
| $content = file_get_contents($fileName); | ||
| $this->createAttachment($content, $attachFileName, $contentType); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
| /** | ||
| * @link http://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license http://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| namespace yii\email; | ||
|
|
||
| use yii\email\swift\Message as SwiftMessage; | ||
|
|
||
| /** | ||
| * Message provides the email message sending functionality. | ||
| * | ||
| * Usage: | ||
| * ~~~ | ||
| * $email = new Message(); | ||
| * $email->from = 'sender@domain.com'; | ||
| * $email->to = 'receiver@domain.com'; | ||
| * $email->subject = 'Message Subject'; | ||
| * $email->text = 'Message Content'; | ||
| * $email->send(); | ||
| * ~~~ | ||
| * | ||
| * This particular class uses 'SwiftMailer' library to perform the message sending. | ||
| * Note: you can replace usage of this class by your own one, using [[Yii::$classMap]]: | ||
| * ~~~ | ||
| * Yii::$classMap['yii\email\Message'] = '/path/to/my/email/Message.php' | ||
| * ~~~ | ||
| * | ||
| * @author Paul Klimov <klimov.paul@gmail.com> | ||
| * @since 2.0 | ||
| */ | ||
| class Message extends SwiftMessage {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
| /** | ||
| * @link http://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license http://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| namespace yii\email; | ||
|
|
||
| use yii\base\InvalidConfigException; | ||
|
|
||
| /** | ||
| * VendorMailer is a base class for the mailers, which use external library (vendor) | ||
| * to perform their job. | ||
| * This class provides the ability to compose and wrap the actual email mailer | ||
| * and message under the basic interface. | ||
| * | ||
| * @see VendorMessage | ||
| * | ||
| * @property array|object $vendorMailer vendor mailer instance or its array configuration. | ||
| * | ||
| * @author Paul Klimov <klimov.paul@gmail.com> | ||
| * @since 2.0 | ||
| */ | ||
| abstract class VendorMailer extends BaseMailer | ||
| { | ||
| /** | ||
| * @var string|callback vendor autoloader callback or path to autoloader file. | ||
| * If the vendor classes autoloading is already managed in some other place, | ||
| * for example via Composer, you should leave this field blank. | ||
| */ | ||
| public $autoload; | ||
| /** | ||
| * @var array|object vendor mailer instance or its array configuration. | ||
| * Note: different implementation of [[VendorMailer]] may process configuration | ||
| * in the different way. | ||
| */ | ||
| private $_vendorMailer = array(); | ||
|
|
||
| /** | ||
| * Initializes the object. | ||
| * Registers the vendor autoloader, if any. | ||
| */ | ||
| public function init() | ||
| { | ||
| $this->setupVendorAutoload(); | ||
| } | ||
|
|
||
| /** | ||
| * @param array|object $value mailer instance or configuration. | ||
| * @throws \yii\base\InvalidConfigException on invalid argument. | ||
| */ | ||
| public function setVendorMailer($value) | ||
| { | ||
| if (!is_array($value) && !is_object($value)) { | ||
| throw new InvalidConfigException('"' . get_class($this) . '::vendorMailer" should be either object or array, "' . gettype($value) . '" given.'); | ||
| } | ||
| $this->_vendorMailer = $value; | ||
| } | ||
|
|
||
| /** | ||
| * @return object vendor mailer instance. | ||
| */ | ||
| public function getVendorMailer() | ||
| { | ||
| if (!is_object($this->_vendorMailer)) { | ||
| $this->_vendorMailer = $this->createVendorMailer($this->_vendorMailer); | ||
| } | ||
| return $this->_vendorMailer; | ||
| } | ||
|
|
||
| /** | ||
| * Sets up the vendor autoloader, if any is specified. | ||
| */ | ||
| protected function setupVendorAutoload() | ||
| { | ||
| if (!empty($this->autoload)) { | ||
| if (is_string($this->autoload) && file_exists($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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates vendor mailer instance from given configuration. | ||
| * @param array $config mailer configuration. | ||
| * @return object mailer instance. | ||
| */ | ||
| abstract protected function createVendorMailer(array $config); | ||
|
|
||
| /** | ||
| * Creates the vendor email message instance. | ||
| * @return object email message instance. | ||
| */ | ||
| abstract public function createVendorMessage(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just a number? Isn't an array of errors better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Particular libraries may have a problem with that.