gpt4 book ai didi

php - 如何在 Shopware 6 Controller 中发送电子邮件?

转载 作者:行者123 更新时间:2023-12-05 00:45:53 24 4
gpt4 key购买 nike

在 Shopware 5 中有一个 mail->send() 函数,用于发送带有所需模板和内容的电子邮件。Shopware 6 中的函数名称是什么?

附:我看到一些我认为需要的文件,欢迎提供一些邮件发送示例。

Shopware\Core\Content\MailTemplate\Service\MessageFactory.php
Shopware\Core\Content\MailTemplate\Service\MailSender.php

最佳答案

在 Shopware 6 中,您还拥有为您提供 send() method 的邮件服务。 .

所以基本上使用该服务的一个非常简单的例子是:

public function __construct(
MailServiceInterface $mailService,
) {
$this->mailService = $mailService;
}

private function sendMyMail(SalesChannelContext $salesChannelContext): void
{
$data = new ParameterBag();
$data->set(
'recipients',
[
'foo@bar.com' => '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());

关于php - 如何在 Shopware 6 Controller 中发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62633370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com