Functions/Send-MailMessageUsingHtmlTemplate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Function Send-MailMessageUsingHtmlTemplate {
    <#
        .Synopsis
            Send mail using a html file template
        .DESCRIPTION
            Replace content in provided template using input hashtable.
        .PARAMETER MailTo
            Receiver(s) of the mail (;-separated list)
        .PARAMETER MailFrom
            Name of the sender
        .PARAMETER Subject
            The subject line of the mail
        .PARAMETER TemplateFile
            The full path to the html template file
        .PARAMETER ReplacementHash
            Hashtable to use for replace strings in the template. The hash key is used for search and replace
        .PARAMETER Attachements
            A string array of file names to attach with the mail
        .PARAMETER SMTPserver
            The server to use for sending the mail.
        .NOTES
            TIP: Use a domain json-file or dfp-file to set default SMTP server name
            "Send-MailMessage*:SMTPserver": "smtp.contoso.com",
             
            Send-MailMessage*:SMTPserver="smtp.contoso.com"
     
        #>

        Param(
         [parameter(mandatory=$true)]
         [string]$MailTo,
         [string]$MailFrom,
         [parameter(mandatory=$true)]
         [string]$Subject,
         [string]$TemplateFile,
          [parameter(mandatory=$true)]
         [hashtable]$replacementHash,
         [string[]]$Attachments,
         [string]$SMTPserver
        )
        
        if (Test-Path $TemplateFile) {        
            $messageBody = Get-Content -Path $TemplateFile | Out-String

            #Use corresponding txt file to override input paramameters (if found)
            $parameterFile = $TemplateFile.Substring(0,$TemplateFile.LastIndexOf('.')) + ".txt"
            if (Test-Path($parameterFile)) {
                #Prameter file syntax
                #$Subject="Standard subject line for this template file"
                Get-Content -Path $parameterFile | Invoke-Expression
            }
    
            #Replace "key" text in template and subject line with hash content
            ForEach($key in $replacementHash.Keys) {
                $Subject = $Subject -replace $key,$replacementHash[$key]
                $messageBody = $messageBody -replace $key,$replacementHash[$key]
            }

            Write-Verbose "Send mail to [$MailTo] from [$MailFrom] using [$SMTPserver]"
            
            #Split multiple addresses
            if ($MailTo.IndexOf(';') -gt 0) {
                $MailTo=$MailTo.Split(';')
            }

            if ($Attachments) {
                Send-MailMessage -SmtpServer $SMTPserver -To $MailTo -From $MailFrom -Subject $Subject -Body $messageBody -BodyAsHtml -ErrorAction Stop -Encoding UTF8 -Attachments $Attachments
            } else {
                Send-MailMessage -SmtpServer $SMTPserver -To $MailTo -From $MailFrom -Subject $Subject -Body $messageBody -BodyAsHtml -ErrorAction Stop -Encoding UTF8
            }
           
        } else {
            throw [System.IO.FileNotFoundException] "Missing mail template file $TemplateFile"
        }
    }