php - How do you send an email in Shopware 6 controller?

In Shopware 5 there was amail->send()
function which was sending an email with the desired template and content.
What is the name of the function in Shopware 6?
P.S. I see some files which I think are the ones needed, some examples of mail sending would be welcomed.
Shopware\Core\Content\MailTemplate\Service\MessageFactory.php
Shopware\Core\Content\MailTemplate\Service\MailSender.php
Answer
Solution:
Within Shopware 6 you also have the Mailservice which provides you a send() method.
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',
[
'[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(),
);
}
Make also sure to tag the service within yourservices.xml
.
<service id="Your\Namespace\Services\YourSendService">
<argument id="Shopware\Core\Content\MailTemplate\Service\MailService" type="service"/>
</service>
Email Template
If you want to use Email Templates, there is also a how-to add a mail template in a plugin
If you have an email template, you need to fetch it before sending the email. Then you're able to get the content from your email template to pass those values to thesend()
method.
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.
$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: please make sure the php redis extension is installed and enabled.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.