Class yii\swiftmailer\Message
| Inheritance | yii\swiftmailer\Message » yii\mail\BaseMessage |
|---|---|
| Available since extension's version | 2.0 |
| Source Code | https://github.com/yiisoft/yii2-swiftmailer/blob/master/src/Message.php |
Message implements a message class based on SwiftMailer.
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $headers | array | Headers in format: [name => value]. |
yii\swiftmailer\Message |
| $priority | integer | Priority value as integer in range: 1..5, where 1 is the highest priority and 5 is the lowest. |
yii\swiftmailer\Message |
| $readReceiptTo | string | Receipt receive email addresses. | yii\swiftmailer\Message |
| $returnPath | string | The bounce email address. | yii\swiftmailer\Message |
| $signature | array|callable|\Swift_Signer | Signature specification. | yii\swiftmailer\Message |
| $swiftMessage | \Swift_Message | Swift message instance. | yii\swiftmailer\Message |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| createSwiftMessage() | Creates the Swift email message instance. | yii\swiftmailer\Message |
| createSwiftSigner() | Creates signer from it's configuration. | yii\swiftmailer\Message |
| setBody() | Sets the message body. | yii\swiftmailer\Message |
Property Details
Priority value as integer in range: 1..5, where 1 is the highest priority and 5
is the lowest.
Receipt receive email addresses. Note that the type of this property differs in getter and setter. See getReadReceiptTo() and setReadReceiptTo() for details.
Signature specification. See addSignature() for details on how it should be specified.
Method Details
This method is called after the object is created by cloning an existing one.
It ensures $swiftMessage and signers is also cloned.
| public mixed __clone ( ) |
public function __clone()
{
if ($this->_swiftMessage !== null) {
$this->_swiftMessage = clone $this->_swiftMessage;
}
foreach ($this->signers as $key => $signer) {
$this->signers[$key] = clone $signer;
}
}
Adds custom header value to the message.
Several invocations of this method with the same name will add multiple header values.
| public $this addHeader ( string $name, string $value ) | ||
| $name | string |
Header name. |
| $value | string |
Header value. |
| return | $this |
Self reference. |
|---|---|---|
public function addHeader($name, $value)
{
$this->getSwiftMessage()->getHeaders()->addTextHeader($name, $value);
return $this;
}
Adds message signature.
| public $this addSignature ( array|callable|\Swift_Signer $signature ) | ||
| $signature | array|callable|\Swift_Signer |
Signature specification, this can be:
|
| return | $this |
Self reference |
|---|---|---|
| throws | \yii\base\InvalidConfigException |
on invalid signature configuration |
public function addSignature($signature)
{
if ($signature instanceof \Swift_Signer) {
$signer = $signature;
} elseif (is_callable($signature)) {
$signer = call_user_func($signature);
} elseif (is_array($signature)) {
$signer = $this->createSwiftSigner($signature);
} else {
throw new InvalidConfigException('Signature should be instance of "Swift_Signer", callable or array configuration');
}
$this->getSwiftMessage()->attachSigner($signer);
$this->signers[] = $signer;
return $this;
}
| public attach ( mixed $fileName, array $options = [] ) | ||
| $fileName | mixed | |
| $options | array | |
public function attach($fileName, array $options = [])
{
$attachment = \Swift_Attachment::fromPath($fileName);
if (!empty($options['fileName'])) {
$attachment->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$attachment->setContentType($options['contentType']);
}
$this->getSwiftMessage()->attach($attachment);
return $this;
}
| public attachContent ( mixed $content, array $options = [] ) | ||
| $content | mixed | |
| $options | array | |
public function attachContent($content, array $options = [])
{
$attachment = new \Swift_Attachment($content);
if (!empty($options['fileName'])) {
$attachment->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$attachment->setContentType($options['contentType']);
}
if (!empty($options['setDisposition'])) {
$attachment->setDisposition($options['setDisposition']);
}
$this->getSwiftMessage()->attach($attachment);
return $this;
}
Creates the Swift email message instance.
| protected \Swift_Message createSwiftMessage ( ) | ||
| return | \Swift_Message |
Email message instance. |
|---|---|---|
protected function createSwiftMessage()
{
return new \Swift_Message();
}
Creates signer from it's configuration.
| protected \Swift_Signer createSwiftSigner ( array $signature ) | ||
| $signature | array |
Signature configuration:
|
| return | \Swift_Signer |
Signer instance |
|---|---|---|
| throws | \yii\base\InvalidConfigException |
on invalid configuration provided |
protected function createSwiftSigner($signature)
{
if (!isset($signature['type'])) {
throw new InvalidConfigException('Signature configuration should contain "type" key');
}
$signature['type'] = strtolower($signature['type']);
if (!in_array($signature['type'], ['dkim', 'opendkim'], true)) {
throw new InvalidConfigException("Unrecognized signature type '{$signature['type']}'");
}
if (isset($signature['key'])) {
$privateKey = $signature['key'];
} elseif (isset($signature['file'])) {
$privateKey = file_get_contents(Yii::getAlias($signature['file']));
} else {
throw new InvalidConfigException("Either 'key' or 'file' signature option should be specified");
}
$domain = ArrayHelper::getValue($signature, 'domain');
$selector = ArrayHelper::getValue($signature, 'selector');
if ($signature['type'] === 'opendkim') {
Yii::warning(__METHOD__ . '(): signature type "opendkim" is deprecated, use "dkim" instead.');
return new \Swift_Signers_OpenDKIMSigner($privateKey, $domain, $selector);
}
return new \Swift_Signers_DKIMSigner($privateKey, $domain, $selector);
}
| public embed ( mixed $fileName, array $options = [] ) | ||
| $fileName | mixed | |
| $options | array | |
public function embed($fileName, array $options = [])
{
$embedFile = \Swift_EmbeddedFile::fromPath($fileName);
if (!empty($options['fileName'])) {
$embedFile->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$embedFile->setContentType($options['contentType']);
}
return $this->getSwiftMessage()->embed($embedFile);
}
| public embedContent ( mixed $content, array $options = [] ) | ||
| $content | mixed | |
| $options | array | |
public function embedContent($content, array $options = [])
{
$embedFile = new \Swift_EmbeddedFile($content);
if (!empty($options['fileName'])) {
$embedFile->setFilename($options['fileName']);
}
if (!empty($options['contentType'])) {
$embedFile->setContentType($options['contentType']);
}
return $this->getSwiftMessage()->embed($embedFile);
}
| public getCharset ( ) |
public function getCharset()
{
return $this->getSwiftMessage()->getCharset();
}
Returns all values for the specified header.
| public array getHeader ( string $name ) | ||
| $name | string |
Header name. |
| return | array |
Header values list. |
|---|---|---|
public function getHeader($name)
{
$headerSet = $this->getSwiftMessage()->getHeaders();
if (!$headerSet->has($name)) {
return [];
}
$headers = [];
foreach ($headerSet->getAll($name) as $header) {
$headers[] = $header->getValue();
}
return $headers;
}
returns mailer instance.
| public yii\swiftmailer\Mailer getMailer ( ) | ||
| return | yii\swiftmailer\Mailer | |
|---|---|---|
Returns the priority of this message.
| public integer getPriority ( ) | ||
| return | integer |
Priority value as integer in range: |
|---|---|---|
public function getPriority()
{
return $this->getSwiftMessage()->getPriority();
}
Get the addresses to which a read-receipt will be sent.
| public string getReadReceiptTo ( ) | ||
| return | string |
Receipt receive email addresses. |
|---|---|---|
public function getReadReceiptTo()
{
return $this->getSwiftMessage()->getReadReceiptTo();
}
| public getReplyTo ( ) |
public function getReplyTo()
{
return $this->getSwiftMessage()->getReplyTo();
}
Returns the return-path (the bounce address) of this message.
| public string getReturnPath ( ) | ||
| return | string |
The bounce email address. |
|---|---|---|
public function getReturnPath()
{
return $this->getSwiftMessage()->getReturnPath();
}
| public getSubject ( ) |
public function getSubject()
{
return $this->getSwiftMessage()->getSubject();
}
| public \Swift_Message getSwiftMessage ( ) | ||
| return | \Swift_Message |
Swift message instance. |
|---|---|---|
public function getSwiftMessage()
{
if ($this->_swiftMessage === null) {
$this->_swiftMessage = $this->createSwiftMessage();
}
return $this->_swiftMessage;
}
| public setBcc ( mixed $bcc ) | ||
| $bcc | mixed | |
public function setBcc($bcc)
{
$this->getSwiftMessage()->setBcc($bcc);
return $this;
}
Sets the message body.
If body is already set and its content type matches given one, it will be overridden, if content type miss match the multipart message will be composed.
| protected mixed setBody ( string $body, string $contentType ) | ||
| $body | string |
Body content. |
| $contentType | string |
Body content type. |
protected function setBody($body, $contentType)
{
$message = $this->getSwiftMessage();
$oldBody = $message->getBody();
$charset = $message->getCharset();
if (empty($oldBody)) {
$parts = $message->getChildren();
$partFound = false;
foreach ($parts as $key => $part) {
if (!($part instanceof \Swift_Mime_Attachment)) {
/* @var $part \Swift_Mime_MimePart */
if ($part->getContentType() == $contentType) {
$charset = $part->getCharset();
unset($parts[$key]);
$partFound = true;
break;
}
}
}
if ($partFound) {
reset($parts);
$message->setChildren($parts);
$message->addPart($body, $contentType, $charset);
} else {
$message->setBody($body, $contentType);
}
} else {
$oldContentType = $message->getContentType();
if ($oldContentType == $contentType) {
$message->setBody($body, $contentType);
} else {
$message->setBody(null);
$message->setContentType(null);
$message->addPart($oldBody, $oldContentType, $charset);
$message->addPart($body, $contentType, $charset);
}
}
}
| public setCc ( mixed $cc ) | ||
| $cc | mixed | |
public function setCc($cc)
{
$this->getSwiftMessage()->setCc($cc);
return $this;
}
| public setCharset ( mixed $charset ) | ||
| $charset | mixed | |
public function setCharset($charset)
{
$this->getSwiftMessage()->setCharset($charset);
return $this;
}
| public setFrom ( mixed $from ) | ||
| $from | mixed | |
public function setFrom($from)
{
$this->getSwiftMessage()->setFrom($from);
return $this;
}
Sets custom header value to the message.
| public $this setHeader ( string $name, string|array $value ) | ||
| $name | string |
Header name. |
| $value | string|array |
Header value or values. |
| return | $this |
Self reference. |
|---|---|---|
public function setHeader($name, $value)
{
$headerSet = $this->getSwiftMessage()->getHeaders();
if ($headerSet->has($name)) {
$headerSet->remove($name);
}
foreach ((array)$value as $v) {
$headerSet->addTextHeader($name, $v);
}
return $this;
}
Sets custom header values to the message.
| public $this setHeaders ( array $headers ) | ||
| $headers | array |
Headers in format: |
| return | $this |
Self reference. |
|---|---|---|
public function setHeaders($headers)
{
foreach ($headers as $name => $value) {
$this->setHeader($name, $value);
}
return $this;
}
| public setHtmlBody ( mixed $html ) | ||
| $html | mixed | |
public function setHtmlBody($html)
{
$this->setBody($html, 'text/html');
return $this;
}
Set the priority of this message.
| public $this setPriority ( integer $priority ) | ||
| $priority | integer |
Priority value, should be an integer in range: |
| return | $this |
Self reference. |
|---|---|---|
public function setPriority($priority)
{
$this->getSwiftMessage()->setPriority($priority);
return $this;
}
Sets the ask for a delivery receipt from the recipient to be sent to $addresses.
| public $this setReadReceiptTo ( string|array $addresses ) | ||
| $addresses | string|array |
Receipt receive email address(es). |
| return | $this |
Self reference. |
|---|---|---|
public function setReadReceiptTo($addresses)
{
$this->getSwiftMessage()->setReadReceiptTo($addresses);
return $this;
}
| public setReplyTo ( mixed $replyTo ) | ||
| $replyTo | mixed | |
public function setReplyTo($replyTo)
{
$this->getSwiftMessage()->setReplyTo($replyTo);
return $this;
}
Set the return-path (the bounce address) of this message.
| public $this setReturnPath ( string $address ) | ||
| $address | string |
The bounce email address. |
| return | $this |
Self reference. |
|---|---|---|
public function setReturnPath($address)
{
$this->getSwiftMessage()->setReturnPath($address);
return $this;
}
Sets message signature
| public $this setSignature ( array|callable|\Swift_Signer $signature ) | ||
| $signature | array|callable|\Swift_Signer |
Signature specification. See addSignature() for details on how it should be specified. |
| return | $this |
Self reference. |
|---|---|---|
public function setSignature($signature)
{
if (!empty($this->signers)) {
// clear previously set signers
$swiftMessage = $this->getSwiftMessage();
foreach ($this->signers as $signer) {
$swiftMessage->detachSigner($signer);
}
$this->signers = [];
}
return $this->addSignature($signature);
}
| public setSubject ( mixed $subject ) | ||
| $subject | mixed | |
public function setSubject($subject)
{
$this->getSwiftMessage()->setSubject($subject);
return $this;
}
| public setTextBody ( mixed $text ) | ||
| $text | mixed | |
public function setTextBody($text)
{
$this->setBody($text, 'text/plain');
return $this;
}
| public setTo ( mixed $to ) | ||
| $to | mixed | |
public function setTo($to)
{
$this->getSwiftMessage()->setTo($to);
return $this;
}