init
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
namespace Stemcell\Datacollection\Helper;
|
||||
|
||||
use Alekseon\CustomFormsEmailNotification\Block\Adminhtml\Form\Edit\Tab\ConfirmationEmail;
|
||||
use Laminas\Mime\Message;
|
||||
use Laminas\Mime\Mime;
|
||||
use Laminas\Mime\Part;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\Mail\Template\TransportBuilder;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Exception\MailException;
|
||||
use Magento\Framework\Translate\Inline\StateInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use \Magento\Framework\App\Helper\Context;
|
||||
|
||||
class Email extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
const FORM_RECORDS_NOTIFICATION_TEMPLATE = 'alekseon_custom_form_records_notification_email_template';
|
||||
|
||||
public function __construct(
|
||||
private TransportBuilder $transportBuilder,
|
||||
private StateInterface $inlineTranslation,
|
||||
private StoreManagerInterface $storeManager,
|
||||
private Filesystem $filesystem,
|
||||
Context $context
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送带Excel附件的邮件
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function sendEmailWithAttachment(array $recipientEmail, string $recipientName, array $excels)
|
||||
{
|
||||
$this->inlineTranslation->suspend();
|
||||
|
||||
try {
|
||||
$storeId = $this->storeManager->getStore()->getId();
|
||||
|
||||
// 邮件变量
|
||||
$templateVars = [
|
||||
'emailTitle' => $recipientName,
|
||||
'recordHtml' => '<p>邮件内容</p>'
|
||||
];
|
||||
|
||||
$templateId = self::FORM_RECORDS_NOTIFICATION_TEMPLATE;
|
||||
|
||||
// 发件人信息(定义在 Stores > Configuration > General > Store Email Addresses)
|
||||
$from = [
|
||||
'email' => $this->scopeConfig->getValue('trans_email/ident_general/email'),
|
||||
'name' => 'Stemcell'
|
||||
];
|
||||
|
||||
// 创建 Transport
|
||||
$transport = $this->transportBuilder
|
||||
->setTemplateIdentifier($templateId)
|
||||
->setTemplateOptions([
|
||||
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
|
||||
'store' => $storeId
|
||||
])
|
||||
->setTemplateVars($templateVars)
|
||||
->setFromByScope($from)
|
||||
->addTo($recipientEmail)
|
||||
->getTransport();
|
||||
|
||||
$mail = $transport->getMessage();
|
||||
$body = new Message();
|
||||
foreach ($excels as $excel) {
|
||||
if (empty($excel['content'])) {
|
||||
echo $excel['title'].PHP_EOL;
|
||||
}
|
||||
$attachment = new Part($excel['content']);
|
||||
$attachment->type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||
$attachment->disposition = Mime::DISPOSITION_ATTACHMENT;
|
||||
$attachment->encoding = Mime::ENCODING_BASE64;
|
||||
$attachment->filename = mb_convert_encoding($excel['title'], 'GB2312');
|
||||
|
||||
// $body->addPart(new Part(' '));
|
||||
$body->addPart($attachment);
|
||||
|
||||
|
||||
}
|
||||
$mail->setBody($body);
|
||||
|
||||
$transport->sendMessage();
|
||||
|
||||
$this->inlineTranslation->resume();
|
||||
return true;
|
||||
|
||||
} catch (MailException $e) {
|
||||
$this->inlineTranslation->resume();
|
||||
throw new \Exception(__('邮件发送失败: %1', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user