BIReportGuy
BIReportGuy

Reputation: 817

Powershell Converting Body to HTML Body

I have a script that looks to folder for updates and sends out an email if there has been any updates. How do I change or convert the body of the email to a URL or HTML link instead of text?

The below path and servers are examples.

Here's my script:

    Param (
    [string]$Path = "\\Desktop\Myfolder\status updates",
    [string]$SMTPServer = "smtp.myfolder.com",
    [string]$From = "[email protected]",
    [string]$To = "[email protected]",
    [string]$CC = "[email protected]",
    [string]$Subject = "New Status Report"
    )

$SMTPMessage = @{
    To = $To
    Cc = $cc
    From = $From
    Subject = "$Subject"
    Smtpserver = $SMTPServer    
}

$File = Get-ChildItem $Path | Where { $_.LastWriteTime -ge [datetime]::Now.AddMinutes(-10) }
If ($File)
{   $SMTPBody = "`The following files have recently been added/changed.  Please copy and paste the following link to Windows Explorer:`n`n"
    $File | ForEach { $SMTPBody += "$($_.FullName)`n" }

    Send-MailMessage @SMTPMessage -Body $SMTPBody

Upvotes: 1

Views: 6997

Answers (2)

FoxDeploy
FoxDeploy

Reputation: 13537

PowerShell offers a Convert-ToHTML cmdlet which is perfect for your needs.

Simply pipe your content, $SMTPBody, into it. You can even apply a CSS file to make the e-mail look just that extra bit better.

$HTMLBody = ConvertTo-HTML -body $SMTPBody -CSSUri http://www.csszengarden.com/examples/style.css

Then reference $HTMLBody in your e-mail.

The sky is the limit with CSS. If you'd like to read more, I covered this topic in a blog post. Here's an example of what we can create. enter image description here

In this example, we use the -Pre and -Post parameters to add content above and below the table. The table is created by piping objects into Convert-ToHTML with the -As Table property. This cmdlet with just four parameters can create Web page worthy output!

Upvotes: 3

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10754

Set your $SMTPBody value to be a string containing the HTML, and then add the -BodyAsHTML switch to your Send-MailMessage command.

...
$SMTPBody = "<em>This is italicised</em>"
...

Send-MailMessage -Body $SMTPBody -BodyAsHTML -To ...

Upvotes: 1

Related Questions