Pular para o conteúdo principal

Envelope

The Envelope class represents an email message. It's totally decoupled from the Mailer engine, so you can also use it as a DTO (Data Transfer Object).

Creating an Envelope

Basic Constructor

$envelope = new \ByJG\Mail\Envelope();

Constructor with Parameters

$envelope = new \ByJG\Mail\Envelope(
'[email protected]', // From address
'[email protected]', // To address
'Email Subject', // Subject
'<p>HTML Body</p>', // Body
true // isHtml (default: true)
);

Setting Email Properties

From Address

// Simple from address
$envelope->setFrom('[email protected]');

// From address with name
$envelope->setFrom('[email protected]', 'John Doe');

Recipients

// Set a single recipient (replaces existing)
$envelope->setTo('[email protected]', 'Jane Doe');

// Add multiple recipients
$envelope->addTo('[email protected]');
$envelope->addTo('[email protected]', 'User Two');

CC and BCC

// Carbon Copy
$envelope->addCC('[email protected]', 'Manager');
$envelope->setCC('[email protected]'); // Replaces all CC

// Blind Carbon Copy
$envelope->addBCC('[email protected]');
$envelope->setBCC('[email protected]'); // Replaces all BCC

Subject and Body

// Set subject
$envelope->setSubject('Important Notice');

// Set HTML body
$envelope->setBody('<h1>Hello</h1><p>This is an HTML email</p>');
$envelope->isHtml(true);

// Set plain text body
$envelope->setBody('This is plain text');
$envelope->isHtml(false);

Reply-To

$envelope->setReplyTo('[email protected]');

// If not set, defaults to the From address
$replyTo = $envelope->getReplyTo();

Getting Properties

All properties have corresponding getter methods:

$from = $envelope->getFrom();
$to = $envelope->getTo(); // Returns array
$subject = $envelope->getSubject();
$body = $envelope->getBody();
$cc = $envelope->getCC(); // Returns array
$bcc = $envelope->getBCC(); // Returns array
$isHtml = $envelope->isHtml();

Text Body Generation

The Envelope can automatically generate a plain text version of an HTML body:

$envelope->setBody('<h1>Title</h1><p>Paragraph</p>');
$textBody = $envelope->getBodyText();
// Returns: "# Title\n\nParagraph\n"

This is useful for multipart emails that include both HTML and plain text versions.