Codeplace | Send automated emails with Ruby on Rails using Mandrill

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello everyone welcome to another step cast and today I'm going to talk to you about action mailer so I have here opener in a rails guides page here on the action mailer section as you can see here on the introduction action mailer allows you to send emails from your application using mailer classes and views so as you can see so a lot like controllers you have the the respective classes and views just like you have other controllers classes then you have the respective views action mailer works exactly or very similar way to the controllers and so let me just show you the app you going to be able so this is it a very simple app as you can see no styling at all just lets see you sign in and I'm going to sign in with this account that I already have set up and you have just the hello John and you have just the an app that can make posts then the users can also comment on those posts and what it actually does is let me post here first post and some content when I create the post you can see here in my email which is using the same account you can see I have received an email already and he says post creator this is my first mailer okay so I send an email every time I create a post but now what I can actually do is the real life situation let me sign in with a different user the password is password and now I'm Mike I was John now I'm going to comment on the John's post if I come in here comment and I submit it see here I already received an email so commented created hi John I'm the post owner Mike has just commented on your post and this is the comment content this is what I'm going to show you a very simple app that I think you will understand very well how the action mailer works first I'm going to teach you how to send through your Gmail account which is perfectly fine if you want to send just a couple of emails but if you want to send a lots and lots of emails you should use a dedicated email service for that the one we're going to use is called mandrel and it's provided by MailChimp so you know it's good so without more further ado let's begin okay so I'll start off by doing the complete blog application as fast as I can so open the terminal go to your root folder and type rails new my mail app makes sense so the first thing we'll do is add a device authentication to our app so a user authentication if you go to the github device official page repository you can you can scroll down to the getting started getting started section and just follow along so the first thing we'll do after create the app is open our app so my mail app and here we are let's go to the gem file and let's put device here so let's go to CD my mail app okay let's clear the console and run the bundle sorry bundle okay so the second thing we need to do is install device so let's type this on your terminal the press enter is going to install and successfully installed afterwards let's create a model for our users so let's call user ok create all the files that we need and now let's generate a views okay down here on the configuring views you can just copy this command here go to your terminal paste it and you see here all the views created now I'm just going to do some changes to the device defaults I want to actually made make a new controller and let's call registrations underscore controller dot RV I just want to make some changes because I want to be able to register the user's name and for that I have a code snippet here that I'm going to paste and explain this very simple you create a controller called registrations and you put this code there are these two private methods essentially what it's doing is overriding a device's parameters so I want to add the reason why I did is because I want to add this name field here okay so the second thing we need to do is go into our migration and add the user name field so T dot string name okay you can close it close it and now let's go to our routes and we want to change this device for users but we want to specify a custom controller and the way to do this is you can see here tell the router to use this controller you can just copy this code here and paste it a change it for this one so it's still of course divided for users but now we specify the controllers here we want the registrations controller and our file is called registration so okay so this is all we need to do to set up our device so we are good to go and Creek lets migrate the database okay it's created now let's create them for the posts let's do a scaffold for the posts this generates scaffold post fields that we want our title which is a string content is text and we want to have some sort of relationship between users and posts so we want to put here a foreign key called user ID and this way rails is smart enough to know that this user ID is a foreign key and it will set all the relationships that we need okay it's done so now we can actually migrate the table now let's create the relations relationships between the posts in the user so we know that posts is going to have many posts and the user and the posts are it is going to belong to a user okay with this relationship here and the foreign key this is all that rails need to set up the relationship so now let's create just a controller for our home page so let's do rails g controller and we want just the home controller sorry and we want just one action which is index and on the index we're gonna have just a simple if condition here there let's go to a home controller under the index and we want to use actually one of device and helpers here as I can show you here these are all device helpers that you can use wherever you want so this one is a user sign and this is going to check if a user is signed in it and if it is we want them to redirect to the posts path okay this is this simply what we want to do and on the layout now let's go to the views layouts application HTML div in here we want to put this simple if-else condition - let me just copy and explain it to you so below the body and above the yield tag it's very simple here so if the user is sign in I wanted to display hello and the current user name this is another device helper that fetches the current user object so it you can then go search for the attributes so hello current user name and then the link to sign out if it is not sign in then give me these two links to sign in or to register okay this is all this all we need now let see what we have so far actually let's start the rail server I think there is no migration pending okay let's see what we have okay localhost this is the route because we didn't set up yet so let's go to work config route instead of get here let's put route and home action controller is home the action is index okay so let's refresh it and you can see it goes to the to the index action okay so you can see you have a sign-in form in a signup form very simple and it's notice that if I try actually it will allow me to go there yeah because first we need to set some user authentication in the post controller and we do this by pasting this before action the in the respective controller so let's go to our posts controller and lat right the first line should be before action authenticate users so you will all CENTAC eight the user each time you go to their XE you can see if I try to go to posts e will redirect me again to user signing okay so this is complete and now let's sign up with some users so okay so I forgot something I need to actually come here to the device views and I add that extra field but I and add it here at the actual views so let's put an extra field here it will be not an email filled by the text one is going to be named name let's take the outer focus of the second one and let's copy and paste it on the editor as well okay just remove the auto focus from the second one okay so this is all you need to be up and running if i refresh it now you can see you see here a name and name field and let's put the first one is Mike the email is that one password is password okay let's sign out and register another one this one is going to be John the email is my mailer app password password okay now let's create a let's actually change a little bit the post controller so that we can connect the post with the correspond to correspondent user the way to do is you open the post controller you come here to the create action and in here instead of just creating one individual or let's say independent post we're gonna assign it to a user so add user equals current user you know this helper already and in here instead of post new we want to put a user dot posts dot new so this will connect the post with the current user so if i refresh now if i type a new post actually i don't want the user here i'm gonna delete it afterwards so let's put first post as you can see here con content for the post i'm sorry i'm trying to do is as fast as I can and you see the user is - it was a sign - John okay so it's working now just let's just delete all that information on the post views that we don't need we don't need this field user ID here we don't need the the index as well we don't need this post user ID we don't need of course in the show this post or user ID okay we're all set now to create the actual posts so to do the posts let's generate a model let's clear the console rails G model comment and in the comment we want just content which is text and the same foreign key so we want to use the same religion relationship we had between the posts and users now we want it it's a comments and posts so we press enter it does what we want now just migrate the table and into the models let's create the relationship so a post as many comments and the comment belongs to a post okay this is all rail rails needs to config the relationships so now we are ready to actually make a controller for Thiam for the comments so let's create a comment the score controller dot RB in here I'm going to paste some code there comment common controller here we go explain you just to make it a little faster so we have just one method here which is create and the same way as the user so we first fetch the post and then we is assigned the post to that comments or else the comments that we're going to create we're going to assign to that particular post and then we redirect it to the post on which we commented and here is just a strong parameters as I'm sure you have seen already instead of doing all of this in here it just find a private method okay so now we want a partial to be rendered on the show on the show view of the post because we want to click the show post and then make a comment inside that post so let's create a new folder here the same as posts it must be with the same name so comments and here we have comments folder inside let's create a new partial for the forum so HTML at ARB and let's paste the code that we had from posts that's created here and let's just delete what we don't need we don't need this error and link their comments do not have titles they just have contents and this should be enough but we want to actually change this to be able to create comments so you're not owning reffering you're not only making a form referencing the the post itself but the comments of that post so you have to put an array and inside first is the post add post and then it's post comments comments not build this is a way to do it this will connect the comment that you are creating it will reference the same that particular post on which you are commenting and that's the way rails handles the the relationship so we can close this off and in here we want to have an h1 saying actually an h2 make my two big comments a and in here we're going to put a break line here and we gonna render the form here render so comments excuse me comments form is a partial that is right here now what we want to do is to render all the comments up here and the way to do is the way to do this is it's very smart indeed you'll see how smart rails can be because you can actually put this render post our comments what rails expects is that if you if you are ready but when you put render is is it's looking for a partial and if you put the instance I mean see the object of that post and the comments it's smart enough to know that you have to to have one partial named comment for each of the comments and I'm going to show you how to do that you come here and you create a new partial and you named it comment dot HTML a DRB and inside here you can actually relate this you can actually put this variable like this just comment not content he knows it is actually assigning this comment - it's like a for each is like a for each loop here is gonna for each comment for each comment there there there is it's going to look for thew partial and this is representing each comment it's going to loop through all the records there are ok so this is all you need to to to render and render the PO the comments end the form the last thing we need to do is going to routes and we need a nested route here we have to put resources posts do and inside resources comments what this does is I'm going to show you here what this is doing if you type rig rounds going to show you all the routes that you have on your application as you can see here this is this is the routing for the posts you see they are in depending then they don't need anything else but the post but in here you see it's a new post comment edit post comment and post comments so for you to create a new comment you need a post ID this is what NASA droughts nesib routing is is doing this actually ok so I think now we have all of this set up I think we are ready to create our first comments so let's check it let's try let's put rails s let's see what we have so far ok no errors which is good ok my post here let's and I'm John let's good show you see here the form there and content then for the comment let's see what happens ok it created the comment s10 which is related with a comment with 3m content is related to that first post there so it's working exactly like we want it now we were ready to start actually doing a some mailing so to do that you go here to your phone or mail or folders and you create a new class let's call it since the first mailing we're going to do it's about posts being created so let's call it post mailer RB ok now you actually have to create a class here the same name of course which inherits from action mailer base and in here you're going to have several methods as as much as you want and each method is a mail is an email we're going to send and each method or male has the respective view here and so first let's create the views actually the way to do it is create a new folder and following the rails conventionally must be the same name as the class so in our case post mailer without any extension of course so it will create a folder here called a post mailer at least at least I hope it would do you think maybe I put the I here it took a while but if it is post mailer now inside you're going to have the different templates just like the controllers you have the your controllers and you have the respective views the mailer is the same you have the class in the respective view inside that folder now in here it's going to save this file and this file it's a template so it's corresponds to each method imagine if you have a lot of methods here a lot of males each template must have the same name as the method so let's say this and I know that the first method is going to be called post created dot HTML or ARB ok let's just do it this post created ok so we have all the view setup now let's set up the mailer in here I know already it's called post created in here you pass up a method called mail inside the argument that it passes here is the mash in the inside an air hash you have the different headers mails the male headers so the first is tool next comes from then the subject and then if you want to do a very simple email you can put the actual body here so I think this is very self-explanatory so the 2 is going to be the user where you want to send this the from is the domain there or the email that you want to send from the subject is the subject and the body is the content of the email so let's do a let's try and do it the post that the mail that we want so we want to each time I create a post I received an email saying that I've just created a post so to do that what we need to do is - of course it's going to be we want it to be dynamic so we're going to pass here an argument called user and in here I'm going to call the user dot email now the from using the Gmail SMTP settings it will overwrite this from it will make no difference what you put in but on the mandrel it's a different case it will actually send this header so we are using a gmail so it won't make sense so let's let's just put something like services services at gmail.com you can do that there is no problem at all on the subject let's put post created ok on the body let's put this is my first mailer ok ok everything is very done now what we actually need to do is go to our controllers or posts controller where we want to call this mailer and the method so the way to do it is we want to go to the create action and if it saves so if the post is actually created we want to call the mailer post mailer dot and now the name of the method post created and now we pass the argument here so I want the current user that is making the post to receive an email that is created daily daddy just created an email so let's pass this argument over there and you have to put dot deliver otherwise you will create the email but really it will not send the email ok so the last thing of course is configuration it's very simple the way you can figures MVP with Gmail and or man chose the SMTP settings are very very easy to implement so you open the application of RB into the config folder and you just have to type things I have a code snippet here you can google it for smtps Gmail settings or action action mailer or a Gmail SMTP rails it will it will find this with no difficulty so these five fields are standard so it there it will always be the same but the username and password of course it want so you have to put here your this case your Gmail account and your password the reason why I commented this environment environment variables here is because if you want to do on a live productions website you would not put your credentials on your code it's not the way you do it we are just using on the development environment so there is no trouble but imagine if you have a kid of a counter and you push this code to github everyone can see your credentials so the way to do it is you actually put you assign environment variables and then you set is on your production host okay so we are all set to begin and test our mailer so let's go to Gmail - G - the Google Chrome sorry let's create a new post so first poll first user post and content for the post let's see what happens I create the post and as you can see here the post was in fact created the one last thing that I completely forgot is under the config folder environments I got the development are be there because that's the environment that we are working on we just need to add a extra configuration right here our lastly so config action mailer not rays delivery but rays I'm sorry dot delivery method must be set of course to SMTP okay so let's um begin and start the rails server again and now let me just go back let me destroy this post let's create a new one so posting content and let's create it okay so it's taking a little while er which is good you can see the post is created and here I already received an email from myself because I'm currently setting up with this gmail account and as you can see post created is a subject the body was this is my first mailer you see already working so now let's do something a little more fun so let's do a mailing that if the user creates a post and now and then a different user comments on that post the post owner will receive the knee mail saying that the user commented on your post so let's put here doesn't matter okay now when I comment this post I want to receive an email here saying that you see I have just created another post and I received the email so it's it's actually working now when I when I comment this post here I want to receive an email if I'm the post owner saying that somebody and I want to know the name commented on my post so let's do it the tool the way to do it is exactly the same way so you go here under the mailers and create a new Miller class so now let's call it comment mailer Darvey let's paste the code from the post mailer to here okay let's just change a little bit comment mailer this is going to be comment created and I want some more data here so I want to pass more arguments over here but first let's go to the controller because it's going to be easier for it to me to plain so let me just open the comments controller here and I want the post user and I can get this because of the relationship between the users and posts I want the content of the comment and I want the current user so the way to do it is if saves this is the same thing after creation we want to call the mailer sorry call the mailer dot now the name of the method which is comment created and now let's pass some arguments arguments the first one is going to be current user you already know what this is the second one is going to be post which I'm referring to this one dot user and the last is of course comment dot content this will get me the content for this comment here now on the mailer I have three arguments so I have cut current user I have the post user and I have the content okay now the mail tube is going to be actually let's put the post user that's what we want we want the post owner to receive the email so as the same as controllers if you want to use some variables on your templates you have to define some instance variables here so the way to do is is current user equals current user the post user equals post user and the content equals content okay so I can use these three variables on my templates so let's do it first let's just change this to comment created I'm not putting a body because I want to render through the template and the from as you know is as I'm using a Gmail settings is and it doesn't matter what what you put here now let's go to our actual template so where is it it's under we have to create it actually because we didn't create it so we have to create a new folder here the same as apposed so comment mailer must be s same the same as the class name now in here we create the temple that the template so a comment created remember must speed the same name as a method so comment on the score created a RB and in here because it has some very simple thing so hi now what I want is Z post user dot name that's what I want and I'm going to put here somebody is post is commented on your post so who is somebody it's the current user the current user ID I miss the add sign here so the current user has commented on your post and in here I'm gonna put the last barrel that I have which is add content you can see it works exactly like the same as a controller and view now I think we are all set to send this the email of course we have to put the dot deliver here otherwise you will not send this email okay so let's try it and let's comment on your post see what happens okay so it took a little which is a good sign it means that is processing the mailer and you can see here I already received one comment created okay I just mistake and just a slightly mistaken here under the view of course I have to put the current user dot okay so let me just delete this one let's put another comment okay so it's processing now let's go check here if i refresh it you see here comment created so hi John and John is the post user here which happens to be the same as the email hi John John has commented on your post another comment is the content of that email so you can see it's all set up it was very easy you have your first email sent from your rails app so now as I've promised you let's send emails through mandrill because it's quite alright to send to through with Gmail account but if you want to send a lot of emails we should use an email service just for that so let's do it go to a mandrill app that come and create an account I have already won my own setup and go here down here on the left to the sections at the settings section it's going to prompt you for the password thank you Wow okay and these settings here this helps SMTP settings are the ones that you should put on your application or Darby and this password here is this API key over there okay so if you go to the dev center of Roku and search for mendel you will get this sending with that SMTP and this is actually the standard configuration you need so just copy this go to your application dot RB and let's just comment it out because we are no longer using Gmail paste this action mailer here and now just gonna delete this domain we don't need this domain here I'm sorry okay in in here you have to put your username and your username is a your actual email from via account from mandrill mandrill app account as you can see here the username is your email and the password is this API key you're free to use it because I will change it afterwards so in here you put your password see and believe it or not this is just what you need to do to set up your SMTP settings so let's um restart the server and see what they have before actually as I told you when you send from the gmail account this will be over over overwritten so and you don't need to worry with that but if you're using montréal this will go in a manner this is you this is what actually going to appear on your email header from so let's put services at domain.com at my domain com okay let's do the same for the mailer here services at mydomain.com now let's actually see what we have so if I create a new post so left post some content here okay so it's processing which is a good thing we just have to see here okay you already receive it now from services that might or may not come that from header that we said but now look this via this server is coming from manual app this is very cool this is my first mailer this is coming from via the actual body because it's the post mailer but now let's let's do a comment on that mailer so let's do some comment I no matter what it is okay now if we go back to our page let's refresh it should be here in a minute now times it takes a little longer okay so you can see here services commented create it so hi John John has companies on your person this is the content okay so you can see this is very easy to use a nice surely you recommend you to go further and check the mandrill as services because they are amazing and I'm just showing you the SMTP settings because you have you can have a complete API integration and they have a lot of analytics to give you the about your email has as you can see here average sends the spam complains average open rate average click rate so this is very very useful and powerful two years so ok so I hope you've enjoyed it and this is all from this struck cast so see you in another one you
Info
Channel: Codeplace
Views: 13,925
Rating: undefined out of 5
Keywords: codecast, actionmailer, action, mailer, ruby on rails, rails, stuk, stuk.io, automated, emails, mails, mandrill, mailchimp, code, how to, gmail
Id: _owXcVLQ20g
Channel Id: undefined
Length: 40min 38sec (2438 seconds)
Published: Thu Jan 08 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.