let's send emails with PowerShell and SendGrid [Music]
hello everyone I'm Travis and this is Ciraltos in the olden days of it when we needed a script
to send an email we'd relay off an email server that had Port 25 open typically one we controlled
but email has moved to the cloud and open email relays aren't really a good idea in this video
we look at a more reliable option using SendGrid with PowerShell to send email from our scripts and
automation jobs before that please like subscribe and share with a friend go ahead I'll wait you can
also become a member for Early Access to videos ad free while private and check out my courses on
Azure virtual desktop and hybrid identities with Windows AD and Azure AD on udemy.com the links
are below hey wait I also launched a newsletter the link to sign up is below your support is
appreciated back to it SendGrid is a SaaS email service offered by Twilio available from the Azure
Marketplace there are multiple tiers available based on the amount of emails sent the lowest is
a free tier that allows up to 100 emails a day I can afford that one that's what we'll use in
this video SendGrid uses an API not Port 25 to send messages this is more secure than an open
email relay we can leverage SendGrid for any automation or application that needs to send an
email as long as the programmer script can access the internet it can send email coming up we'll
start by creating a free SendGrid account then we'll use a PowerShell function that allows us to
specify the two subject and body of the email and send the message through SendGrid both with plain
text and HTML the idea with this example is the function can be added to other automation tasks
so they can send emails for example maybe you want to know when a job is finished or get a message if
there's an error the PowerShell code used for the example coming up is available on GitHub the link
is below check out the newsletter sign up while you're down there let's move to the Azure portal
to get started here we are at the Azure portal let's search for Twilio SendGrid there it is
select your subscription and Resource Group I'll create a new Resource Group for this example
SendGrid RG select a region Central us for this example give it a name send grid demo for this
example we'll use the free 100 option if you need to send more email go to change plan and there we
can see our options let's close that we'll leave reoccurring billing as is and go to next to tags
add tags as needed and go to review and subscribe this is a third party SAS service and we have
to provide some contact information to activate let's scroll down if that all looks good subscribe
that gets the process started the deployment is complete but we're not done yet we need
to configure our account on the send grid site go to configure account now
we'll sign in
we need to approve Twilio's send grid permissions to update data in Azure click yes at the prompt
we need to provide some more information to SendGrid this is a free account but nothing's
really free is it fill out the form to get started once it's up to date click get started
this takes us to the configuration page we can create a single sender or authenticated
domain we'll create a single sender for this example click create a single sender
here we need to add some additional sender information this is required to comply with
anti-spam laws fill out the information and click create you do need to use a real reply
address there is a verification process we'll click create and we'll close the sender has
been created message one thing to notice right away is under verified we've got a red X meaning
it's not verified yet let me check the email account of the address used for verification
I got the verification message to that address let's open up the verification Link in another tab
and now the sender is verified let's close that tab we'll come back to the
original Tab and refresh and here it shows the sender is verified now now that we have
our information verified we can create the API key let's go to API keys under settings
we'll create a new API key
we'll give it a name we'll call this API key send grid demo and we can use restricted access
we can do a lot more with the API than just send emails but for this example we'll just use the
restricted access under mail send will give it the ability to send email let's go to create and view
and now we have the API key we're going to copy by clicking on it save this key you won't be
able to access the key once we leave this page if you lose the key a new API key will have to
be created we'll save it and click done let's move on to the function next we'll need the API
key in the sender email address to complete the next steps let's go into VS Code here we are in
VS Code let's take a look at the function I included some information about the function
including four parameters we need a destination address the subject the content type content type
is optional and set to text plain by default finally we have the content body this is the
actual message in text or HTML format let's scroll down to update the SendGrid API key
add the API key from SendGrid and add the from email address and this has to be that verified
address if you add a fake email address like DoNotReply you'll get an authentication error
and the email won't send next the function creates the header with the API authentication information
then it assembles the body with the destination address subject content type and body we passed in
then it converts the body to JSON and a try catch block it will air with a message if the
conversion fails then it uses invoke rest method passing in the send grid send mail URI with the
method post along with a header in the body for the message this is also in a try catch block
let's select and run the entire function to add it to memory
be sure you updated the API key and the from email address we'll hit f8 or you can do right click
run selection and again that just adds it to memory so now if we use get help
and call the send send grid email function
we get the help information for that function that just means
it's loaded into memory and now it's available to use let's scroll down
next we'll send our first email this one will be text based add
the destination email address along with the subject and the contents
this example uses splatting to pass in parameters the formatting is easier to read
this way splatting is a hash table of parameters passed into the function the last line calls the
function Send-SendGridEmail then passes in the parameters we added to the Splat hash
table let's highlight and run this after a couple seconds you should get an email in the inbox sent
from the function and send grid let's scroll down to the next example I'm not a web developer and
this code proves it I created a multi-line string variable and assigned it to the HTML body I have a
table with a header for some reason I centered all this remember this is just meant as an example
updated with whatever you see fit let's make the window a little bigger next is our destination
email address and subject the content type is text HTML for this example the content type defaults to
text plane if we don't update it after that we pass in the content body that's the HTML body
we added above let me update the email address next we'll highlight this block of code and run
there are no errors after a few seconds we should see the message in our inbox
notice this time it's formatted with HTML
that is how to use PowerShell and send grid to send emails this video scratches the surface on
what we can do with SendGrid I hope it helps you better understand the basic task of using SendGrid
and PowerShell to send email please don't forget to like And subscribe and thanks for watching