Class yii\swiftmailer\Message

Inheritanceyii\swiftmailer\Message » yii\mail\BaseMessage
Available since extension's version2.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

Hide inherited 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

Hide inherited methods

Method Description Defined By
__clone() This method is called after the object is created by cloning an existing one. yii\swiftmailer\Message
addHeader() Adds custom header value to the message. yii\swiftmailer\Message
addSignature() Adds message signature. yii\swiftmailer\Message
attach() yii\swiftmailer\Message
attachContent() yii\swiftmailer\Message
embed() yii\swiftmailer\Message
embedContent() yii\swiftmailer\Message
getBcc() yii\swiftmailer\Message
getCc() yii\swiftmailer\Message
getCharset() yii\swiftmailer\Message
getFrom() yii\swiftmailer\Message
getHeader() Returns all values for the specified header. yii\swiftmailer\Message
getMailer() returns mailer instance. yii\swiftmailer\Message
getPriority() Returns the priority of this message. yii\swiftmailer\Message
getReadReceiptTo() Get the addresses to which a read-receipt will be sent. yii\swiftmailer\Message
getReplyTo() yii\swiftmailer\Message
getReturnPath() Returns the return-path (the bounce address) of this message. yii\swiftmailer\Message
getSubject() yii\swiftmailer\Message
getSwiftMessage() yii\swiftmailer\Message
getTo() yii\swiftmailer\Message
setBcc() yii\swiftmailer\Message
setCc() yii\swiftmailer\Message
setCharset() yii\swiftmailer\Message
setFrom() yii\swiftmailer\Message
setHeader() Sets custom header value to the message. yii\swiftmailer\Message
setHeaders() Sets custom header values to the message. yii\swiftmailer\Message
setHtmlBody() yii\swiftmailer\Message
setPriority() Set the priority of this message. yii\swiftmailer\Message
setReadReceiptTo() Sets the ask for a delivery receipt from the recipient to be sent to $addresses. yii\swiftmailer\Message
setReplyTo() yii\swiftmailer\Message
setReturnPath() Set the return-path (the bounce address) of this message. yii\swiftmailer\Message
setSignature() Sets message signature yii\swiftmailer\Message
setSubject() yii\swiftmailer\Message
setTextBody() yii\swiftmailer\Message
setTo() yii\swiftmailer\Message
toString() yii\swiftmailer\Message

Protected Methods

Hide inherited 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

Hide inherited properties

$headers public property

Headers in format: [name => value].

public array $headers null
$priority public property

Priority value as integer in range: 1..5, where 1 is the highest priority and 5 is the lowest.

public integer $priority null
$readReceiptTo public property

Receipt receive email addresses. Note that the type of this property differs in getter and setter. See getReadReceiptTo() and setReadReceiptTo() for details.

public string $readReceiptTo null
$returnPath public property

The bounce email address.

public string $returnPath null
$signature public property

Signature specification. See addSignature() for details on how it should be specified.

public array|callable|\Swift_Signer $signature null
$swiftMessage public property

Swift message instance.

public \Swift_Message $swiftMessage null

Method Details

Hide inherited methods

__clone() public method (available since version 2.0.7)

This method is called after the object is created by cloning an existing one.

It ensures $swiftMessage and signers is also cloned.

public void __clone ( )

                public function __clone()
{
    if ($this->_swiftMessage !== null) {
        $this->_swiftMessage = clone $this->_swiftMessage;
    }
    foreach ($this->signers as $key => $signer) {
        $this->signers[$key] = clone $signer;
    }
}

            
addHeader() public method (available since version 2.0.6)

Adds custom header value to the message.

Several invocations of this method with the same name will add multiple header values.

public $this addHeader ( $name, $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;
}

            
addSignature() public method (available since version 2.0.6)

Adds message signature.

public $this addSignature ( $signature )
$signature array|callable|\Swift_Signer

Signature specification, this can be:

  • \Swift_Signer instance
  • callable, which returns \Swift_Signer instance
  • configuration array for the signer creation
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;
}

            
attach() public method

public void attach ( $fileName, array $options = [] )
$fileName
$options

                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;
}

            
attachContent() public method

public void attachContent ( $content, array $options = [] )
$content
$options

                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;
}

            
createSwiftMessage() protected method

Creates the Swift email message instance.

protected \Swift_Message createSwiftMessage ( )
return \Swift_Message

Email message instance.

                protected function createSwiftMessage()
{
    return new \Swift_Message();
}

            
createSwiftSigner() protected method (available since version 2.0.6)

Creates signer from it's configuration.

protected \Swift_Signer createSwiftSigner ( $signature )
$signature array

Signature configuration: [type: string, key: string|null, file: string|null, domain: string|null, selector: string|null]

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);
}

            
embed() public method

public void embed ( $fileName, array $options = [] )
$fileName
$options

                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);
}

            
embedContent() public method

public void embedContent ( $content, array $options = [] )
$content
$options

                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);
}

            
getBcc() public method

public void getBcc ( )

                public function getBcc()
{
    return $this->getSwiftMessage()->getBcc();
}

            
getCc() public method

public void getCc ( )

                public function getCc()
{
    return $this->getSwiftMessage()->getCc();
}

            
getCharset() public method

