Message.php
TLDR
This file, src/Illuminate/Mail/Message.php
, is part of the Laravel framework's Mail package. It contains the Message
class, which is responsible for creating and manipulating email messages. The class extends the Email
class from Symfony's Mime component and uses the ForwardsCalls
trait.
Class Message
The Message
class is the main class in this file. It extends Symfony's Email
class and provides additional functionality for creating and manipulating email messages. It has the following methods:
__construct(Email $message)
This method is the constructor of the Message
class. It accepts an instance of the Email
class as its parameter and initializes the $message
property with it.
from($address, $name = null)
This method adds a "from" address to the email message. It accepts the email address as the first parameter and optionally accepts the sender's name as the second parameter. It supports both single addresses and arrays of addresses.
sender($address, $name = null)
This method sets the "sender" of the email message. It accepts the email address as the first parameter and optionally accepts the sender's name as the second parameter. It supports both single senders and arrays of senders.
returnPath($address)
This method sets the "return path" of the email message. It accepts the email address as the parameter.
to($address, $name = null, $override = false)
This method adds a recipient to the email message. It accepts the email address as the first parameter and optionally accepts the recipient's name as the second parameter. It also accepts an additional override
parameter, which indicates whether the recipient should override any existing recipients.
forgetTo()
This method removes all "to" addresses from the email message.
cc($address, $name = null, $override = false)
This method adds a carbon copy recipient to the email message. It accepts the email address as the first parameter and optionally accepts the recipient's name as the second parameter. It also accepts an additional override
parameter, which indicates whether the recipient should override any existing carbon copy recipients.
forgetCc()
This method removes all carbon copy addresses from the email message.
bcc($address, $name = null, $override = false)
This method adds a blind carbon copy recipient to the email message. It accepts the email address as the first parameter and optionally accepts the recipient's name as the second parameter. It also accepts an additional override
parameter, which indicates whether the recipient should override any existing blind carbon copy recipients.
forgetBcc()
This method removes all blind carbon copy addresses from the email message.
replyTo($address, $name = null)
This method adds a "reply to" address to the email message. It accepts the email address as the first parameter and optionally accepts the sender's name as the second parameter.
subject($subject)
This method sets the subject of the email message. It accepts the subject as the parameter.
priority($level)
This method sets the priority level of the email message. It accepts the priority level as the parameter.
attach($file, array $options = [])
This method attaches a file to the email message. It accepts the file path, a file object implementing the Attachable
interface, or an instance of the Attachment
class as the first parameter. It also accepts an optional array of options for attaching the file.
attachData($data, $name, array $options = [])
This method attaches in-memory data as an attachment to the email message. It accepts the data, the name of the attachment, and an optional array of options for attaching the data.
embed($file)
This method embeds a file in the email message and returns the content identifier (CID) of the embedded file. It accepts the file path, a file object implementing the Attachable
interface, or an instance of the Attachment
class as the parameter.
embedData($data, $name, $contentType = null)
This method embeds in-memory data in the email message and returns the content identifier (CID) of the embedded data. It accepts the data, the name of the embedded data, and an optional content type.
getSymfonyMessage()
This method returns the underlying Symfony Email
instance.
__call($method, $parameters)
This method dynamically passes missing methods to the underlying Symfony instance using the ForwardsCalls
trait.
<?php
namespace Illuminate\Mail;
use Illuminate\Contracts\Mail\Attachable;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
/**
* @mixin \Symfony\Component\Mime\Email
*/
class Message
{
use ForwardsCalls;
/**
* The Symfony Email instance.
*
* @var \Symfony\Component\Mime\Email
*/
protected $message;
/**
* CIDs of files embedded in the message.
*
* @deprecated Will be removed in a future Laravel version.
*
* @var array
*/
protected $embeddedFiles = [];
/**
* Create a new message instance.
*
* @param \Symfony\Component\Mime\Email $message
* @return void
*/
public function __construct(Email $message)
{
$this->message = $message;
}
/**
* Add a "from" address to the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function from($address, $name = null)
{
is_array($address)
? $this->message->from(...$address)
: $this->message->from(new Address($address, (string) $name));
return $this;
}
/**
* Set the "sender" of the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function sender($address, $name = null)
{
is_array($address)
? $this->message->sender(...$address)
: $this->message->sender(new Address($address, (string) $name));
return $this;
}
/**
* Set the "return path" of the message.
*
* @param string $address
* @return $this
*/
public function returnPath($address)
{
$this->message->returnPath($address);
return $this;
}
/**
* Add a recipient to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function to($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->to(...$address)
: $this->message->to(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'To');
}
/**
* Remove all "to" addresses from the message.
*
* @return $this
*/
public function forgetTo()
{
if ($header = $this->message->getHeaders()->get('To')) {
$this->addAddressDebugHeader('X-To', $this->message->getTo());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a carbon copy to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function cc($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->cc(...$address)
: $this->message->cc(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'Cc');
}
/**
* Remove all carbon copy addresses from the message.
*
* @return $this
*/
public function forgetCc()
{
if ($header = $this->message->getHeaders()->get('Cc')) {
$this->addAddressDebugHeader('X-Cc', $this->message->getCC());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a blind carbon copy to the message.
*
* @param string|array $address
* @param string|null $name
* @param bool $override
* @return $this
*/
public function bcc($address, $name = null, $override = false)
{
if ($override) {
is_array($address)
? $this->message->bcc(...$address)
: $this->message->bcc(new Address($address, (string) $name));
return $this;
}
return $this->addAddresses($address, $name, 'Bcc');
}
/**
* Remove all of the blind carbon copy addresses from the message.
*
* @return $this
*/
public function forgetBcc()
{
if ($header = $this->message->getHeaders()->get('Bcc')) {
$this->addAddressDebugHeader('X-Bcc', $this->message->getBcc());
$header->setAddresses([]);
}
return $this;
}
/**
* Add a "reply to" address to the message.
*
* @param string|array $address
* @param string|null $name
* @return $this
*/
public function replyTo($address, $name = null)
{
return $this->addAddresses($address, $name, 'ReplyTo');
}
/**
* Add a recipient to the message.
*
* @param string|array $address
* @param string $name
* @param string $type
* @return $this
*/
protected function addAddresses($address, $name, $type)
{
if (is_array($address)) {
$type = lcfirst($type);
$addresses = collect($address)->map(function ($address, $key) {
if (is_string($key) && is_string($address)) {
return new Address($key, $address);
}
if (is_array($address)) {
return new Address($address['email'] ?? $address['address'], $address['name'] ?? null);
}
if (is_null($address)) {
return new Address($key);
}
return $address;
})->all();
$this->message->{"{$type}"}(...$addresses);
} else {
$this->message->{"add{$type}"}(new Address($address, (string) $name));
}
return $this;
}
/**
* Add an address debug header for a list of recipients.
*
* @param string $header
* @param \Symfony\Component\Mime\Address[] $addresses
* @return $this
*/
protected function addAddressDebugHeader(string $header, array $addresses)
{
$this->message->getHeaders()->addTextHeader(
$header,
implode(', ', array_map(fn ($a) => $a->toString(), $addresses)),
);
return $this;
}
/**
* Set the subject of the message.
*
* @param string $subject
* @return $this
*/
public function subject($subject)
{
$this->message->subject($subject);
return $this;
}
/**
* Set the message priority level.
*
* @param int $level
* @return $this
*/
public function priority($level)
{
$this->message->priority($level);
return $this;
}
/**
* Attach a file to the message.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @param array $options
* @return $this
*/
public function attach($file, array $options = [])
{
if ($file instanceof Attachable) {
$file = $file->toMailAttachment();
}
if ($file instanceof Attachment) {
return $file->attachTo($this);
}
$this->message->attachFromPath($file, $options['as'] ?? null, $options['mime'] ?? null);
return $this;
}
/**
* Attach in-memory data as an attachment.
*
* @param string|resource $data
* @param string $name
* @param array $options
* @return $this
*/
public function attachData($data, $name, array $options = [])
{
$this->message->attach($data, $name, $options['mime'] ?? null);
return $this;
}
/**
* Embed a file in the message and get the CID.
*
* @param string|\Illuminate\Contracts\Mail\Attachable|\Illuminate\Mail\Attachment $file
* @return string
*/
public function embed($file)
{
if ($file instanceof Attachable) {
$file = $file->toMailAttachment();
}
if ($file instanceof Attachment) {
return $file->attachWith(
function ($path) use ($file) {
$cid = $file->as ?? Str::random();
$this->message->addPart(
(new DataPart(new File($path), $cid, $file->mime))->asInline()
);
return "cid:{$cid}";
},
function ($data) use ($file) {
$this->message->addPart(
(new DataPart($data(), $file->as, $file->mime))->asInline()
);
return "cid:{$file->as}";
}
);
}
$cid = Str::random(10);
$this->message->addPart(
(new DataPart(new File($file), $cid))->asInline()
);
return "cid:$cid";
}
/**
* Embed in-memory data in the message and get the CID.
*
* @param string|resource $data
* @param string $name
* @param string|null $contentType
* @return string
*/
public function embedData($data, $name, $contentType = null)
{
$this->message->addPart(
(new DataPart($data, $name, $contentType))->asInline()
);
return "cid:$name";
}
/**
* Get the underlying Symfony Email instance.
*
* @return \Symfony\Component\Mime\Email
*/
public function getSymfonyMessage()
{
return $this->message;
}
/**
* Dynamically pass missing methods to the Symfony instance.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->forwardDecoratedCallTo($this->message, $method, $parameters);
}
}