Sending Custom HTML Emails via PowerShell

Hi Everyone,

I hope COVID restrictions are starting to ease for you all. Still, don’t want to let your guard down too much, ey! Better safe than sorry.

In this post, I’m showing a simple way to send HTML emails from PowerShell. This includes sending emails that contain local images instead of hosting them on a separate website. I might have done this in the past, but I want to just throw this out there again. Lets hop in!

First of all, HTML shows different on EVERY SINGLE DEVICE. Outlook is particularly bad, completely ignoring the head that might contain styling and CSS. So the only way around this is to put the styling on every single element in the HTML body.

Here is the HTML I used, we will discuss what is happening below:

<!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: Arial, sans-serif;
            }
        </style>
    </head>
<body style="margin:0;padding:0;">
        <table role="presentation" style="width:100%;border-collapse:collapse;border:0;border-spacing:0">
            <tr>
                <td align="center" style="padding:0;">
                    <table role="presentation"
                        style="width:800px;border-collapse:collapse;border-spacing:0;text-align:left;">
                        <!--Logo-->
                        <tr>
                            <td align="center" style="padding:40px 0 10px 0">
                                <img src="cid:logo.png" alt="" width="350" style="height:auto;display:block;" />
                            </td>
                        </tr>
                        <tr>
                            <td style="padding:10px 10px 10px 10px;">
                                <table role="presentation"
                                    style="width:100%;border-collapse:collapse;border:0;border-spacing:0;">
                                    <!--First chunk-->
                                    <tr>
                                        <td style="padding:0 0 20px 0">
                                            <h1
                                                style="font-size:38px;margin:0 0 20px 0;font-family:Arial,sans-serif;text-align:center;">
                                                Information Technology
                                            </h1>
                                            <p
                                                style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">
                                                Dear $firstname,
                                            </p>
                                            <h2 style="font-size:22px;margin:0 0 10px 0;font-family:Arial,sans-serif">
                                                Welcome!
                                            </h2>
                                            <p
                                                style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">
                                                Generic text can go here
                                            </p>
                                            <h2 style="font-size:22px;margin:0 0 10px 0;font-family:Arial,sans-serif">
                                                Your Login Information
                                            </h2>
                                            <p>
                                                style="margin:0 0 12px 0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">
                                                More generic text
                                           </p>
                                            <p>
                                                style="margin:0;font-size:16px;line-height:24px;font-family:Arial,sans-serif;">
                                            </p>
                                        </td>
                                    </tr>
								</table
							</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        </td>
        </tr>
        <!--Footer-->
        <tr>
            <td style="padding:30px;background:#272a36;">
                <table role="presentation"
                    style="width:100%;border-collapse:collapse;border:0;border-spacing:0;font-size:9px;font-family:Arial,sans-serif;">
                    <tr>
                        <td style="padding:0;width:50%;" align="center">
                            <p style="margin:0;font-size:18px;line-height:16px;font-family:Arial,sans-serif;color:#ffffff;">
                                For an additional support, or if you have any questions, please
                                send an email to <a href="mailto:help@example.com"
                                    style="color:#d6d6d6;text-decoration:underline;">help@example.com</a>
                            </p>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
    </body>
</html>
  • Setting the Office product details and pixel density information
  • The logo.png is loaded using the cid: prefix which looks at the loaded images, you can see this below
  • The rest is just generic HTML with inline styling

Next we need to start creating the PowerShell email object.

#Define a valid SMTP server to send your emails through
$smtpServer = 'SERVER HERE' #e.g smtp.local.com
#Define a new SMTP object using the server defined above
$smtpObject = New-Object Net.Mail.SmtpClient($smtpServer)

#Create a new mail message object 
$msg = New-Object Net.Mail.MailMessage
$msg.From = 'DoNotReply@example.com'
$msg.ReplyTo = 'bccemail@example.com'
$msg.BCC.Add('bccemail@example.com')
$msg.To.Add('recipient@example.com')
$msg.subject = 'Example Email Subject'
$msg.IsBodyHtml = $True

