How to Send Email with PHP and Twilio SendGrid

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
twilio sengrid makes it super simple to send your first email in five minutes or less in this video we're going to show you how it works in PHP but if you use any of the programming languages appearing on screen right now there's a helper library to help you out let's take a look at how it works your app makes a post request to slash mail slash send over in sendgrid and sendgrid sends an email to your user's inbox we'll start by creating an API key head into sendgrid and go under settings to API Keys hit the create API key button and give it a name go to restricted access and scroll down to where it says mail send and expand that and click on where it says mail send scroll down and hit the create and view button copy the key that appears here and store it in an environment variable inside of the terminal I'm going to create a directory for our project called send email PHP and change directories into it we're going to use composer to install the sendgrid helper Library so I'll create a composer.json file and I'll also create our send email.php file and then I'll open this up in a text editor I'm using visual studio code but you can use whatever you want let's open that composer.json file and specify our dependencies so inside of here we'll have a require property and that's going to be sendgrid sendgrid and we'll use version 7. give this file a save and open up the integrated terminal and run composer install to install the sendgrid helper Library once that's done we'll open up the send email PHP file and start writing our code to send the email we'll require the vendor slash autoload.php file which will pull in the sendgrid helper library that we just installed with composer then we'll create a email object calling new sendgrid slash mail slash mail then we'll use that email object to set the from email address now this is an email address that I've authenticated inside of sendgrid details on that down in the description but that's going to be me at brentschooly.com and set my name to Brent schooly next we'll set the subject for the email and that's the subject line and it's going to say sending with sendgrid is fun then we'll set the to email address and this is another email just this could be anything that you want to send to this one's going to be Brent from twilio gmail.com and it's also me so I'll set my name to Brent schooly next we'll add some content to our email the first set of content will be for plain text HTML readers so we'll set it to text plane and we'll say and easy to do anywhere especially with PHP then we'll set some content for if it's an HTML email client so we'll set it to text HTML and we'll wrap that same text inside of a strong tag next we'll create a send grid object that's going to be new send Grid in the parameter it takes here is my sendgrid API key which I have stored in environment variable called sendgrid underscore API underscore key now inside of a try catch block it is the internet after all we're going to call sendgrid send and pass it the email that we created and then we'll store the response in a response object and then we'll log out some details about that response to the command line just to see what happens when we send the email so that we know that it worked correctly down in the catch block just in case anything went wrong we'll catch the exception that gets thrown by the helper library and we'll just log that out to the command line as well give this file a save head to your terminal and run PHP send email.php you'll get the response right away you can see here it's a 202 accepted from the API which means our email will get created if I head over to Gmail at this point we'll see that my email is indeed in my inbox and it's wrapped in that strong tag since we're viewing it in HTML and it came from and went to the right email addresses that's all there is to it
Info
Channel: Twilio
Views: 12,518
Rating: undefined out of 5
Keywords: twilio, sendgrid, send email api, twilio sendgrid, twilio node, twilio email, sendgrid send email, sendgrid email, sendgrid tutorial, how to, sendgrid api, sendgrid api tutorial, brent-schooley
Id: -lFMDQ5tziE
Channel Id: undefined
Length: 3min 44sec (224 seconds)
Published: Tue Apr 09 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.