in this video I send emails with PowerShell and SendGrid
hello my name is
Travis and this is Ciraltos in this video I create a reusable powershell function
to send emails through SendGrid before that please take a second to
subscribe and click the bell icon for new content and thanks to those who
subscribe let's move on I started this project with a simple goal in mind I
wanted to send email through PowerShell without using an SMTP relay some
organizations block non-standard ports like port 25 used to send email I
originally planned to use an azure function as kind of an smtp relay
sending a web hook to the function and then the function sends the email once I
started using SendGrid I realized that was over complicating
things I could simply send an email using the send grid API over standard
web ports SendGrid is a third party product integrated into Azure used for
sending emails it's able to send a large volume of email such as what would be
required for an email marketing campaign but it can send an individual email as
well the free tier allows for up to twenty-five thousand emails a month way
more than enough for what I need I set up a similar system a while ago using an
azure automation runbook to send an email from a web hook that required an
O365 account with an Exchange Online license this was used to send
notifications if the organization's SMTP relay server was unavailable using send
grid instead doesn't require a run book or license I intend to use this function
with another project I'm working on to deliver reports but it can be integrated
into any script that needs to send an email here's what I cover in the demo
I'm going to set up a send grid account and Azure create an API key walk through
the PowerShell function and then test sending emails Here I am at the portal
the first thing I'm going to do is create a resource and I'm going to
search for send grid and create a send grid it's gonna ask me for some
information I need to enter in a name I'll just add sendgrid 01 in case I need
more than one and a password this password will be
used to authenticate to the SMTP server if you do send over port 25
you need a username and password for that I don't plan to use this I'll use
the API key instead I'll create a new resource group called SendGrid RG and
here's the pricing tiers so you can see the free allows for some reporting
integrations advanced delivery and up to 25,000 emails a month there are
additional service plans if you need them but I will select the free tier you
need a promotion code to send over five million emails a month that does not
apply to me here it's asking for some contact information SendGrid will send a
confirmation email if you don't confirm that I think you're limited to like a
hundred emails a day so it's just something to keep in mind you will want
to confirm that email and under legal terms it's gonna ask you to review and
it does ask for a phone number I did not get a phone confirmation with this but
it does ask for a phone number and I'll create it and this will take a couple
minutes to deploy so I'll come back once it's done here I'm back at the dashboard
I'm just going to search for SendGrid there it is I'll click here go into my
SendGrid account here's sendgrid there's a couple things
I'll point out you can change the password that we set up on the
deployment here if you need to there's other information over here under
settings properties configuration contact information here you can get
your SMTP server if you do want to send it by SMTP and the username but you can
get that from the actual service itself and if you want to manage of the service
if you need an API key like we do I'm gonna click that button here we're into
the service now this is the SendGrid website not Azure anymore and if we want
to see account details here's that username again that's the
username I would use to sign into the SMTP service but I'm gonna go under
account details and API keys I need to create an API key to send using the rest
method so let's create an API key and this is pretty easy I'm gonna do full
access I'll give it a name just call it PowerShell create view the only trick
to this is once we exit the screen you will not be able to get this key anymore
so I'm gonna copy it I'm just gonna paste it in here so I don't lose it so
once they click done so now I have an API key for PowerShell to use to send
email here i am and VSCode let me get this out of the way as you can see I
have a function and that function is gonna accept some input parameters most
of them are required I've got the destination email address that's who the
email is going to the from address that's who of the email is coming from
the subject the content type so this is going to be either text plain or text
HTML you can see that's not mandatory and if you don't add anything it's going
to default to text slash plain and then finally we have the content body that's
the actual body of the email I have some comments and help file in here so this
just explains what each parameter does and what you need it for after that I've
got the API key now I'm gonna have to enter that API key the one you see there
is different I'll just copy this and paste it in ideally you wouldn't want to
keep this API key in your code I'm gonna use this in either Azure functions or
Azure automation so I'll use the key vault or an environmental variable to
hold that key instead of adding it to the code for this demo I'm just gonna
leave it in there here's the headers so this is building out the headers it's
going to have the bearer token and that's the API key and the content type
and then down here is the body within the body we have the personalizations so
this is the To and that's the email address the from email address this is
just building out a JSON body to pass in to the API and one thing I will point
out if I come back up here to this I put an API reference in the description if
you need to go back and look at that let's just hop over and take a look at
that quick if I come over to mail send gives a lot of information on how to
send the email and here is the information that I'm adding so you can
see there's two as an array object I have an email address in but I could
also add a name so if I wanted to add a display name that's different from the
email address and there's a whole bunch of options in here so you can add to
this as you need to for example from we can add the email address as well as a
name there's BCC options and Carbon Copy
options as well so if you need any further information or need to add to
this this is where you can find that information
so all this function does is then builds out the header builds out the body then
I've got a try-catch block for the conversion of the body to JSON that will
throw an error and break if it doesn't work and then I've got a second one that
runs the invoked-restmethod and it just post the header in the body if that
doesn't work it will write an error message as well one thing I noticed is
this won't return a code like a 200 code if it's good but it will return a code
if it fails so if it does have a problem you'll get that message in this write
error otherwise no news is good news I guess let's just put that in memory the
first example I have here is going to call the function I'm using splat if
you're not familiar with that that's just a way of adding in the parameters
in a list format I could do it the other way where I add the parameters like
destination
and so on I'm not doing that I kind of like the layout of the splat instead
it's easy to read and if I reuse this code in other scripts just makes it a
little bit easier to change so all I'm doing is building out that array I'll
add that to memory as well and then if I run the command so there's no response
give it a second to work its way through there it is and here's the message so
that worked down here I did something a little bit differently I'm creating an
HTML body I'm gonna add that to memory and here you can see unlike the other
one where I left the content type out I switch the content type here to text
HTML I'm just gonna run this whole thing so now it's the content body is gonna be
the HTML body and the point with this is that you can send both a text based
email as well as a HTML based email here it is so here you can see it centered
the content so that worked as expected that's it you can use this code to send
an email over standard web ports through SendGrid this works for both text and
HTML based email I'll add a github link to the comments below where you can find
this code that's it for the demo I can't promise that this will eliminate the
need for an SMTP relay in your organization after all there always be a
printer scanner a postage machine that needs an open relay this function will
make sending email from a PowerShell script a lot easier though don't forget
to subscribe and thanks for watching