$msg.Body = 'HTML FROM ABOVE - This can be a separate variable or right here'

#Provide a path to the photos for the email
$scriptPath = 'C:\example'

#Create a new mail attachment as an image
$logo = New-Object System.Net.Mail.Attachment -ArgumentList "$scriptPath\logo.png"
$logo.ContentDisposition.Inline = $True
$logo.ContentDisposition.DispositionType = "Inline"
$logo.ContentType.MediaType = "image/png"
$logo.ContentId = 'logo.png'

#Add the image attachment to the email, this allows the HTML to use the cid: prefix
$msg.Attachments.Add($logo)

#Try to send the email
try{
    $smptObject.Send($msg)
}catch{
    Write-Host 'Failed to send the email: ' -ForegroundColor Red -NoNewLine
    Write-Host $Error[0] -ForegroundColor Red
}

#Dispose of the image attachments and the email object to avoid memory leaks
#logo.Dispose()
$msg.Dispose()

Hopefully this shows just how easy it is to create and send HTML emails using PowerShell, including images send directly from the script instead of hosting them and suppling a web page URL.

If you have any questions etc, feel free to comment and I’ll help however I can.

Enjoy! 🙂

Hide Tags from WordPress Blog Posts

So you want to hide tags from your WordPress posts? Easy…

To remove the tags line, you need to find out how to reference it and change its style using CSS. This might sound alien and weird but trust me, you can do it. Or just post a comment if you’re having difficulty 🙂

  1. Open your website, open a post and open the dev tools in your browser. This is usually done by pressing F12
  2. Look at the screenshot below and follow the step:
    1. Press where arrow #1 hints to
    2. Hover over the “Tagged” section as arrow #2 shows
    3. Finally, see the name or ID that is given to that object (Mine is tags-links)

 

3. Now that we have that, we can add some CSS to our site to make this not appear.

4. Go into Appearance -> Customize -> Additional CSS

5. Here we will add the following lines (Note that you should replace this with whatever you found in section 2.3 above):

.tags-links{
    display:none;
}

6. You can see this below:

7. Now when we refresh the page, you should see that your tags have gone completely from all your posts!

Enjoy!

MAC Address Lookup API Using PowerShell

Meet macvendors.com! I’ve used this website quite a bit in the past and recently saw that they have an API. This means I can query MAC address vendors using PowerShell instead of loading the site every time.

So I quickly threw together a small test to see if this would work using Invoke-WebRequest. You can see this below:

$mac_example = "3C-07-71-75-BC-32"
Invoke-WebRequest -Uri "https://api.macvendors.com/$mac_example"

This returns the following information:

StatusCode        : 200
StatusDescription : OK
Content           : Sony Corporation
RawContent        : HTTP/1.1 200 OK
                    Connection: keep-alive
                    x-request-id: lhgjrrs7mf0desm40sifji9reoehi08b
                    Content-Length: 16
                    Cache-Control: max-age=0, private, must-revalidate
                    Content-Type: text/plain; charset=utf-8...
