Sending email with Rails using Sendgrid and Heroku

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up welcome back everyone in this episode we're going to talk about sending email messages from a rails app running on heroku we're continuing to build on our form 4 tracker which tells us about insider trades that are happening on the sec and so yeah let's jump into it and get started so the first thing i'm going to do is go to sendgrid.com you there is technically an add-on a send grid add-on for heroku but it does not work for me i've never been able to get it to work so here i'm just going to go to start for free see i'm not a robot except the terms say get started when you first sign up you have to create a sender identity there's a whole bunch of steps you have to go through to make sure that you're not a spammer who is just going to like you know send a bunch of nasty emails so we're going to send we're going to create a sender create okay so i first got a confirm your email address email so i'm going to click on confirm email address i want to make sure that is confirmed i got my single sender verification so i'm going to say verify single sender okay sender verified so now i can return to single sender verification and i see that it's verified okay so now it says ready to send your first email campaign and follow the the guidelines so what i want to do is go into settings and api keys and i want to create an api key and this is going to be an api key that's used to actually send the email so i'm going to say this is my like form for email like heroku or email sender we're going to create and view this api key and i'm going to go plop this into our settings rails credentials colon edit which allows me to update my credentials for this application and i'm going to add a new send grid key here and i'm just going to call it i guess api key okay so from the sendgrid knowledge center they have this section here where it tells you all the different settings that you need for the smtp settings for your action mailer base so we want to go to config environment and configure our action mailer so we're going to do that now so we'll open up the environment and here we're going to say action mailer base we're just going to paste this in actually and so the username i'm going to change this from the old school ruby to new school so it's going to be an api key the password is going to be our send grid api key which we just stored in rails credentials we're going to say rails.application.credentials.dig send grid api key that's the secret uh api key the domain that we're sending from is actually let's see form fours.herokuapp.com so i'm going to grab that and i'm hoping that doesn't get us banned because it's different than our then our email address so we're gonna add that to our application and push it up to heroku so the difference is that when you use the the heroku add-on for send grid it uses your username and password which is an old style of authentication for working with the send grid with the send grid api and i've found that if i try just adding the add-on directly to sendgrid i get auto-banned so using the using the heroku add-on for send grid does not work or it's not it has not worked in my experience so the only way that i've got an email to work is by passing this api key and so you'll have to go to send grid sign up for an account do all the sender verification process and then create an api key so that you can send email from the dashboard okay the one other thing we need to do is go to our application mailer and update the default from address so that it doesn't come from at example.com instead we want it to come from our the email address for our verified sender so i'm going to say this is our from address and the layout is the same and i think we also want to go to production.rb for our environments and ensure that mailer perform caching we want to actually like perform deliveries so i think in development dot rb perform deliveries we actually have this here and that's set to true um so so configuring the action mailer settings directly from uh production versus from our config.rb or our environment.rb is a little bit different but we could we could have just as easily set all these same settings so action mailer dot username password and we might even need to come back and do that but i think for now um this should work all right so now it's deployed to heroku we can open up the heroku rails console heroku run rail c that'll fire up the rails console and then we can experiment with our form 4 and try to deliver it and we might need to remember how this actually works so form for notification uh right so we're gonna say form four notification with form four deliver and then we pass it i think we pass it like yeah the current the user that we want to send it to so we're going to say we'll say form for notification dot with form 4 form 4 dot first dot deliver to user.first and this should send an email directly it should send both an email and a push a web push notification so here we have this performing noticed um email job that's going out delivery methods email and we expect that that's actually going to work so let's go check our email and see if we got anything and it looks like we it looks like we actually did receive a notification so we've got uh nick was like the very first form for so we received the email here on gmail and that means that we are delivering successfully so now notifications should be set up and running um we can go to our dashboard here in sendgrid and we can we should be able to see if we refresh the page uh it might be delayed a bit but um yeah the the like actual activity here is behind yep so we were able to successfully receive this email address so again the steps were we went to sendgrid and we signed up for a new account on sendgrid we had to verify our sender our sender's email address once we confirmed the sender's email address which we used a custom domain for that in in my case i'm using my cgive dev domain once we were able to confirm that sender we got we created an api key and once we had an api key we were able to use that as our authentication mechanism to the sendgrid api to send emails through through sendgrid the next thing that we did was we opened up environment.rb so config environment.rb and we added this section to the action mailer base smtp settings we also added our api key to our credentials file so that we could send emails using that api key and this is the settings that you'll need when you're when you're setting this up um finally we just deployed to production because our environment variables uh and our credentials were set up heroku was able to deserialize and pass those along without issue all right that's how you send email using heroku and rails and send grid in 2021 hope you enjoyed and we'll see in the next one you
Info
Channel: CJ Avilla
Views: 1,208
Rating: undefined out of 5
Keywords: Sending email with Rails using Sendgrid and Heroku, rails email, send email with rails and heroku, heroku rails email, send email with rails, sendgrid rails, sendgrid heroku, heroku sendgrid rails, How to send email with ruby on rails, ruby on rails, ruby, how to send email with ruby, sending email with sendgrid, sending email with heroku, sending email with rails on heroku, emails with rails
Id: kQyfOLKPrOE
Channel Id: undefined
Length: 8min 43sec (523 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.