Sending Welcome Email 0:00So let's imagine, if we go into our controllers directory, that when a new user successfully registers, here's the store method they would hit, where we validate the request, create the user, and sign them in. But maybe, also as part of that, we want to send them a welcome email. We can do that by using the mail facade. So let's import that, and we can say mail to the user that we just created. So this will fetch the email address off of that, and we will send a new email of some sort. Okay, but we have to create this email. So let's take a look. Generating Mailable Class 0:32Okay, but we have to create this email. So let's take a look. Let's run phpArtisan once again, and like many other things, there is a generator for a new mail class. All right, let's take a look, phpArtisan makeMail, and we'll give this a name that corresponds to what it is. It could be orderProcessed, or projectCompleted, or commentReceived, or in this case, how about just simply welcome. It's a welcome email. So now you'll see that Laravel will generate this new mail folder, and we have our welcomeIt's a welcome email. So now you'll see that Laravel will generate this new mail folder, and we have our welcome email. So that means we'll new up a new welcome, and we'll go ahead and import that, useAppMailWelcome. Now let's take a look at this class. If we check, we can see, all right, there's a build method where we can reference a view. Now this view name should correspond to the email that you want to send. So for example, we could say emails.welcome. Now we could create a resources, views, emails, and then a welcome.blade.php file. So let's just nest a whole bunch here, and I'm going to say welcome to Laracast in this Configuring Mail Drivers 1:26Now we could create a resources, views, emails, and then a welcome.blade.php file. So let's just nest a whole bunch here, and I'm going to say welcome to Laracast in this case, and then of course you would populate that. Okay, we'll keep it like that. Now we can do a lot more here, but this will get us started. So let's close this out and check it. When the user signs up, we will send a new mail to the user, and the specific email we send is the welcome mail. But now how does Laravel go about even sending this mail? Well if we go into config slash mail, you'll see that we can specify one of many supportedBut now how does Laravel go about even sending this mail? Well if we go into config slash mail, you'll see that we can specify one of many supported mail drivers, and this will be especially useful when you push to production. Maybe you want to use Mailgun or Mandrill or SparkPost. All of those are excellent services that can take care of this for you, and Laravel includes support out of the box. So if we visit our env file for the local environment, let's scroll down to mail. We can see the default mail driver is SMTP, but it specifies a host of MailTrap, and it has a port specific for that. Okay, let's take a look at MailTrap. Testing Email with MailTrap 2:32has a port specific for that. Okay, let's take a look at MailTrap. MailTrap is an optional service. You don't have to use this. You can reference anything you want, but it works really good for testing email. Let's take a look. You can sign up, get started sending them for free, and yeah, you can sign up through GitHub. I already have an account though, so I will log in, and we'll create a new inbox here, and we'll call it demo.I already have an account though, so I will log in, and we'll create a new inbox here, and we'll call it demo. Okay, so if we take a look, we're going to see our credentials here, and notice the host and the port name match up with what you see right here. So let's just substitute our username, as well as the password, and believe it or not, we're all set to go. Now when we fire off this email, it's going to use this configuration, which means MailTrap will receive the email. Let's give it a shot. Let's register. Setting Global From Address 3:19Let's give it a shot. Let's register. Let's do test, test at example.com, and there we go. We've created our account, and we should also have a new email, and sure enough, we do. Simple as that. But now, notice that from is set to example, so where can we configure this? Well, I'd recommend going to your config slash mail file, and then if we scroll down, you can see there is a global from address. Now, once again, you can set an environment variable if this will be different from local to production. Personalizing Email Content 3:47Now, once again, you can set an environment variable if this will be different from local to production. Otherwise, we can just hard code it. All right, now all emails will be sent from that address. Now what if we want to configure this email? For example, we want to send a welcome email, but I'd prefer that the email say something like welcome to Larracas, comma, and then the name of the person who signed up. Okay, in those situations, we can pass through the user who signed up to the welcome email. Now, if we switch over there, so let's assign this, and I'll use a little macro here. Finally, yeah, we can type into this if we want, and then import that.Now, if we switch over there, so let's assign this, and I'll use a little macro here. Finally, yeah, we can type into this if we want, and then import that. All right, at the very top. Okay, so now when we new up a welcome email, we're going to accept a user, and a neat thing about these mailable classes is that any public property you define on it will immediately be available to the view. Very useful. Alternatively, you can pass a with method in the exact same way that you normally load a view. So now, check it out.a view. So now, check it out. If we go back to emails welcome, we could say welcome to Larracas, user, name. Okay, let's give it another shot. So let's go ahead and delete this one. And now we will log out and register a new user. Okay, so we should have a new email, and sure enough, welcome to Larracas, and that's the person's name in this case. So this is incredibly useful. It gives us a nice terse API that we can reference.So this is incredibly useful. It gives us a nice terse API that we can reference. I love it. But one thing is that it's still on us to actually create a nice email and format it nicely. But what if Larafel could assist in that area as well? Let's take a look in the next episode.