简体   繁体   English

如何在 Shopware 6 controller 中发送 email?

[英]How do you send an email in Shopware 6 controller?

In Shopware 5 there was a mail->send() function which was sending an email with the desired template and content.在 Shopware 5 中,有一个mail->send() function 正在发送带有所需模板和内容的 email。 What is the name of the function in Shopware 6? Shopware 6 中的 function 的名称是什么?

PS I see some files which I think are the ones needed, some examples of mail sending would be welcomed. PS我看到一些我认为是需要的文件,一些邮件发送的例子会受到欢迎。

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

Within Shopware 6 you also have the Mailservice which provides you a send() method .在 Shopware 6 中,您还拥有为您提供send() 方法的 Mailservice。

So basically a really simple example to use the service would be:所以基本上使用该服务的一个非常简单的例子是:

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(),
    );
}

Make also sure to tag the service within your services.xml .还要确保在您的 services.xml 中标记services.xml

<service id="Your\Namespace\Services\YourSendService">
  <argument id="Shopware\Core\Content\MailTemplate\Service\MailService" type="service"/>
</service>

Email Template Email 模板

If you want to use Email Templates, there is also a how-to add a mail template in a plugin如果您想使用 Email 模板,还有一个如何在插件中添加邮件模板

If you have an email template, you need to fetch it before sending the email.如果您有 email 模板,则需要在发送 email 之前获取它。 Then you're able to get the content from your email template to pass those values to the send() method.然后,您可以从 email 模板中获取内容,以将这些值传递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;
}

You can later than set those Email values which are coming from your Email template (which is also available within the administration ) instead of hardcoding it within your send method.您可以稍后设置来自您的 Email 模板(也可以在管理中使用)的 Email 值,而不是在您的发送方法中对其进行硬编码。

$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM