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 and how to use it. In this post, we’re going to tie it all together and create a masterpiece of an email that even Gordon Ramsay would want to open. If you haven’t checked out Part I and II yet, it would probably be helpful to start there first. Specifically we’ll be using our template we created in Part II and the process in PowerShell from Part I. Let’s start cookin’ good lookin’!

\\\LET IT THAW

I’m not sure how you saved the template in Step 2, but hopefully it was just about any format other than stone tablet. We have options here: you can save it as an HTML file or a TXT file or even a string in PowerShell, but that last one might get a little ridiculous. For this example, let’s save it as a TXT file and then we’ll import it (output as a string) and let it thaw out in PowerShell.

$PathToHTMLTemplate = C:\\Kitchen\\Fridge\\EmailTemplate.txt
$EmailContent = Get-Content $PathToHTMLTemplate | Out-String

This imports our template as a string and stores it in our variable $EmailContent. For more info on strings and things, check out String Theory. If you recall from our template, we already included some unique text that we can easily replace with content in PowerShell. These place holders in our template are:

  • $Section1Head
  • $Section1Body
  • $Section2Head
  • $Section2Body
  • $YourTeam

So let’s say we want to extend our original email from Part I by adding some spice. Let’s get our network adapter info, convert the table to HTML, and then define some of our variables:

$Adapters_HTML = Get-NetAdapter | select Name, MacAddress, MediaConnectionState | ConvertTo-HTML -Fragment
$Section1Head = 'Update'
$Section1Body = "It's dinner time amico! We've also been trying to reach you about your extended vehicle warranty. This email is extremely coherent and I'm not one to pettinare le bambole."
$Section2Head = 'Network Info'
$Section2Body = 'Below is the network adapter info you asked for:' + $Adapters_HTML
$YourTeam = 'Team Saucy'

\\\Ingredient Swap

Now that we’ve defined our content that we want to insert into our HTML template, we just need to use the Replace() method in our $EmailContent variable. We’re replacing the literal characters of $Section1Head with the content we’ve stored in our variable $Section1Head. Then we just repeat it for the rest of our variables:

$EmailContent = $EmailContent.Replace('$Section1Head',$Section1Head)
$EmailContent = $EmailContent.Replace('$Section1Body',$Section1Body)
$EmailContent = $EmailContent.Replace('$Section2Head',$Section2Head)
$EmailContent = $EmailContent.Replace('$Section2Body',$Section2Body)
$EmailContent = $EmailContent.Replace('$YourTeam',$YourTeam)
Man, this is smellin’ good! Let’s call in the fam to taste test and make sure they’re picking up what we’re putting down. Let’s use Send-MailMessage to provide a sample of what we have so far:
Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body $EmailContent -SmtpServer server1.food.edu -Port 25
Spicy HTML Email 3 - First Draft

\\\Needs More Salt

Like everything in the south, it’s absolutely perfect…but we’re going to add more salt anyway. I’m looking at our table and I’m just not digging it. It’s too bland, it’s lifeless, and when you poke it with a stick it doesn’t even move. Let’s shake that table like a salt shaker and add some style. To start off with, we’ll add some CSS to our actual Email Template txt file. Open it up in your favorite text editor (much love for the OG Notepad) and right below the <h1> line in the <style> element of our <head> section, let’s add the following code:

table#t01{
	border-collapse: collapse;
	border-width: 1px;
	border-color: black;
	border-style: solid;
}
table#t01 th{
	color: white;
	background-color: #04afd3;
	padding: 3px;
	border-width: 1px;
	border-color: black;
	border-style: solid;
}
table#t01 td{
	border-width: 1px;
	border-color: black;
	border-style: solid;
}

In a nutshell, we’re creating a new style for a table with the ID of “t01” and then within that table we’re styling the table headers and the table data elements. We want the headers of our columns to stand out a little bit so we give them white text and a different background color. We want all of the cells to have a thin, black border. This is great and all, but we have to give our $Adapters_HTML table an id of “t01” to be styled like we want. We can do that with—you guessed it—a spatula. No, wait, it’s actually the Replace() Method again. It looks like this:

$Adapters_HTML = $Adapters_HTML.Replace('<table>','<table id="t01">')

