Laravel E-Commerce - Transactional Email - Part 19

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome back and in this video I'd like to spend some time showing you how to send transactional emails from within our application transactional emails are emails that get sent out by the application when some important event or some transaction has happened for example when you register for the site when you change your password or the example I'll be using is when we place an order in laravel each type of email is represented as a mailable class I think it's a very clean way to separate your email logic into its own class so let's go ahead and make a mailable for when an order is placed so let's do a PHP artisan make mail let's call it order placed and what's that what what that's gonna do is it's gonna make a new class in this mail directory called order placed and this is where all your logic for sending an email goes so let me show you how to send the most basic email at first and then we'll focus on passing the order in after that so here you can chain on a whole bunch of different methods so for example you can put the from field so admin admin com and you can also specify the name here so admin you can also specify where it's going so this is going to this person over here and again you can specify the name name of person let me just pull this up and then we can specify other things like for example if you one sent a BCC just put the email in here we can specify the subject so the subject would be whatever subject line and then the view is the view we're gonna be using as an email template so we'll make one called emails got orders dot traced so let's go ahead and make that view and to our resources views let's make a new one called emails and folder emails let's make a new one called orders and new file called placed top blade of PHP now email templates are a huge topic and I'm not gonna be talking too much about it but definitely take a look at using templates that already exist because there's a whole bunch of different markup you have to write when you're writing emails it's all like old-school table format here's one free one that's pretty popular its foundations templates and you can just use these if you like for example here's a very basic template or if you want to get fancy you can like go in for store something and there should be a whole bunch of nice nicer templates here so marketing email templates so in my example I'm not gonna be using your names gonna make some dummy markup here but later on I'll show you the predefined ones that laravel has and we'll make use of those well let's just say email and as you say this is the email that is being sent out okay now to actually send the email it's going to our check out controller that I said I want this email to be sent out as we finished the order so let me just put some stuff in my card here at the card check out guest is fine and after we submit this if it's successful then I want it to send an email and again I'm using mail trap here I rely on this service heavily so I'll definitely use it if you're testing emails it's a back to our code so checkout controller all right after we put it into our database let's send the mail so we'll call the mail facade and we'll call send and we'll just pass it that mailable we just made so it's called order placed and it is doesn't take any parameters for now make sure we import mail make sure we import order placed and if we did that correctly that should send the email before I do that I just want to note that where order placed so all of these that you chain on here so from to BCC all of this can be also defined in here so a lot of times the email address is coming in from the request and you don't have access to that in the mailable so you can just do something like this like request email and then you can chain on the send them there but we're just going to leave like that for our case all right see if this works like I said in my environment file I'm already set up for mail trap so this should work if we just fill this out and hopefully this works and it looked like it did check our mail track here and there it is this is the email that's being sent out and you can see it's from we said we specified that - and we also specified a BCC if you noticed after I click the checkout button it took a few seconds to process the email I mean it's doing other things as well but the sending of the email takes the longest it takes about one to two seconds which is not a huge deal but you should definitely be utilizing queues when you're sending out mail now I'm not going to do that I already have another video on how to use cues and laravel horizon and I have a few examples of sending out mail so definitely check that out and you'll also have to instead of mail send you'll do mail queue when you're doing that but like I said for this example we'll just do it the normal way all right now let's pass in the order so when I make a new mail bowl I want to be able to pass in order like this first thing I want to do is in this helper method over here I want to return the order so let's return that so where is it this one where we're just inserting into the database I'll just return this order over here okay now in our order placed mailable so to pass in properties all you have to do is specify it using public so public order and then we're going to pass it into the constructor so we'll just do order order make sure to import that and then we'll just assign it okay this from can actually be moved to your environment variables so if you look at config mail you'll see a option here for from so you can see it's gonna get it from this environment variable so if you if you specify this in your environment file so mail from address mail from name then it's gonna get it from that so let's do that so mail from address equals min and min calm and mail from make equals admin cool and now we can take this line out sorry I forgot a semicolon up here and now instead of hard-coding this we can get a 50 order so this order billing email and then this order building name this can stay the same let's put a more meaningful subject order for laravel ecommerce example and you can put like the order ID in there if you want but I'm not gonna do that okay save that and if we did that correctly then then that should pass in the order so laptop one actually no we have to work on the view so let's go back to placed and now we have access to the order variable so let me just put this in a paragraph tag and let me just paste this in so you'll have to watch me type it and this is just listing out the order details and you can put as much as you want in here but these are the ones that I'm putting in so just the order ID order email billing email and the order total so now if we do this our email should include that information so okay and also but like the product in here but I won't do that like you have that relationship so you can do order products and then you can loop through that and listed it here but maybe I'll do it later okay specify that put this end okay let's see if that went through it did and there is our information cool all right now let me show you how to make use of layer valves default email templates you can actually see it when you're doing like a reset password let me see if I actually have it in L trap I guess I don't but you know the authentication when you're doing forgot password here this thing there's actually a default email template that Arabelle uses and let me show you how to use it so I'm gonna paste some code in here and here it is it using this component which is the default message and just like before we're just passing in the order details but instead of HTML it's now in markdown and it has several components which you can actually customize and I'll show you that in a second but it's a bit cleaner to use markdown in my opinion and all you have to do to change that instead of a view you would specify markdown now something else I want to show you is how to render emails inside your browser instead of always using mail trap which can get quite slow so all you have to do let's me go to my routes file here let me make a new one let's call it mailable it doesn't need any programs and let's just grab a random order so up order fine subscribe the first one and all you have to do to render this in the browser is just return the mailable so no app mail order placed and that's in the order and we can see this in the browser if we hit it instead of always going through mail trap so mailable and there it is there's the default email template and let me show you how you can customize this if you don't like the headers for whatever reason so what can you do if you do a PHP artisan vendor publish and we want this one here laravel mail so let's specify 5:15 and as you can see it made a new folder with all of that mail views in there so right views vendor mail so here's all the markdown and the corresponding HTML so if you want to change the header you can go ahead and do that like right here and you can just customize it to your liking you can even specify a theme for your CSS here and you can customize it as much as you want let's just do one more test to make sure that this works make sure it actually gets sent as an email so it's gonna put some products in here actually no one do I'm gonna also include the products so right here I actually have a heading for it already so let's just do a for each for each order products as product we're just gonna do name price and quantity now when you take mark down you shouldn't indent too much because markdown thinks that indentation is is code so try not to indent too much I'm gonna paste this in actually so you'll have to watch me so there we go I'm justifying the product name a price and the quantity so let's see if that goes through in our email so let me put more than one product so twelve and let's add a phone check out and just fill us out I'm sorry okay save that worked there you go there's the default level template with our own information and there's the two items that we ordered again it's not the prettiest but you can spend some time to make it look nice and this is actually your app name in your environment file so is this so if you look here just going to fake a flame so if you just change the app name in your environment variable here where is the app name yeah nerville ecommerce example then that will change in the email okay one more thing I want to do is actually send out real emails using a real transactional email provider I personally use mailgun and they're pretty good I'm pretty satisfied with them I don't have any experience using other services so I can't compare other services include mandrill and Amazon SES but like I said I use Milligan I think ten thousand is free and after that it's pretty cheap their service is actually pretty good and I'm pretty happy with them so let's just mailgun set it up because it was actually quite a bit of setup involved in terms of setting up MX records and a whole bunch of DNS stuff which I don't remember how to do so we'll see how this goes I mean I've done it for other applications but it's been a while so let's do it together before we do that if we go into the actual web server I actually just pushed the code using on foyer here and my male trap credentials are still in the environment well I just wanna see if an email gets sent out using male trap on the real server so let me just add guessed the law for twos okay let me see if this works okay looks like it does let's check me I'll trap and there it is cool so that's the works now let's set up mail going all right so let's make a new domain and it says here that they recommend using a sub domain so let's go ahead and do that it's to M G dot laravel ecommerce example dot c a let me make sure I spell that right okay let's add a domain okay so it's it's saying that we need to add these DNS records and these values so we have some txt records and we have some MX records and this one too if we want to set up tracking we don't need this one actually I mean if you want to set up tracking I've never done this so I'm not gonna do it we'll do these ones so I have my digitalocean here and I'm in networking and laravel ecommerce is right here so let's manage the domain and let's set up these MX records so I mean the CDs ones first okay so txt hostname is this and the values that okay so txt record hostname is this is that right no it's just mg yeah you see how uh digitalocean depends there for you okay the value is this don't ask me what these mean because I have no idea what text records are or what MX records are I just know that I have to do with email okay so that's done is to it for the next one another text record call is that so the host name is this yep value is this a long string oops okay and create a record okay now let's set up some MX records okay MX what's the host name for this I think it's just male prior server yeah I think it's just the default it's actually just mg let's grab this ace that in priority is it priority 10 ok create that record and the next one is same thing but with be so mg B 10 create record okay so we have these two MX records and we have these two new text records is that all wait for your domain to verify oh I can take 24 to 48 hours let's see if it worked okay we can actually check it here but like I said it might take 24 to 48 hours to propagate or if we're lucky it can work right now and yeah it looks like it worked okay so now all we have to do is grab our SMTP login and password and change the SMTP server name in our environment file and that should work okay so the SMTP host name is this we want the on lawyer servers managed environment okay so here is where mail trap was defined before but now we're going to use mail gun so the host name is this instead of mail trap we'll use this the login is this username I mean and the password is this here we go paste that in and I think this can stay the same update on servers that should work and we cross our fingers and do this again this time I'm gonna put my real email address in here and then we are gonna hope that it works cool and it looked like it did let me check my email come on email nothing nothing junk there we go yeah we do have it is this it yeah I don't know why it went to my junk oh because we didn't specify yeah like the email name so hotmail is identifying it as junk so yeah I'm not too worried about that because this is more of an email client issue you can try you can try to change the name of this email and maybe that will work but again it's more of an email client issue if you look at this this actual email from Milligan was detected as junk as well so again not to worry about that so there you have it guys we managed to use transactional email in our application we've managed to use mailable x' we've managed to use markdown mailable x' and we've actually used a real email transactional service to send out real emails please like comment and subscribe if you haven't already done so thanks for watching guys see you in the next one okay thanks bye [Music]
Info
Channel: Andre Madarang
Views: 10,494
Rating: undefined out of 5
Keywords: laravel ecommerce, laravel e-commerce, ecommerce laravel, e-commerce laravel, drehimself, andre madarang, laravel voyager, laravel admin, admin laravel, laravel admin builder, laravel crud builder, laravel email, email laravel, laravel mailgun, mailgun laravel, laravel mailtrap, mailtrap laravel, laravel transactional email, laravel mailable, mailables laravel, laravel mailable markdown, laravel mail tutorial, laravel email tutorial, laravel mx records
Id: yU7BcYXfWyw
Channel Id: undefined
Length: 29min 31sec (1771 seconds)
Published: Wed Feb 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.