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',
[
'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.
<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 the send() 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());