· laravel · 3 min read

Day 3: Building a SaaS with Laravel

Episode 3! Queue, email, database ! a lot of good things

My goal for the day was to finish the homepage, which is currently just a “work-in-progress” page where people can enter their email, and so I “subscribe them” to the notify list.

It might look simple, but I had to touch on several aspects:

  • Database
  • Configure a mail server (even two because of “dev and production”)
  • Mail template
  • Controller that verifies the email
  • Play with the Queue
  • Install a component library (MaryUI) just to make things a bit nicer.
  • Alpine because I tyr to first dispatch an event … but then move to toastr

I was considering buying TablePlus because it looks great but finally discovered DataflareApp, which is nice too and free :) However, it’s still in development. I’ve tried DBeaver before, some Visual Studio Code extensions, and recently the built-in database “plugin” in PHPStorm.

DataflareApp

Nice Error!

I copied/pasted some code and, well, obviously didn’t adapt everything correctly. It made me realize how good the errors are in PHP. The proposed solution is not correct (I don’t have to run a migration but correct my table name).

Nice Error

Sent my first “Verify email”

I’ve used Mailtrap for the dev environment, so all the mail I send is caught into that database. A very convenient service, and you just have to set some .env variables.

First Email

Sending via the send() Method

I began by writing the following code:

  $newsletterObj = (new CreateNewsletterAction())->execute($validatedData);

  // Send verification email
  Mail::to($this->email)
      ->send(new NewsletterVerificationMail($newsletterObj));

But it was very slow… so I explored the QUEUE world. Thankfully, it’s quite easy with email; instead of using send, I opted for the queue.

  $newsletterObj = (new CreateNewsletterAction())->execute($validatedData);

  // Send verification email
  Mail::to($this->email)
      ->queue(new NewsletterVerificationMail($newsletterObj));

you also have to change the QUEUE_CONNECTION in your env

QUEUE_CONNECTION=database

and then execute

php artisan queue:table

php artisan migrate

Mail Server for Prod - Resend

They dub themselves as “email for developers” - it was quite easy to use; here is the doc for Laravel.

You can send up to 100 emails per day. I think it’s an okay solution for when you begin a project.

What’s wrong with Mailtrap?

I don’t know the reason, but Mailtrap stopped working after a moment. I thought it was because I configured Resend, but the issue came from the queue, which was “blocked.”

To solve the issue, I had to

php artisan queue:clear
php artisan queue:restart

Fly.io Queue Process?

Now that I’m using the queue system to send emails, I thought I had to configure a Queue process in fly.io. Well, I didn’t, and my emails are still being sent. Not sure how it works, really - I will need to dig deeper into that.

Back to Blog