magento2 自定义发送多个附件 邮件
时间: 2023-10-23 12:14:13 浏览: 153
要在Magento 2中发送多个附件的电子邮件,您需要对`Magento\Framework\Mail\Template\TransportBuilder`类进行扩展。
下面是一个示例代码,它可以让您在Magento 2中发送多个附件的电子邮件:
1. 创建 `Vendor\Module\Model\Mail\Template\TransportBuilder.php` 文件并添加以下代码:
```php
<?php
namespace Vendor\Module\Model\Mail\Template;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\Template\TransportBuilder as MagentoTransportBuilder;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
class TransportBuilder extends MagentoTransportBuilder
{
/**
* @var array
*/
protected $attachments = [];
/**
* @param array $attachments
* @return $this
*/
public function addMultipleAttachment($attachments = [])
{
foreach ($attachments as $attachment) {
if (file_exists($attachment['path'])) {
$this->attachments[] = [
'type' => $attachment['type'],
'name' => $attachment['name'],
'path' => $attachment['path']
];
}
}
return $this;
}
/**
* @param null|string|array $to
* @param array $templateVars
* @param null|string $templateOptions
* @param null|string $transportOptions
*
* @throws MailException
*
* @return TransportInterfaceFactory
*/
public function getTransport(
$to = null,
array $templateVars = [],
$templateOptions = null,
$transportOptions = null
) {
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
$this->message->createAttachment(
file_get_contents($attachment['path']),
$attachment['type'],
\Zend_Mime::DISPOSITION_ATTACHMENT,
\Zend_Mime::ENCODING_BASE64,
$attachment['name']
);
}
}
return parent::getTransport($to, $templateVars, $templateOptions, $transportOptions);
}
}
```
2. 创建 `Vendor_Module` 模块的 `di.xml` 文件并添加以下代码:
```xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\Mail\Template\TransportBuilder" type="Vendor\Module\Model\Mail\Template\TransportBuilder" />
</config>
```
3. 在您的模块中使用以下代码发送多个附件的电子邮件:
```php
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
class SendEmail extends Action
{
/**
* @var TransportBuilder
*/
protected $transportBuilder;
/**
* @var StateInterface
*/
protected $inlineTranslation;
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @param Context $context
* @param TransportBuilder $transportBuilder
* @param StateInterface $inlineTranslation
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
StoreManagerInterface $storeManager
) {
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->storeManager = $storeManager;
parent::__construct($context);
}
/**
* @return void
*/
public function execute()
{
$attachmentOne = [
'name' => 'Attachment One',
'path' => 'path/to/attachment/one.pdf',
'type' => 'application/pdf'
];
$attachmentTwo = [
'name' => 'Attachment Two',
'path' => 'path/to/attachment/two.pdf',
'type' => 'application/pdf'
];
try {
$this->inlineTranslation->suspend();
$this->transportBuilder->setTemplateIdentifier('your_email_template_id')
->setTemplateOptions([
'area' => 'frontend',
'store' => $this->storeManager->getStore()->getId()
])
->setTemplateVars([])
->setFrom([
'email' => 'sender@example.com',
'name' => 'Sender Name'
])
->addTo('recipient@example.com', 'Recipient Name')
->addMultipleAttachment([$attachmentOne, $attachmentTwo])
->getTransport()
->sendMessage();
$this->inlineTranslation->resume();
$this->messageManager->addSuccess(__('Your email was sent successfully.'));
} catch (\Exception $e) {
$this->inlineTranslation->resume();
$this->messageManager->addError(__('There was an error sending your email. Please try again later.'));
}
return $this->_redirect('*/*/index');
}
}
```
以上代码将会发送带有两个附件的电子邮件。您可以根据自己的需要更改附件的数量和详细信息。
阅读全文