infrangible / m2-mail-attachment
Technical helper that allows Infrangible modules to add file attachments to the emails sent by the store.
Package info
github.com/infrangible-dev/m2-mail-attachment
Type:magento2-module
pkg:composer/infrangible/m2-mail-attachment
Requires
- php: >=8.1
- infrangible/m2-core: ~5.0.7
- infrangible/m2-foundation: ~3.0.2
- magento/framework: ~103.0.6
Requires (Dev)
- infrangible/phpstan-magento: ~1.2.0
Suggests
- symfony/mime: Installed with Magento 2.4.8 and newer, which build the messages with Symfony Mime; older versions use Laminas Mime through Magento's classes
Provides
None
Conflicts
None
Replaces
None
README
Adds attachments to the template emails of Magento, for modules and customizations that send files with an email, for example a PDF document.
Usage
The module replaces Magento's Magento\Framework\Mail\Template\TransportBuilder with a class that has the method
addAttachment(). Add the attachments before getTransport(), like the other data of the message:
$transport = $this->transportBuilder ->setTemplateIdentifier('sales_email_order_template') ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId]) ->setTemplateVars($templateVars) ->setFromByScope('sales', $storeId) ->addTo($email, $name) ->addAttachment($pdfContent, 'application/pdf', 'attachment', 'base64', 'invoice.pdf') ->getTransport(); $transport->sendMessage();
| Parameter | Default | Use |
|---|---|---|
$content |
the content of the file | |
$fileType |
application/octet-stream |
the content type |
$disposition |
attachment |
attachment, or inline to show it in the email |
$encoding |
base64 |
the transfer encoding up to Magento 2.4.7; newer versions use base64 |
$fileName |
none | the file name |
The attachments belong to the next message only: getTransport() removes them with the other data of the message.
Magento 2.4.8 and newer build the messages with Symfony Mime, older versions with Laminas Mime; the module adds the attachments in both cases. Without attachments, the messages are the ones of Magento. There are no settings.
Requirements
- Magento Open Source or Adobe Commerce 2.4.6 or newer
- PHP 8.1 or newer
Installation
composer require infrangible/m2-mail-attachment bin/magento module:enable Infrangible_MailAttachment bin/magento setup:upgrade
In production mode, also run bin/magento setup:di:compile.
License
Infrangible Mail Attachment is licensed under the MIT License - see the LICENSE file for details.