تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Adding Auth Middleware0:00

So here's where we left off in the previous episode. We can now successfully render a Vue into HTML, and then convert that HTML to a PDF that we save to our storage/app directory. But one thing I noticed is that I'm assuming I have a signed-in User. Let's fix that real quick in my routes file. Okay, so once again, here's my route, and I'm hiding all of the other routes up top. Let's simply add a middleware of auth, and that should do it. Cool. So now if I switch back, how do we want to do this? How do we fire off an email?

Choosing Email Testing Tools0:33

So now if I switch back, how do we want to do this? How do we fire off an email? Well first, I need to figure out how I want to test this. Do we simply log the email, or should we use a dedicated client or service? Let's have a look at a few options you might consider. MailHog is a dead simple and easy to use web-based and API-based SMTP server. Really easy to use. You can fire it up from the command line, and you're all set to go. Another option is MailTrap, and this works great as well. It looks good.

Installing and Configuring MailHog1:20

It does not matter. I generally use MailHog, again, because it's dead simple, and it mostly does what I need. Okay, so if you want to install that on your machine, let's see. On a Mac, you can use Homebrew. And if you're not familiar with Homebrew, it is a great tool. Just visit brew.sh, and you can install it in seconds. It's a package manager for the Mac. Allows you to install any number of things, including MailHog. Once you do that, you can run this command, MailHog, to set up your server. Otherwise, if you're not on a Mac, just have a look at the instructions for your OS of choice.

Once you do that, you can run this command, MailHog, to set up your server. Otherwise, if you're not on a Mac, just have a look at the instructions for your OS of choice. Finally, for configuration in your .env file, we want to make sure we're on localhost, and we have the port set to 1025. So real quick, if I visit my .env file, here's what I have for the mail settings. Again, the host is localhost, and the port is 1025. Alright, so let's give this a shot. MailHog, and have a look here, being served at this URL. It works. Okay, so let's give it a shot.

Sending a Test Email2:20

It works. Okay, so let's give it a shot. Let's boot up tinker, and if I want to quickly send a raw email, we can use raw, hello viewers, and then this will accept the message, and we can declare who it's to. Let's just send it to support@lericast.com. And yeah, I think that should do it. So if you configured everything properly, you can now send and preview an email to MailHog. Very cool. Let's get back to work. So we've successfully saved our PDF.

Creating InvoicePaid Notification2:50

Let's get back to work. So we've successfully saved our PDF. Now I want to notify the User. For that, let's create a new layer of Notification. I can use the make:notification command for that, and I'll say InvoicePaid, and let's have a look at that. InvoicePaid, and yeah, let's see down here, we're just going to build it up dynamically. You have a new paid invoice from Leracasts. Of course, behind the scenes, I'd take a little more time to fill this out. Thank you for using our application.

Of course, behind the scenes, I'd take a little more time to fill this out. Thank you for using our application. And now I want to attach that PDF. Now there should be, let's see, attach, and yeah, so we're going to need a path to the file that we're attaching, so something like PDF. And then I know one thing we can do is override, if I need to set, what is this treated as? And I don't know, I might need to further configure that, but let's try this first. Okay, so this will need a path to our PDF. So PDFPath, why don't we do that? And then come back down and let's update this.

So PDF path, why don't we do that? And then come back down and let's update this. Or actually, you know what, I don't like PDF path. Why don't we change that to invoice path? And update that, I'm sorry, invoice path. Okay, I think that should do it. So now, if we come back to my controller here, yeah, I could say, give me the current user, and we will notify them that they have a new, what did I call it, paid invoice, invoice paid, and then we just have to give it a path to the invoice. So why don't we say invoice path, I don't think the save method, does that return the

paid, and then we just have to give it a path to the invoice. So why don't we say invoicePath, I don't think the save method, does that return the path or anything? No, it doesn't return anything. Okay, so let's prepare a path to where the PDF is saved. And then we will build up a notification and email the current user. And again, here, I will say return, done. All right, let's give it a shot. So we will come back and give this a refresh. And we failed, expecting a semicolon.

This is the one we want. We have attached the PDF. I mean, come on, how easy is that? It's ridiculous. It's so simple. Let's switch back. We have the body of our email. Again, I would configure this a little more behind the scenes, but I think you get the idea. So now, in the next episode, why don't we convert the logic in this controller into

Planning a Queueable Job5:27

Idea. So now, in the next episode, why don't we convert the logic in this controller into a reusable and queueable job?

MailhogLaravel Notifications

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