Forms             : {}
Headers           : {[Connection, keep-alive], [x-request-id, lhgjrrs7mf0desm40sifji9reoehi08b], [Content-Length, 16],
                    [Cache-Control, max-age=0, private, must-revalidate]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 16

This provides with a lot of useless information. All I really want is the content field which contains the manufacturer information. So what I’m going to do is wrap the Invoke-WebRequest in brackets and select the content field as shown below:

(Invoke-WebRequest -Uri "https://api.macvendors.com/$mac_example").content

Which simple returns “Sony Corporation”. Perfect. Enjoy!

Make Website Confirm Before Leaving

Long pause between blogs here but busy life = slow blog.

In this blog, I will discuss how I created a simple webpage that would allow me to leave a pinned tab open in my web browser that would create a popup box when trying to leave the site. The popup box can be seen below:

This is helpful because Chrome doesn’t give the option to make sure you actually want to close the window. So if you have ever, just like myself, been 2 hours deep into researching something that seemed really important at the time and then accidentally clicked the red X and have it all close, then you know how I feel.

So I created my own website that I would load and pin in the Chome windows so that this wouldn’t happen to me. This is the code that I used:

<script>
 window.onbeforeload = function(){
  return "Do you really want to leave?";
 };
</script>

and the site that I created is currently live at noautoclose.mharwood.uk

Just make sure to click anywhere on the page for the script to work, enjoy!

Customized Email Signatures In HTML

Back off holiday, want to do something simple. So email signature it is.

I made mine in the Mimecast administrator center as a way to become more familiar with HTML so that I could create an actual signature for some users.

Before we look at the code – I will put an image below of what the signature would actually look like. Also, I own NONE of the images/sites shown here. (Sorry, just didn’t want legal people sniffing around).

Signature

Now that you have seen the signature, you can see that is it in no way a real world example of something that should be used. But I also couldn’t use the actual one for…reasons. I just wanted to show you every feature I used in the real one.

Below is the actual code I used:

<html>
 <head>
 </head>
  <body><mc type="body">
  <p style="font-family:calibri:">This is a <b>test</b> email signature</p>

  <img src="http://combiboilersleeds.com/images/test/test-5.jpeg" alt="TEST LEFT" style="float:left;width:50px;height:20px;" />
  <p style="font-family:courier;">This is the second line of the test email signature</p>
  <p style="font-family:verdana;font-size:80%;">This is the last line of the test email signature</p>

  <img src="http://combiboilersleeds.com/images/test/test-5.jpeg" alt="TEST IMAGE" style="width:200px;height:70px;" />

  Click <a href="https://krebsonsecurity.com/" target="_blank"><i>here</i></a> to open a link

 </body>
</html>

You may notice that I have linked Krebs blog – Enjoy!

Adding Signatures In Mimecast

Bit different from, the now usual, Powershell guides. Thought I’d do something different. This guide will show you how to add a custom signature to a user, multiple users’ or even globally.

Lets start off. You’ll want to log into your Mimecast administrative console, go to Service-Gateway-Policies. In here you should see “Stationary”. We want to create a new definition, so click on the “Definition” button on the “Stationary” listing.

Here you can click the “New Item” button. For the Description, obviously, enter a brief description of the new definition. The “Unique Identification Text” is used to scan an email that the definition is applied to to determine if the signature has already been added, so you want to add something that is in the signature and something that is unique to this signature. For the short code, enter a unique name. Below is an example of mine:

1

Now we want to edit the HTML so that there actually is a signature to put onto the emails. Click on the “Edit HTML” button and enter your signature. This is the code I added to mine:

<html>
 <head>
 </head>
  <body>
    <mc type="body">

    <font size="2" color="gray">
    This is a test email signature for marketing
    </font>

    <img src="IMAGE LINK" alt="ALTERNATIVE IMAGE NAME" style="width:214px;height:24px:" />

 </body>
</html>

So in my signature I have the message “This is a test email signature for marketing” and also a company logo.

Now we have that, we need to add it to an actual policy, otherwise it simply won’t be used by anything. Navigate back after saving your definition to the “Policies” page and click on the “Stationary” row. You now want to click “New Policy” which should open up a new policy creation window.

For the “Policy Narrative” enter a name for the policy, for the “Select Stationary”, select “lookup”, find your newly created definition and click “Select”. Since I created mine as a test and my Mimecast access is on my business email address, I configured an individual send and to email address for the policy to apply to. On this page you can also select when the policy will be active or even a range of source IP addresses.

Below are the values that I entered. The emails are removed for obvious reasons:

2

Finally, below is an example of an email with the newly added signature:

3

You should now have a custom signature that applies to every user defined in the policy instead of individual users.