Browse Source

Refactored mail.

tags/2.0.0-alpha
Qiang Xue 12 years ago
parent
commit
ae0e0b4375
  1. 31
      Mailer.php
  2. 88
      Message.php

31
Mailer.php

@ -12,10 +12,10 @@ use yii\base\InvalidConfigException;
use yii\mail\BaseMailer;
/**
* Mailer based on SwiftMailer library.
* Mailer implements a mailer based on SwiftMailer.
*
* To use Mailer, you should configure it in the application configuration like the following,
*
* By default PHP 'mail' function will be used as default email transport.
* You can setup different email transport via [[vendorMailer]] property:
* ~~~
* 'components' => array(
* ...
@ -34,9 +34,20 @@ use yii\mail\BaseMailer;
* ),
* ~~~
*
* @see http://swiftmailer.org
* You may also skip the configuration of the [[transport]] property. In that case, the default
* PHP `mail()` function will be used to send emails.
*
* To send an email, you may use the following code:
*
* ~~~
* Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
* ->setFrom('from@domain.com')
* ->setTo($form->email)
* ->setSubject($form->subject)
* ->send();
* ~~~
*
* @method Message compose($view = null, array $params = []) creates new message optionally filling up its body via view rendering.
* @see http://swiftmailer.org
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
@ -69,7 +80,7 @@ class Mailer extends BaseMailer
/**
* @param array|\Swift_Transport $transport
* @throws \yii\base\InvalidConfigException on invalid argument.
* @throws InvalidConfigException on invalid argument.
*/
public function setTransport($transport)
{
@ -95,8 +106,12 @@ class Mailer extends BaseMailer
*/
public function send($message)
{
Yii::trace('Sending email message', __METHOD__);
return ($this->getSwiftMailer()->send($message->getSwiftMessage()) > 0);
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', $address);
}
Yii::trace('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
return $this->getSwiftMailer()->send($message->getSwiftMessage()) > 0;
}
/**

88
Message.php

@ -10,10 +10,10 @@ namespace yii\swiftmailer;
use yii\mail\BaseMessage;
/**
* Email message based on SwiftMailer library.
* Message implements a message class based on SwiftMailer.
*
* @see http://swiftmailer.org/docs/messages.html
* @see \yii\swiftmailer\Mailer
* @see Mailer
*
* @method Mailer getMailer() returns mailer instance.
* @property \Swift_Message $swiftMessage vendor message instance.
@ -42,7 +42,15 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function charset($charset)
public function getCharset()
{
$this->getSwiftMessage()->getCharset();
}
/**
* @inheritdoc
*/
public function setCharset($charset)
{
$this->getSwiftMessage()->setCharset($charset);
return $this;
@ -51,17 +59,49 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function from($from)
public function getFrom()
{
$this->getSwiftMessage()->getFrom();
}
/**
* @inheritdoc
*/
public function setFrom($from)
{
$this->getSwiftMessage()->setFrom($from);
$this->getSwiftMessage()->setReplyTo($from);
return $this;
}
/**
* @inheritdoc
*/
public function to($to)
public function getReplyTo()
{
$this->getSwiftMessage()->getReplyTo();
}
/**
* @inheritdoc
*/
public function setReplyTo($replyTo)
{
$this->getSwiftMessage()->setReplyTo($replyTo);
return $this;
}
/**
* @inheritdoc
*/
public function getTo()
{
$this->getSwiftMessage()->getTo();
}
/**
* @inheritdoc
*/
public function setTo($to)
{
$this->getSwiftMessage()->setTo($to);
return $this;
@ -70,7 +110,15 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function cc($cc)
public function getCc()
{
$this->getSwiftMessage()->getCc();
}
/**
* @inheritdoc
*/
public function setCc($cc)
{
$this->getSwiftMessage()->setCc($cc);
return $this;
@ -79,7 +127,15 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function bcc($bcc)
public function getBcc()
{
$this->getSwiftMessage()->getBcc();
}
/**
* @inheritdoc
*/
public function setBcc($bcc)
{
$this->getSwiftMessage()->setBcc($bcc);
return $this;
@ -88,7 +144,15 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function subject($subject)
public function getSubject()
{
$this->getSwiftMessage()->getSubject();
}
/**
* @inheritdoc
*/
public function setSubject($subject)
{
$this->getSwiftMessage()->setSubject($subject);
return $this;
@ -97,7 +161,7 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function textBody($text)
public function setTextBody($text)
{
$this->setBody($text, 'text/plain');
return $this;
@ -106,7 +170,7 @@ class Message extends BaseMessage
/**
* @inheritdoc
*/
public function htmlBody($html)
public function setHtmlBody($html)
{
$this->setBody($html, 'text/html');
return $this;
@ -234,4 +298,4 @@ class Message extends BaseMessage
{
return new \Swift_Message();
}
}
}

Loading…
Cancel
Save