handlers/mail.psm1
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
<#
.Synopsis Handler for mail .DESCRIPTION by default Keep=$true, this means that the mail is send only when Flush() occurs #> function New-uLogMail { param( [string] $Name, [string] $Source = $MyInvocation.ScriptName, [string] $Formatter = [LogFormatter]::MailDefault, [LogLevel] $Level = [LogLevel]::SUCCESS, [Parameter(Mandatory=$true)] [string] $From = '', [Parameter(Mandatory=$true)] [string] $To = '', [string] $Cc = '', [string] $Bcc = '', [Parameter(Mandatory=$true)] [string] $Smtp = '', [string] $Port = 25, [PSCredential] $Credentials = $null, [switch] $Append, [switch] $Keep = $true, [Switch] $Enabled = $true ) Begin{ } Process{ $log = [PSCustomObject] @{Name = $Name; Enabled = $Enabled; Type = 'mail'; Formatter = $Formatter; Level = $Level; From = $From; To = $To; Cc = $Cc; Bcc = $Bcc; Smtp = $Smtp; Port = $Port; Credentials = $Credentials; Source = $Source; Path = $Path; Data = @(); Keep = $Keep; } $log | Add-Member -MemberType ScriptMethod -Name Flush -Value { $newData = "<br><table><tr><td>" + [string]::Join("</td></tr><tr><td>", $this.Data.Message) + "</tr></td></table>" $maxLevel = ($this.Data.Level | Get-Unique | % {[LogLevel]::$_.Value__} | Measure-Object -Maximum).Maximum $level = [loglevel]::GetName([loglevel], [int] $maxLevel) if ($this.Formatter -notmatch '-'){$this.Formatter = 'Format-' + $this.Formatter} $FormattedMessage = & $this.Formatter -Record (New-LogRecord -Message $newData -Level $level ) $FormattedMessage += ("<BR><BR><b><i>Source : </b>{0}</i><BR>" -f $this.Source) #TOFIX:add Source when calling formatter #TOFIX:$newData should be an array, and formatter format the array $subject = "[{0}] - {1}" -f $level, $this.Source.Substring( $this.Source.LastIndexOf('\') + 1) $args = @{} if ($this.Cc -ne ''){$args['Cc'] = $this.Cc} if ($this.Bcc -ne ''){$args['Bcc'] = $this.Bcc} if ($this.Credentials -ne $null){$args['Credentials'] = $this.Credentials} Send-MailMessage -From $this.From ` -To $this.To ` -Smtp $this.Smtp ` -Port $this.Port ` -Subject $subject ` -BodyAsHtml $FormattedMessage ` @args $this.Data = '' } $log | Add-Member -MemberType ScriptMethod -Name WriteLog -Value { param($Record) if (-not $this.Enabled){return} if ($Record.Level -ge $this.Level){ if ($this.Keep -eq $true){ $this.Data += $Record }else{ if ($this.Formatter -notmatch '-'){$this.Formatter = 'Format-' + $this.Formatter} $FormattedMessage = & $this.Formatter -Record $Record $FormattedMessage += ("<BR><BR><BR><b><i>Source : </b>{0}</i><BR>" -f $this.Source) $subject = "[{0}] - {1}" -f $Record.Level, $this.Source.Substring( $this.Source.LastIndexOf('\') + 1) $args = @{} if ($this.Cc -ne ''){$args['Cc'] = $this.Cc} if ($this.Bcc -ne ''){$args['Bcc'] = $this.Bcc} if ($this.Credentials -ne $null){$args['Credentials'] = $this.Credentials} Send-MailMessage -From $this.From ` -To $this.To ` -Smtp $this.Smtp ` -Port $this.Port ` -Subject $subject ` -BodyAsHtml $FormattedMessage ` @args } } } $log } } |