MMMHUHU
在 Shopware 6 中,您还拥有 Mailservice,它为您提供send() 方法。所以基本上使用该服务的一个非常简单的示例是: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());