亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Shopware 6 控制器中發送電子郵件?

如何在 Shopware 6 控制器中發送電子郵件?

PHP
LEATH 2023-07-08 15:43:57
在 Shopware 5 中,有一個mail->send()功能可以發送包含所需模板和內容的電子郵件。Shopware 6 中的功能名稱是什么?PS我看到一些我認為是需要的文件,一些郵件發送的例子將受到歡迎。Shopware\Core\Content\MailTemplate\Service\MessageFactory.php Shopware\Core\Content\MailTemplate\Service\MailSender.php
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

在 Shopware 6 中,您還擁有 Mailservice,它為您提供send() 方法。

所以基本上使用該服務的一個非常簡單的示例是:

public function __construct(

? ? MailServiceInterface $mailService,

) {

? ? $this->mailService = $mailService;

}


private function sendMyMail(SalesChannelContext $salesChannelContext): void

{

? ? $data = new ParameterBag();

? ? $data->set(

? ? ? ? 'recipients',

? ? ? ? [

? ? ? ? ? ? '[email protected]' => 'John Doe'

? ? ? ? ]

? ? );


? ? $data->set('senderName', 'I am the Sender');


? ? $data->set('contentHtml', 'Foo bar');

? ? $data->set('contentPlain', 'Foo bar');

? ? $data->set('subject', 'The subject');

? ? $data->set('salesChannelId', $salesChannelContext->getSalesChannel()->getId());


? ? $this->mailService->send(

? ? ? ? $data->all(),

? ? ? ? $salesChannelContext->getContext(),

? ? );

}

還要確保在您的services.xml.


<service id="Your\Namespace\Services\YourSendService">

? <argument id="Shopware\Core\Content\MailTemplate\Service\MailService" type="service"/>

</service>

電子郵件模板


如果您想使用電子郵件模板,還有如何在插件中添加郵件模板


如果您有電子郵件模板,則需要在發送電子郵件之前獲取它。然后,您可以從電子郵件模板中獲取內容,以將這些值傳遞給該send()方法。


private function getMailTemplate(SalesChannelContext $salesChannelContext, string $technicalName): ?MailTemplateEntity

{

? ? $criteria = new Criteria();

? ? $criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName', $technicalName));

? ? $criteria->setLimit(1);


? ? /** @var MailTemplateEntity|null $mailTemplate */

? ? $mailTemplate = $this->mailTemplateRepository->search($criteria, $salesChannelContext->getContext())->first();


? ? return $mailTemplate;

}

您可以稍后設置來自電子郵件模板(也可以在管理中使用)的電子郵件值,而不是在發送方法中對其進行硬編碼。


$data->set('contentHtml', $mailTemplate->getContentHtml());

$data->set('contentPlain', $mailTemplate->getContentPlain());

$data->set('subject', $mailTemplate->getSubject());


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 177 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號