public void getCharset ( )

                public function getCharset()
{
    return $this->getSwiftMessage()->getCharset();
}

            
getFrom() public method

public void getFrom ( )

                public function getFrom()
{
    return $this->getSwiftMessage()->getFrom();
}

            
getHeader() public method (available since version 2.0.6)

Returns all values for the specified header.

public array getHeader ( $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;
}

            
getMailer() public method

returns mailer instance.

public yii\swiftmailer\Mailer getMailer ( )
return yii\swiftmailer\Mailer
getPriority() public method (available since version 2.0.6)

Returns the priority of this message.

public integer getPriority ( )
return integer

Priority value as integer in range: 1..5, where 1 is the highest priority and 5 is the lowest.

                public function getPriority()
{
    return $this->getSwiftMessage()->getPriority();
}

            
getReadReceiptTo() public method (available since version 2.0.6)

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();
}

            
getReplyTo() public method

public void getReplyTo ( )

                public function getReplyTo()
{
    return $this->getSwiftMessage()->getReplyTo();
}

            
getReturnPath() public method (available since version 2.0.6)

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();
}

            
getSubject() public method

public void getSubject ( )

                public function getSubject()
{
    return $this->getSwiftMessage()->getSubject();
}

            
getSwiftMessage() public method

public \Swift_Message getSwiftMessage ( )
return \Swift_Message

Swift message instance.

                public function getSwiftMessage()
{
    if ($this->_swiftMessage === null) {
        $this->_swiftMessage = $this->createSwiftMessage();
    }
    return $this->_swiftMessage;
}

            
getTo() public method

public void getTo ( )

                public function getTo()
{
    return $this->getSwiftMessage()->getTo();
}

            
setBcc() public method

public void setBcc ( $bcc )
$bcc

                public function setBcc($bcc)
{
    $this->getSwiftMessage()->setBcc($bcc);
    return $this;
}

            
setBody() protected method

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 void setBody ( $body, $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);
        }
    }
}

            
setCc() public method

public void setCc ( $cc )
$cc

                public function setCc($cc)
{
    $this->getSwiftMessage()->setCc($cc);
    return $this;
}

            
setCharset() public method

public void setCharset ( $charset )
$charset

                public function setCharset($charset)
{
    $this->getSwiftMessage()->setCharset($charset);
    return $this;
}

            
setFrom() public method

public void setFrom ( $from )
$from

                public function setFrom($from)
{
    $this->getSwiftMessage()->setFrom($from);
    return $this;
}

            
setHeader() public method (available since version 2.0.6)

Sets custom header value to the message.

public $this setHeader ( $name, $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;
}

            
setHeaders() public method (available since version 2.0.7)

Sets custom header values to the message.

public $this setHeaders ( $headers )
$headers array

Headers in format: [name => value].

return $this

Self reference.

                public function setHeaders($headers)
{
    foreach ($headers as $name => $value) {
        $this->setHeader($name, $value);
    }
    return $this;
}

            
setHtmlBody() public method

public void setHtmlBody ( $html )
$html

                public function setHtmlBody($html)
{
    $this->setBody($html, 'text/html');
    return $this;
}

            
setPriority() public method (available since version 2.0.6)

Set the priority of this message.

public $this setPriority ( $priority )
$priority integer

Priority value, should be an integer in range: 1..5, where 1 is the highest priority and 5 is the lowest.

return $this

Self reference.

                public function setPriority($priority)
{
    $this->getSwiftMessage()->setPriority($priority);
    return $this;
}

            
setReadReceiptTo() public method (available since version 2.0.6)

Sets the ask for a delivery receipt from the recipient to be sent to $addresses.

public $this setReadReceiptTo ( $addresses )
$addresses string|array

Receipt receive email address(es).

return $this

Self reference.

                public function setReadReceiptTo($addresses)
{
    $this->getSwiftMessage()->setReadReceiptTo($addresses);
    return $this;
}

            
setReplyTo() public method

public void setReplyTo ( $replyTo )
$replyTo

                public function setReplyTo($replyTo)
{
    $this->getSwiftMessage()->setReplyTo($replyTo);
    return $this;
}

            
setReturnPath() public method (available since version 2.0.6)

Set the return-path (the bounce address) of this message.

public $this setReturnPath ( $address )
$address string

The bounce email address.

return $this

Self reference.

                public function setReturnPath($address)
{
    $this->getSwiftMessage()->setReturnPath($address);
    return $this;
}

            
setSignature() public method (available since version 2.0.6)

Sets message signature

public $this setSignature ( $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);
}

            
setSubject() public method

public void setSubject ( $subject )
$subject

                public function setSubject($subject)
{
    $this->getSwiftMessage()->setSubject($subject);
    return $this;
}

            
setTextBody() public method

public void setTextBody ( $text )
$text

                public function setTextBody($text)
{
    $this->setBody($text, 'text/plain');
    return $this;
}

            
setTo() public method

public void setTo ( $to )
$to

                public function setTo($to)
{
    $this->getSwiftMessage()->setTo($to);
    return $this;
}

            
toString() public method

public void toString ( )

                public function toString()
{
    return $this->getSwiftMessage()->toString();
}