aimanecouissi / module-mail-transport-builder-attachment
Add email attachment support to the Magento mail transport builder
Package info
github.com/aimanecouissi/magento2-module-mail-transport-builder-attachment
Type:magento2-module
pkg:composer/aimanecouissi/module-mail-transport-builder-attachment
Requires
- php: ^8.3
- magento/framework: 103.0.*
README
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.