bredabeds/magento-email-attachments
Composer 安装命令:
composer require bredabeds/magento-email-attachments
包简介
Magento 2 module for adding attachments to emails by BredaBeds
README 文档
README
This module adds the ability to programatically send attachments with Magento 2 emails.
Template Variables
Method 1 (template variables):
->setTemplateVars([ 'attachments' => '/path/to/document.pdf' ])
Method 2 (template variables):
// 'type' and 'filename' are optional // If 'type' is not set, mime_content_type is used to guess // If 'filename' is not set, the filename from the path is used ->setTemplateVars([ 'attachments' => [ 'path' => '/path/to/document.xlsx', 'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'filename' => 'Document.xlsx' ] ])
Method 3 (template variables):
->setTemplateVars([ 'attachments' => [ '/path/to/document1.pdf', [ 'path' => '/path/to/document2.xlsx', 'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // optional 'filename' => 'document2.xlsx' // optional ], '/path/to/document3.docx' ] ])
Method 4 (addAttachment function):
$absolutePath = '/path/to/document1.pdf'; $transport = $this->transportBuilder ->setTemplateIdentifier('simple_text_email') ->setTemplateOptions([ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId() ]) ->setTemplateVars([ 'email_subject' => $subject, 'message' => $body, 'attachments' => $absolutePath // Can be added here via template variables ]) ->setFromByScope($this->contactsConfig->emailSender()) ->addTo($to) ->addAttachment(file_get_contents($absolutePath), basename($absolutePath), 'application/pdf'); // Or here $transport->getTransport()->sendMessage();
Browserless PDF generation and basic email template
This module also has the ability to generate a PDF via Browserless from a URL and it has a very basic template called "simple_text_email" that allows the body and subject to be set via template variables (see above example). This module has a wrapper helper for that template:
public function __construct( protected \BredaBeds\EmailAttachments\Helper\SendBasicEmail $sendBasicEmail, protected \BredaBeds\EmailAttachments\Helper\Browserless $browserless ) { } protected function sendTestEmail() { $absolutePath = $this->browserless->generatePdfFromUrl( 'https://google.com', // URL 'google.pdf', // Filename to use 'pdf/files' // Location in the "var" path ); // Saves a PDF to: "/path/to/magento/var/pdf/files/google.pdf" $this->sendBasicEmail->sendEmail( 'test@example.com', // To 'Your PDF Document', // Subject 'Your PDF document is attached. Please contact us if you have any questions or concerns.', // Body [$absolutePath] // Optional: Sets attachments via template variables, see methods 1-3 above. ); // Sends an email with the previously saved PDF }
For Browserless to work, add to the ./app/etc/env.php file:
'bredabeds' => [
'browserless' => [
'endpoint' => '127.0.0.1:3000',
'token' => 'secret-token'
]
]
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2025-04-09