I live in the South, specifically in God’s country AKA TEXAS BABAY! There’s plenty of amazing things about Texas and definitely some reasons to loathe being from Houston, but it’s hard to knock our food. I mean, is there really even a debate between Whataburger vs In-N-Out? I’ll save you a google search (or bing or asking Jeeves or however you live your life): the answer is no. Long live the Whataburger! Tex-Mex is another blessed invention from the south and it’s phenomenal. If you’re from here, Tex-Mex is most likely a part of the lowest level of Maslow’s Hierarchy of Needs for your survival and wellbeing. You’ve replaced all bland, tasteless food in your life with a kick in the mouth of spicy seasoning and flavor for days. The only issue is once you’ve incorporated all of this extra flavor into your diet, it will inadvertently spill over into your tech life. No longer can you bear to send emails from PowerShell that are bland and tasteless. Your tastebuds now crave rich emails with cavernous colors and all of the flavorful formatting that HTML has to offer. Well, let’s satisfy your hunger with a Snickers by converting our objects into tables, adding some CSS styling, and building a function to give us a spicy status bar that even Outlook will like. Let’s spice up those HTML Emails with PowerShell!

Wait, before we even get out all of our ingredients, I need you to do something for me. Stop what you’re doing and tweet Chuy’s for me and tell them to bring back the Elvis Green Chile Fried Chicken please.

I’ll wait.

Did you do it???

You’re the best! And trust me, if you never had Elvis Green Chile Friend Chicken from Chuy’s while they were serving them…your life has a hole in it you didn’t even know about. Thought you were dropped on your head as a child? No ma’am, this is the reason you are the way you are today.

\\\You’ve Got Mail

Okay, now let’s take a look at sending a basic email in PowerShell using Send-MailMessage. Yeah, yeah, I know it’s technically being phased out but I’ll keep using it until they rip it from my cold dead hands! The basics of sending an email (assuming you have an SMTP relay already configured and working) is:
To, From, Subject, Body, SmtpServer, Port.

There are some others depending on your needs, but let’s just go over sending a simple email if you haven’t done that before. An example looks like this:

Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body "It's dinner time amico!" -SmtpServer server1.food.edu -Port 25

So your port might be different if you’re using SSL or you might have to authenticate to your relay depending on if it’s internal and what security measures you have in place, but you get the idea. And yes Karen, I know Send-MailMessage is obsolete but they can pry it from my cold, dead hands!

Let’s take a look at this super-wordy and well-crafted email:
Underwhelming Email
Well that was a lot of a letdown. Where is the spice!? Where is the seasoning!? Where is the passion that was stirred into every line of code!? I see nothing but bland cafeteria food!

Okay, </rant>. Let’s get back in the kitchen. These types of emails work for those of us with no personality and no friends, but the rest of the world is accustomed to information being a little easier on the eye. Especially if we wanted to send some actual data in this email…

\\\Refried Beans

What if we wanted to get the MAC Address of our network interfaces and include the object in the body of the email?
We could try:

$Adapters = Get-NetAdapter | select Name, MacAddress, MediaConnectionState
$Body = "It's dinner time amico! And oddly, here is the network adapter information you wanted:" + $Adapters
Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body $Body -SmtpServer server1.food.edu -Port 25
Here goes nothing! Send away!
Underwhelming Email 2
Mama mia! Thisa literally makesa mi eyeballsa burn! Make it stahp! Notice our table never made it into the Body String. Even if we used the -BodyAsHTML switch, this thing is still not looking right. Here’s actual footage of us sending this email to some helpless recipient:

\\\Two Tbps of Conversion

Thankfully, we’re given a cool tool to convert our $Adapters object to HTML with the ConvertTo-HTML cmdlet:

$Adapters | ConvertTo-HTML
ConvertTo-HTML
Looking at the output, we can see that it adds a header and then converts our object into an HTML table with <tr> (Table Rows) and <td> (Table Data) tags. If we only want to send the object with no other content in the body, this works. But if we want the table without the <html> and <body> tags so we can add it ourselves, use the -Fragment switch:
$Adapters | ConvertTo-HTML -Fragment
By using the -Fragment switch, we can add other elements into the body of our email and utilize HTML to take it up a notch. Let’s add in some more text with a count of the adapters and make the number bold. Maybe include a link to a great recipe that pairs really nicely with network adapters. We want to make sure to wrap our body (not physically, of course) in an HTML tag.
$Body = ''
$Body += "It's dinner time amico! And oddly, you have <b>$($Adapters.count) network adapters.</b> Here is the info:<br><br>"
$Body += $Adapters_HTML
$Body += "<br>Oh yeah! I almost forgot, here's a great recipe to make your own <a href=`"https://www.modernhoney.com/chuys-creamy-jalapeno-cilantro-dip-dressing`">Chuy's Creamy Jalapeno Sauce!</a>"
If you haven’t read through my posts about manipulating Strings (Part I, Part II) and you’re lost on all of the above, go check it out! In the above block of code, we utilized several HTML elements: <b> (bold/strong), <a> for a link, <br> for a line break. Let’s take a whiff at this and smell what we have cookin’:
Slightly Less Underwhelming Email

It’s definitely starting to get saucy. We’re well on our way to crafting a delcious email that’s sure to please the palate. In Part II, we’ll take what we’ve been building and spice it up with a graphic, a footer, and some sections to better visualize our masterpiece. Stay saucy my friends!

OTHER POSTS YOU WANT TO READ

Index Scripts for Windows Search

So you just finished writing some code and you go to save your file. You summarize all of the important aspects this section of code contains into a nice, easy-to-read file name that your future self will immediately recognize. Fast forward to the future where you...

Array vs ArrayList (PowerShell)

For some tasks in life, being precise is a necessity. But most of us get away with rounding, paraphrasing, and hitting in the general vicinity most of the time. Depending on your personality, you may be one who strives for perfection and strains on every miniscule...

Spice Up HTML Emails with PowerShell – Part III

So far in this series we've fumbled our way around the kitchen and tried to wing it when sending HTML emails with PowerShell. It was clunky to say the least. We then went through our spice rack and built an HTML template, highlighting the nuances of each spicy element...

Spice up HTML Emails with PowerShell – Part II

In Part I of our scrumptious concoction we put our script into the oven to let it bake. But we forgot to add our secret sauce that's sure to leave our recipients drooling, which is clearly our goal. In this post we'll continue to spice up HTML emails with PowerShell...

Disable O365 Apps in License (PowerShell)

If it's free, it's better. This is basically my mantra for life and a really easy one to follow at that. You can apply it to all sorts of things: ice cream, candy, food. Okay, maybe just free food tastes better when you know you didn't have to pay for it. Especially...
%d