How to Send HTML Mail In PowerShell

Send HTML Mail

To Send HTML Mail email in PowerShell requires you to use Microsoft Graph. Send Mail is now depreciated and you shouldn’t be using it anyway. It is nice to add email automation to your scripts but wouldn’t it be better if you could send an nicely formatted email through HTML? Well you can.

It does require some set up. If you look at my previous article on the correct way to send email through PowerShell you can see how to get the App registration set up. Once you are done that, You can use the following script.

Send HTML Mail In PowerShell

Here is an example script that sends an HTML formatted message with a simple table. It also uses CSS formatting:

#Declare Parameters

$clientID = “your ID from App Registration”
$clientSecret = “your client secret from App Registration”
$tenantID = “your tenant ID”

##Run Script##

#Connect to GRAPH API

$MailSender = "who email will be from"

#Connect to GRAPH API
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}


$HTMLBody = @"
<!DOCTYPE html>
<html>
<head>
<style>
* {
'box-sizing: border-box;
}
.row {
margin-left:-5px;
margin-right:-5px;
}
.column {
'float: left;
'width: 20%;
padding: 5px;
}
/* Clearfix (clear floats) */
.row::after {
content: \"\";
clear: both;
display: table;
}
table {
font-family: verdana;
font-size: 10pt;
border-collapse: collapse;
border-spacing: 0;
height: 100px;
width: 315px;
border-right:1px solid black;
padding: 0px;
}
th, td {
text-align: left;
padding: 0px;
}
p {
 margin-top: 2;
 margin-bottom: 2;
}
</style>
<html>
<body>
<p>Here is the Contact Information that you require.</p>
<br>
<table style=\"width:100%\">
  <tr>
    <th>Company Name</th>
    <th>Contact Name</th> 
    <th>Email Address</th>
  </tr>
  <tr>
    <td>John Doe</td>
    <td>Acme Co.</td>
    <td>[email protected]</td>
  </tr>
  <tr>
    <td>Eve Jackson</td>
    <td>Jackson Inc.</td>
    <td>[email protected]</td>
  </tr>
  <tr>
    <td>Bruce Wayne</td>
    <td>Wayne Enterprises</td>
    <td>[email protected]</td>
  </tr>
</table>
<br>
<p>Sincerely,</P>
<p>Sales</p>
</body>
</html>
"@

$msg = $HTMLBody

#Send Mail    
$URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
$BodyJsonsend = @"
                    {
                        "message": {
                          "subject": "Customer Information",
                          "body": {
                            "contentType": "HTML",
                            "content": "$msg"

                          },
                          "toRecipients": [
                            {
                              "emailAddress": {
                                "address": "email address to"
                              }
                            }
                          ]
                        },
                        "saveToSentItems": "false"
                      }
"@

Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

Escape the Quote Character When You Need To Send HTML Mail

It is very important to note that the script needs to escape an quote (“) symbol it sees. That character is the back slash (\). You will need it before every quotation mark in the script.

You will get a 400 Error (Bad Request) if you don’t. I was working on a heavily formatted email to send in PowerShell and I had to go through a lot of lines of code to ensure all of the quotation marks were escaped.

Now that you have a good base, whenever you need to send a mail for whatever reason in PowerShell you have an excellent “go to” HTML template for whatever reason you need.

If you find this post useful please consider buying me a coffee. It will keep me caffeinated so I can provide more Quick IT Tips!

Happy IT’ing

Dan

Avatar photo

I am an IT professional with over twenty years experience in the field. I have supported thousands of users over the years. The organizations I have worked for range in size from one person to hundreds of people. I have performed support from Help Desk, Network / Cloud Administration, Network Support, Application Support, Implementation and Security.

Pin It on Pinterest