aimanecouissi/module-mail-transport-builder-attachment

Add email attachment support to the Magento mail transport builder

Maintainers

Package info

github.com/aimanecouissi/magento2-module-mail-transport-builder-attachment

Type:magento2-module

pkg:composer/aimanecouissi/module-mail-transport-builder-attachment

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-04-12 20:13 UTC

This package is auto-updated.

Last update: 2026-04-12 20:13:21 UTC


README

Latest Stable Version Total Downloads Magento Version Require License PHP Version Require

Extends the Magento mail transport builder with email attachment support. Provides AttachmentTransportBuilderInterface, which extends the core TransportBuilder and adds attachment methods on top of it.

Installation

composer require aimanecouissi/module-mail-transport-builder-attachment
bin/magento module:enable AimaneCouissi_MailTransportBuilderAttachment
bin/magento setup:upgrade
bin/magento cache:flush

Usage

Inject AttachmentTransportBuilderInterface via constructor dependency injection. Type-hint the constructor parameter with the concrete class AttachmentTransportBuilder in the DocBlock so your IDE can resolve both the core builder methods and the attachment API.

use AimaneCouissi\MailTransportBuilderAttachment\Api\AttachmentTransportBuilderInterface;
use AimaneCouissi\MailTransportBuilderAttachment\Model\AttachmentTransportBuilder;

/**
 * @param AttachmentTransportBuilder $attachmentTransportBuilder
 */
public function __construct(private readonly AttachmentTransportBuilderInterface $attachmentTransportBuilder)
{
}

To attach a file from raw content, call addAttachment() before getTransport():

$this->attachmentTransportBuilder
    ->setTemplateIdentifier('sales_email_order_template')
    ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
    ->setTemplateVars(['order' => $order, 'store' => $store])
    ->setFromByScope('sales')
    ->addTo($customer->getEmail(), $customer->getName())
    ->addAttachment($pdfContent, 'order-confirmation.pdf', 'application/pdf')
    ->getTransport()
    ->sendMessage();

To attach a file from disk, use addAttachmentFromFile() instead. The filename and MIME type are detected automatically if omitted:

$this->attachmentTransportBuilder
    ->setTemplateIdentifier('sales_email_invoice_template')
    ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => $storeId])
    ->setTemplateVars(['invoice' => $invoice, 'store' => $store])
    ->setFromByScope('sales')
    ->addTo($customer->getEmail(), $customer->getName())
    ->addAttachmentFromFile('/var/www/html/var/export/invoice.pdf')
    ->getTransport()
    ->sendMessage();

Both methods can be chained to attach multiple files to the same email:

$this->attachmentTransportBuilder
    ->addAttachment($invoiceContent, 'invoice.pdf', 'application/pdf')
    ->addAttachmentFromFile('/var/www/html/var/export/shipping-label.pdf')
    ->addAttachment($xmlContent, 'order-data.xml', 'application/xml');

API

The following methods are available on AttachmentTransportBuilderInterface in addition to all methods inherited from the core TransportBuilder.

addAttachment()

public function addAttachment(string $content, string $filename, string $mimeType = 'application/pdf', ?string $encoding = null): static

Adds an attachment from raw content. Defaults to application/pdf MIME type and base64 transfer encoding.

addAttachmentFromFile()

public function addAttachmentFromFile(string $filePath, ?string $filename = null, ?string $mimeType = null): static

Adds an attachment from an absolute file path. Filename and MIME type are detected automatically if omitted. Throws FileNotReadableException if the file does not exist or is not readable.

clearAttachments()

public function clearAttachments(): static

Removes all queued attachments.

getAttachments()

public function getAttachments(): array

Returns all queued attachments as an array of AttachmentInterface.

Uninstall

bin/magento module:disable AimaneCouissi_MailTransportBuilderAttachment
composer remove aimanecouissi/module-mail-transport-builder-attachment
bin/magento setup:upgrade
bin/magento cache:flush

Changelog

See CHANGELOG for all recent changes.

License

MIT