Emailing on Project Creation 0:00I'm back in my projects controller. Now two episodes ago when we talked about Laravel Telescope, as an example we had this section right here. I'm going to go over it one more time very quickly. When a user creates a new project, yes we need to validate it, yes we need to save it to the database, but also a side effect is we're going to email the creator of that project. Just a quick notification, hey your project has been created. Now we can do that through Laravel's mail facade, and you can import it like this if you need to. Anyways, if you need to email someone from your application, you need to do a couple Configuring Mail Drivers 0:33you need to. Anyways, if you need to email someone from your application, you need to do a couple things. First, in your config slash mail file, you need to choose your driver, and Laravel includes punch out of the box. Whether you use Mailgun or Mandrill or SparkPost, I use a tool called Postmark so I just go through SMTP, but no matter what you use, it should be fairly simple to get up and running. Now you can see by default Laravel assumes SMTP, but of course you're going to specify this from your .env file. So if I scroll down, you'll see at the moment I'm using the log driver, which means whenthis from your .env file. So if I scroll down, you'll see at the moment I'm using the log driver, which means when I do code like this, well just log it to a file. I don't actually want to go through SMTP and send an email, I just want to fake it. Log it to a file, I can review it there, and I'm good to go. That's a simple option, but it's not your only one. There's even dedicated testing servers you can use so that you're actually sending an email and you can review it in as close to a real world setting as possible. Anyways, so once you've configured all of this to your needs, if you are doing SMTP then you would update these as well. Adding Project Owner Relation 1:37Anyways, so once you've configured all of this to your needs, if you are doing SMTP then you would update these as well. Once you've configured that, you can say mail to the given person. Now in this case you can see I hardcoded it to be very quick, but in real life you would want to say, well, the email address of the person who created the project. So you might say the project owner email address, and let's make sure that relationship is right. I'm going to go to my project model, and let's see. We don't have a relationship yet between a project and the owner. So you'll remember if we just grab any project here, a project has an owner ID column. So that means if you want to fetch the owner associated with the project, you would saySo you'll remember if we just grab any project here, a project has an owner ID column. So that means if you want to fetch the owner associated with the project, you would say select star from users where the ID is equal to whatever value you have here. And that would give you the user associated with that project. But let's add an eloquent relationship to it. So yes, a project has many tasks, and yes, a project can add a task, but also a project has a user, if you want, or an owner, or a creator, or a maintainer. Whatever terminology, when you sit down with your co-workers, whatever terminology you guys use will be the one you reference here. I'm going to do owner.guys use will be the one you reference here. I'm going to do owner. So once again, what is the relationship? Does a project have many owners? Maybe. It depends on how you designed it. In our case though, a project has one owner. So it specifically belongs to an owner. There we go. We now have that relationship.There we go. We now have that relationship. So now think about it. If I have a given project, if I run project owner, that's going to give me the user instance of the person who created the project, which means you now have something like this. You have an instance just like this, at which point you can fetch the user's email. So we're going to fire off an email to that person who created the project. Let me show you this. Give me the most recently created project. Let's see what we got there.Give me the most recently created project. Let's see what we got there. All right. Write a book. Let's grab the owner of that project. Ah, it's myself. Then let's grab the email address of the owner, and there you go. That's my email address. Great. So send an email, and what specifically are we doing here? Creating a Mailable Class 4:01Great. So send an email, and what specifically are we doing here? We're creating a mailable instance. This is a feature of Laravel. They're called mailable classes. They are stored within your app mail directory, but by default, when you install Laravel, you won't have a mail directory. It will only be created once you scaffold one, and you can do that by saying phpArtisan make me a mailable. So that will give you something like this.make me a mailable. So that will give you something like this. And generally I like to name them, again, as readably as I can think. What does this email represent? Well, it represents that the project has been created. So what would be the email representation for a newly created project? Here you can see we're using a markdown mailable, and again, we learned about this. So if I use the help for that command, we can specify a markdown template to use, which means I could say make mail project created, and the markdown path will be emails.projectCreated. This is the path to a view that should be constructed and populated with a basic emailmeans I could say make mail project created, and the markdown path will be emails.projectCreated. This is the path to a view that should be constructed and populated with a basic email template. And I think you will most of the time reach for something like this, which is what we've done here. So if we scroll down to the resources directory, views, mail, there we go. And one cool thing is you get these little components out of the box. You haven't learned about blade components. It's almost like the opposite of extending blade the way you're used to. You're going in the opposite direction.It's almost like the opposite of extending blade the way you're used to. You're going in the opposite direction. But I don't necessarily want to go over it too much right now. If you're familiar with the view components, it's kind of a similar concept. It's very much inspired by view. So you now understand how to send an email. You can even queue it, which I will talk to you about at the end of this series. What a queue is, why you would reach for it, etc. For now though, we're going to do it all synchronously, which means we do not perform a redirect until all of these other actions complete. Testing Email Delivery 5:48For now though, we're going to do it all synchronously, which means we do not perform a redirect until all of these other actions complete. And as it turns out, sending an email takes a little bit of time. So this is where you would want to use a queue. So file that away and we'll talk about it more in the future. Let's see this in action. So I'm going to go to my storage, logs, and I'll find the log for today and just empty it out. And then I'm going to create a brand new project. New project with some lorem, some text.And then I'm going to create a brand new project. New project with some lorem, some text. Okay, so we've created the project, which means if I switch back, yes, we should have an email that was fired off. But also don't forget, you can go to project.test slash telescope, like we learned a couple lessons ago down to mail. And here we can see another view. Here are the details and here's the information that was fired off to the creator of that project. Now I'll show you one more little tip.project. Now I'll show you one more little tip. This is actually very beneficial. But if you want it to be as real world as possible, you can use a service called MailTrap. You can sign up very quickly and then once you do, you can log in. Here I'll log in with GitHub. And for the basic tier, it should be free. You can have one mailbox. Let's make one. All right.Let's make one. All right. Now here are the credentials that we'll need to specify in our environment file. So I'll grab the username. I'm just going to switch back to our .env file. And we're now going to say for my mail driver, we're going to do basic smtp. The host is smtp.mailtrap.io. You can grab that here if you didn't have it already. The port is 2525. There's the username.The port is 2525. There's the username. There's the password. And we're all set. So now when you create a project, it's actually going to send an email here. Let's try it again. Another new project. Create it. And now you'll see it's taking just a little bit longer. That took, I don't know, three seconds instead of a quarter of a second.And now you'll see it's taking just a little bit longer. That took, I don't know, three seconds instead of a quarter of a second. So again, this is why things like email should be sent through a queue, which we'll discuss later in this series. But if I switch back to MailTrap, you'll now see we do have an email and you can review it that way. So very, very useful here, and you can set this up in literally 30 seconds. Now you have two points to review the mail, here and through MailTrap. Now again, as we wrap up, don't forget on your mailable classes, any public properties you specify, even if it's public foo as far as anything public here, will be availableNow again, as we wrap up, don't forget on your mailable classes, any public properties you specify, even if it's public foo as far as anything public here, will be available within the view that you load. So if you open up your mail slash project created file, if you have this property here, you could then spit out boo or foo. That's a good thing to know. So don't set these to protected. Make them publicly accessible. And then further, if for your email you need specific data or an eloquent model or an email, anything you want, you pass it through to the constructor and then you assign it.And then further, if for your email you need specific data or an eloquent model or an email, anything you want, you pass it through to the constructor and then you assign it. So in this case, if we're going to construct an email for a created project, well I need to know what project that is. So if we switch back to our controller, you'll note that we are passing that in. Project created, and here is the project in question. Okay, so now that you understand mailables, we're going to keep talking about this a bit more in the next episode or two. We'll talk about alternative ways that you can trigger this logic. Because as it turns out, there's many. Alternatives to Trigger Mail 9:21We'll talk about alternative ways that you can trigger this logic. Because as it turns out, there's many. You can do it inline here. You can use a model hook. You can use an event. You can use a job. Lots of choices at your disposal. So let's move on to that in the next episode.