\\\Serving Our Dish

So Here is our final HTML Template, and our final PS code to send the email. Just in case you haven’t had your second cup of coffee today, you want to replace all of my legitimate and absolutely non-fictitious email server settings with your own, equally-legitimate settings.

$PathToHTMLTemplate = C:\\Kitchen\\Fridge\\EmailTemplate.txt
$EmailContent = Get-Content $PathToHTMLTemplate | Out-String
$Adapters_HTML = Get-NetAdapter | select Name, MacAddress, MediaConnectionState | ConvertTo-HTML -Fragment
$Adapters_HTML = $Adapters_HTML.Replace('<table>','<table id="t01">')
$Section1Head = 'Update'
$Section1Body = "It's dinner time amico! We've also been trying to reach you about your extended vehicle warranty. This email is extremely coherent and I'm not one to pettinare le bambole."
$Section2Head = 'Network Info'
$Section2Body = 'Below is the network adapter info you asked for:' + $Adapters_HTML
$YourTeam = 'Team Saucy'
$EmailContent = $EmailContent.Replace('$Section1Head',$Section1Head)
$EmailContent = $EmailContent.Replace('$Section1Body',$Section1Body)
$EmailContent = $EmailContent.Replace('$Section2Head',$Section2Head)
$EmailContent = $EmailContent.Replace('$Section2Body',$Section2Body)
$EmailContent = $EmailContent.Replace('$YourTeam',$YourTeam)
Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body $EmailContent -SmtpServer server1.food.edu -Port 25
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width,initial-scale=1">
     <meta name="x-apple-disable-message-reformatting">
     <title></title>
     <!--[if mso]>
     <noscript>
          <xml>
               <o:OfficeDocumentSettings>
                    <o:PixelsPerInch>96</o:PixelsPerInch>
               </o:OfficeDocumentSettings>
          </xml>
     </noscript>
     <![endif]-->
     <style>
          table, td, div, h1, p {font-family: Roboto, sans-serif;}
          h1 {color: #04afd3}
          table#t01{
               border-collapse: collapse;
               border-width: 1px;
               border-color: black;
               border-style: solid;
          }
          table#t01 th{
               color: white;
               background-color: #04afd3;
               padding: 3px;
               border-width: 1px;
               border-color: black;
               border-style: solid;
          }
          table#t01 td{
               border-width: 1px;
               border-color: black;
               border-style: solid;
          }
     </style>
</head>
<body style="margin:0;padding:0;">
     <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0;background:#ffffff;">
          <tr>
               <td align="center" style="padding:0;">
                    <table role="presentation" style="width:602px;border-collapse:collapse;border-spacing:0;text-align:left;">
                         <tr>
                              <td align="center" style="padding:30px 0;background:#04afd3;">
                                   <img src="https://pngimg.com/uploads/server/server_PNG59.png" alt="" width="300" style="height:auto;display:block;" />
                              </td>
                         </tr>
                         <tr>
                              <td style="padding:30px 10px;background:#FFFFFF;">
                                   <h1>$Section1Head</h1>
                                   <p>$Section1Body</p>
                              </td>
                         </tr>
                         <tr>
                              <td style="padding:30px 10px;background:#FBFBFB;">
                                   <h1>$Section2Head</h1>
                                   <p>$Section2Body</p>
                              </td>
                         </tr>
                         <tr>
                              <td align="center" style="padding:30px 10px;background:#04afd3;">
                                   <p style="color:#FFFFFF">Magic by $YourTeam</p>
                              </td>
                         </tr>
                    </table>
               </td>
          </tr>
     </table>
</body>
</html>

Let’s put on our mittens and pull our creation out of the oven and stare at it for awhile!

Spicy HTML Email 3 - Final Visual

\\\The Final, FINAL Ingredient

If you started crying at just how spicy this email is, you’re not alone. We’re technically done here…but there’s one last finishing touch that will literally turn our dish into a work of art and earn us the title of Iron [Email] Chef…and that’s coloring every other row in our table. Look for an extra bonus post where we’ll build a function to easily take a table and convert it to HTML with all of our styling needs baked in. Ciao ciao!

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 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...

Spice Up HTML Emails with PowerShell – Part I

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...

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 bloggers like this: