Shopware 6 - Tips, tricks and how to's

How to send emails from Shopware 6 programmatically

Lately, I have needed to send an email notification from one Shopware 6 store, if something happened in one of the background scripts. Of course, I could have just used the standard PHP mail function, but I thought, that Shopware certainly must have something of its own. And it has! There is a class Shopware\Core\Content\Mail\Service\AbstractMailService, that contains the “send” method, that we can use to send emails. So now you know, how to send emails in Shopware 6. You are welcome, end of the article.

Of course not, just kidding. 😉 As usual, we will take a look at some examples and talk a bit about the code. In the end, our goal here will be to create a standalone email service, that you can copy to your plugin, register in your services.xml file and then inject it to any class, from which you need it to send some emails for you.

This presumes, that you already have a plugin with a class, to which you could inject our new email service. If you don’t have a plugin yet, then you can for example download and use the sample “skeleton plugin”, available in my Shopware 6 plugin programming tutorial. In the following examples, I will use the name “TestPlugin”, so do not forget to replace it by the name of your plugin, if you want to copy and use my code.

A simple email service for Shopware 6

Without further introduction, here is the code of the email service:

I think it is quite obvious, that the “EmailService” class contains just one method – “sendMail”. To this method, you just have to pass the mandatory parameters. First of all, you need to prepare the email recipients as an array, where the email addresses are keys and the recipient names are values. Then you need a sender name, that you want to display to the person, who receives the email in their favorite email program. And obviously, you need to specify the subject and the content of the email message. The content could be an HTML. The class strips the HTML tags for the plaintext version of the email automatically. Last, but not least, you have to specify the sales channel context – more on that later..

So, now we have our mail service wrapper. Here is how we register it for Shopware in the services.xml file (located in the src/Resources/config in your plugin’s main directory):

You may have expected the fore mentioned Shopware\Core\Content\Mail\Service\AbstractMailService class to be added as an argument to the services.xml file. However, there is another service instead. That is, because the AbstractMailService is an abstract class, which means it can not be instantiated. Therefore, we specify the Shopware\Core\Content\Mail\Service\MailService, which extends this abstract class, to be the one, that gets used in the end. Like this, we could use any other class, that would extend the AbstractMailService class.

And finally, here is how we could inject the email service to some other class and send an email from it:

This was tested and works on Shopware 6.4. It should be all you need to be able to send emails from Shopware 6. The article could end here now with a clear conscience, but let’s take it one step further and make some enhancements.

An advanced email service for Shopware 6

The simple email service is all nice and good for explaining things, but for real world usage, we should add some more perks and features to make our standalone service more robust and practical. So here is what I have added:

  • specify the plaintext and HTML version of the message separately
  • be able to use any version of the message for both cases
  • create sales channel context automatically
  • choose the template for the email
  • return success or failure

Here is the code for my advanced Shopware 6 email service:

As you can see, quite a lot of code was added..

The newly added getMailTemplate method selects a specified (or default) template to be used from the template repository. You can find this repository as a table named “mail_template”, although for better orientation, I recommend the table “mail_template_translation”.

You can also take a look at the email templates in the Shopware 6 Administration. Just go to Settings – Shop – Email templates. The list looks like this:

To get the UUID of the template, that you want to use as a parameter for the getMailTemplate method, you can click on “Edit” and you will then see this ID in an address bar.

For the automatic creation of the sales channel context, I have used an extended version of the class from my article on the topic of Shopware 6 sales channel context creation:

Of course, the services.xml file has to be adjusted accordingly:

And here is an example of the advanced email service call, that you can use from your own plugin and class:

That is it for now. I am satisfied with this version of my email service for Shopware 6 for the time being. It is good enough for my current purposes, but I am sure it could be taken even further. If you have some ideas on how to enhance it even further or have already added some features, you can share them with others in the comments below.