Directus 9 Episode 2

Send Emails With Directus Flows

Flows in Directus enable custom, event-driven data processing and task automation. In this video we are going to send an email to the admin, when new user is created.

Flows are a huge concept and allow us to do large number of automation tasks. Because I can’t cover everything in one video, we are going to focus on sending emails when something happens in our application.

Be sure to let me know down in the comments what other Flows you would like to see on this channel.

What are flows

Flows are a low code way of setting up automation tasks in Directus. They consist of triggers and operations.

Triggers are actions or events that are happening inside your Directus app like when something is created, when there is an incoming webhook, when the cron job has started and so on.

An operation is an action or process performed within the flow. These enable you to manage data: send off emails, push in-app notifications, send webhooks and more.

In this video we are going to send an email to the admin, when new user is created

Mailgun

First we need to setup our email provider - something to send our emails with. In Directus you can use your own SMTP server, sendmail or Mailgun.

For this video we are going to use Mailgun because it is the easiest to setup, and has a free tier. Of course for production websites you would either have to pay for Mailgun or use some other method, like sendmail which should be available on most Linux systems.

First signup to Mailgun and login into their dashboard. Here you will see your sending domain. Click on it. And then select API. Here you can see your API key and your domain name, which we will need to setup in Directus.

But first note that sandbox domains are restricted to authorised recipients only. So add your email address here. I will use the one from temp-mail.org.

You will now get an email to verify that address. Verify it.

Now in the .env file of your Directus app add the API key, and Mailgun domain. That is the last part of the “API base URL” in your Mailgun dashboard.

Also be sure to comment out any other mail delivery options if they are enabled in the .env file.

Now we can run our Directus app, using npx directus start

Setup simple flow

Let’s now create a flow that will send an email to us once the user has signed up. Or when we create a user. We will create our users through the dashboard, but this will also work when someone registers from the frontend of your app or website.

Go to settings and click on Flows.

Click on Create Flow.

We are going to give it a name of “Send email”.

Description can be “Send email when new user is created”.

Ok, now click on the arrow, so we can set up our trigger.

Remember, every flow needs to have a trigger.

As you can see you have a lot of options here, like Event Hooks, Webhooks, CRON jobs and so on. We will choose Event Hook.

And then select “Action (non-blocking)“.

For the Scope select items.create because we want our flow to trigger once the user is created.

As you can see there are no users in this short list, so we have to click on “Show 14 more”. And then click on Directus users.

Click the checkmark to confirm.

Ok, so now we have or trigger set up, be sure that it is set to be active. Inactive flows will not work.

Click on the plus symbol to create an operation for our flow. We will name our operation, “Mail send”.

Directus has a number of available operations like: conditions, create data, delete data, trigger another flow and so on.

We will of course be using Send Email operation.

Fill the “To” field with the email that you verified in the Mailgun. And be sure to press Enter.

Subject is going to be “New user has been created”.

Set the type to be WYSIWYG.

And for the body we can add something like “User has been created” for now. A bit later we will make this a bit more dynamic. For now let’s just test if this works.

Click on the checkmark to confirm it. And then once more to save our flow.

Now let’s go to the users and create a new user.

Check your email. If you didn’t get the email, try checking your spam folder. I will check my mail on temp-mail.org.

Here we have our Email. Great, it works.

Make it a bit more dynamic

Ok, now I want to make the email that we receive to have a bit more data about the user that was just created.

Let’s go to flows again.

Click on our flow, click the pen icon so we can start editing, and then again on the Send Email operation.

When creating flows we have access to the triggers payload. To access it we would write something like this in the Body of an email.

”User {{$trigger.payload}} has been created.”

Let’s save this. And then also save our flow.

Now we will create another user to see what our email looks like now.

Go to users and create a new user.

Great now let’s check our email.

As you can see, here we get full payload object, that we can use inside of our emails. Of course we don’t wanna send full object to the admin, but maybe just first and last name.

Let’s go to the flows again and edit the Body of an email one more time.

Now instead of {{$trigger.payload}} we will write {{$trigger.payload.first_name}} and we will add {{$trigger.payload.last_name}}

Save everything and let’s add another user to see if everything is working as it should.

Let’s check our email.

Nice. Now it says “User Matt Smith has been created”.

Conclusion

As you can see Flows are very powerful, and can enable you to do lots of cool stuff. We just created a very simple Flow, but flows can have multiple operations and even multiple flows that depend on one another.

Maybe we will cover that in another video, so be sure to let me know what kind of flows you would like to see on this channel in the comments below.

Wanna ask a question about video?

Like This Video?

Support it by sharing it ;)

© Watch And Learn,2024