mini\Mail\EmailInterface abstract interface

Documentation

Email Composition Interface

Provides a declarative API for composing RFC 5322 email messages with MIME support. The implementation uses lazy compilation - you describe what you want (text, HTML, attachments, inline images) and the correct MIME structure is built automatically.

Basic Usage

$email = (new Email())
    ->withFrom('sender@example.com')
    ->withTo('recipient@example.com')
    ->withSubject('Hello!')
    ->withTextBody('Plain text version')
    ->withHtmlBody('<h1>HTML version</h1>');

// Cast to string for complete RFC 5322 message (headers + body)
$raw = (string) $email;

// Or stream it (recommended for large emails with attachments)
while (!$email->eof()) {
    fwrite($pipe, $email->read(8192));
}

// Pipe to sendmail
$pipe = popen('/usr/sbin/sendmail -t', 'w');
fwrite($pipe, (string) $email);
pclose($pipe);

Mailbox Addresses

Address methods accept both strings and MailboxInterface:

// Simple string
$email->withFrom('sender@example.com');

// String with display name
$email->withFrom('Frode Børli <frode@ennerd.com>');

// MailboxInterface
$email->withFrom(new Mailbox('frode@ennerd.com', 'Frode Børli'));

// Parsed from string
$email->withFrom(Mailbox::fromString('Frode Børli <frode@ennerd.com>'));

// Multiple recipients
$email->withTo('alice@example.com', 'bob@example.com', $carolMailbox);

// Add recipients incrementally
$email->withTo('alice@example.com')
      ->withAddedTo('bob@example.com');

HTML with Inline Images

Inline images are referenced in HTML using the cid: URL scheme. The array keys become Content-IDs:

$email = (new Email())
    ->withFrom('newsletter@example.com')
    ->withTo('subscriber@example.com')
    ->withSubject('Weekly Update')
    ->withTextBody('View this email in a browser for images.')
    ->withHtmlBody(
        '<html>
          <body>
            <img src="cid:header" alt="Header">
            <p>Hello!</p>
            <img src="cid:logo" alt="Logo">
          </body>
        </html>',
        [
            'header' => '/path/to/header.png',           // String = file path
            'logo' => Message::fromFile('logo.png'),    // Or MessageInterface
        ]
    );

Attachments

Add file attachments with automatic MIME type detection:

$email = (new Email())
    ->withFrom('reports@example.com')
    ->withTo('manager@example.com')
    ->withSubject('Monthly Report')
    ->withTextBody('Please find the report attached.')
    ->withAttachments([
        '/path/to/report.pdf',                    // Filename from path
        '/path/to/data.csv',
        Message::fromFile('/tmp/generated.xlsx'), // Filename from Message
    ]);

To override the filename shown to recipients, use string keys:

$email->withAttachments([
    'Monthly Report.pdf' => '/tmp/report-2024-01.pdf',
    'Raw Data.csv' => $csvMessageInterface,
]);

Complete Example

An email with text alternative, HTML with inline images, and attachments:

$email = (new Email())
    ->withFrom('Name <sender@example.com>')
    ->withTo('recipient@example.com')
    ->withCc('cc@example.com')
    ->withReplyTo('replies@example.com')
    ->withSubject('Project Update')
    ->withTextBody('Please view in HTML for the full experience.')
    ->withHtmlBody(
        file_get_contents('email-template.html'),
        [
            'logo' => '/assets/logo.png',
            'chart' => Message::fromFile('/tmp/chart.png'),
        ]
    )
    ->withAttachments([
        'Project Plan.pdf' => '/documents/plan.pdf',
        'meeting.ics' => $calendarInvite,
    ]);

MIME Structure

The implementation automatically builds the correct nested MIME structure:

multipart/mixed
├── multipart/alternative
│   ├── text/plain
│   └── multipart/related
│       ├── text/html
│       ├── image/png (Content-ID: <logo>)
│       └── image/png (Content-ID: <chart>)
├── application/pdf (attachment: Project Plan.pdf)
└── text/calendar (attachment: meeting.ics)

Templating Pattern

Create reusable templates with immutable composition:

$template = (new Email())
    ->withFrom('noreply@example.com')
    ->withSubject('Welcome!')
    ->withHtmlBody('<h1>Welcome, {name}!</h1>', ['logo' => '/assets/logo.png']);

