Browse Source

Refactored MessageInterface::send().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
bd8f74cf12
  1. 20
      framework/yii/log/EmailTarget.php
  2. 15
      framework/yii/mail/BaseMessage.php
  3. 4
      framework/yii/mail/MessageInterface.php
  4. 9
      tests/unit/framework/mail/BaseMessageTest.php

20
framework/yii/log/EmailTarget.php

@ -60,21 +60,21 @@ class EmailTarget extends Target
*/
public function export()
{
$message = $this->mail->compose();
Yii::configure($message, $this->message);
$this->composeMessage($message);
$this->mail->send($message);
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$this->composeMessage($body)->send($this->mail);
}
/**
* Composes the given mail message with body content.
* The default implementation fills the text body of the message with the log messages.
* @param \yii\mail\MessageInterface $message
* Composes a mail message with the given body content.
* @param string $body the body content
* @return \yii\mail\MessageInterface $message
*/
protected function composeMessage($message)
protected function composeMessage($body)
{
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$message = $this->mail->compose();
Yii::configure($message, $this->message);
$message->setTextBody($body);
return $message;
}
}

15
framework/yii/mail/BaseMessage.php

@ -26,19 +26,14 @@ use Yii;
abstract class BaseMessage extends Object implements MessageInterface
{
/**
* @return MailerInterface the mailer component
*/
public function getMailer()
{
return Yii::$app->getComponent('mail');
}
/**
* {@inheritdoc}
*/
public function send()
public function send(MailerInterface $mailer = null)
{
return $this->getMailer()->send($this);
if ($mailer === null) {
$mailer = Yii::$app->getMail();
}
return $mailer->send($this);
}
/**

4
framework/yii/mail/MessageInterface.php

@ -204,9 +204,11 @@ interface MessageInterface
/**
* Sends this email message.
* @param MailerInterface $mailer the mailer that should be used to send this message.
* If null, the "mail" application component will be used instead.
* @return boolean whether this message is sent successfully.
*/
public function send();
public function send(MailerInterface $mailer = null);
/**
* Returns string representation of this message.

9
tests/unit/framework/mail/BaseMessageTest.php

@ -40,18 +40,11 @@ class BaseMessageTest extends TestCase
// Tests :
public function testGetMailer()
{
$mailer = $this->getMailer();
$message = $mailer->compose();
$this->assertEquals($mailer, $message->getMailer());
}
public function testSend()
{
$mailer = $this->getMailer();
$message = $mailer->compose();
$message->send();
$message->send($mailer);
$this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
}

Loading…
Cancel
Save