foreach ($users as $user) {
    $email = $template
        ->withTo($user->email)
        ->withHtmlBody(
            str_replace('{name}', $user->name, $template->getHtmlBody()),
            $template->getInlines()
        );

    $transport->send($email);
}

Inheritance

Implements: Psr\Http\Message\MessageInterface Psr\Http\Message\StreamInterface Stringable

Methods (51)

Get the From addresses

Return instance with the specified From address(es)

Get the To addresses

Return instance with the specified To address(es)

Return instance with additional To address(es)

Get the Cc addresses

Return instance with the specified Cc address(es)

Return instance with additional Cc address(es)

Get the Bcc addresses

Return instance with the specified Bcc address(es)

Return instance with additional Bcc address(es)

Get the Reply-To addresses

Return instance with the specified Reply-To address(es)

Return instance with additional Reply-To address(es)

Get the subject line

Return instance with the specified subject

Get the Date header

Return instance with the specified Date

Get the plain text body

Return instance with the specified plain text body

Return instance with the specified HTML body and optional inline images

Get the inline images

Return instance with the specified attachments

Get the compiled message body as a stream

public abstract getProtocolVersion()
inherited from Psr\Http\Message\MessageInterface

Retrieves the HTTP protocol version as a string.

public abstract withProtocolVersion()
inherited from Psr\Http\Message\MessageInterface

Return an instance with the specified HTTP protocol version.

public abstract getHeaders()
inherited from Psr\Http\Message\MessageInterface

Retrieves all message header values.

public abstract hasHeader()
inherited from Psr\Http\Message\MessageInterface

Checks if a header exists by the given case-insensitive name.

public abstract getHeader()
inherited from Psr\Http\Message\MessageInterface

Retrieves a message header value by the given case-insensitive name.

public abstract getHeaderLine()
inherited from Psr\Http\Message\MessageInterface

Retrieves a comma-separated string of the values for a single header.

public abstract withHeader()
inherited from Psr\Http\Message\MessageInterface

Return an instance with the provided value replacing the specified header.

public abstract withAddedHeader()
inherited from Psr\Http\Message\MessageInterface

Return an instance with the specified header appended with the given value.

public abstract withoutHeader()
inherited from Psr\Http\Message\MessageInterface

Return an instance without the specified header.

public abstract withBody()
inherited from Psr\Http\Message\MessageInterface

Return an instance with the specified message body.

public abstract __toString()
inherited from Psr\Http\Message\StreamInterface

Reads all data from the stream into a string, from the beginning to end.

public abstract close()
inherited from Psr\Http\Message\StreamInterface

Closes the stream and any underlying resources.

public abstract detach()
inherited from Psr\Http\Message\StreamInterface

Separates any underlying resources from the stream.

public abstract getSize()
inherited from Psr\Http\Message\StreamInterface

Get the size of the stream if known.

public abstract tell()
inherited from Psr\Http\Message\StreamInterface

Returns the current position of the file read/write pointer

public abstract eof()
inherited from Psr\Http\Message\StreamInterface

Returns true if the stream is at the end of the stream.

public abstract isSeekable()
inherited from Psr\Http\Message\StreamInterface

Returns whether or not the stream is seekable.

public abstract seek()
inherited from Psr\Http\Message\StreamInterface

Seek to a position in the stream.

public abstract rewind()
inherited from Psr\Http\Message\StreamInterface

Seek to the beginning of the stream.

public abstract isWritable()
inherited from Psr\Http\Message\StreamInterface

Returns whether or not the stream is writable.

public abstract write()
inherited from Psr\Http\Message\StreamInterface

Write data to the stream.

public abstract isReadable()
inherited from Psr\Http\Message\StreamInterface

Returns whether or not the stream is readable.

public abstract read()
inherited from Psr\Http\Message\StreamInterface

Read data from the stream.

public abstract getContents()
inherited from Psr\Http\Message\StreamInterface

Returns the remaining contents in a string

public abstract getMetadata()
inherited from Psr\Http\Message\StreamInterface

Get stream metadata as an associative array or retrieve a specific key.

Source

src/Mail/EmailInterface.php:183-468