From 7f2f3a119a609237863b3ab3dabe48cdd2e73201 Mon Sep 17 00:00:00 2001 From: GJEBB Date: Thu, 5 Sep 2024 13:18:04 +0100 Subject: [PATCH 1/4] Added -PersistAttachments switch to Send-MDMail which stores the attachment's binary in the serialised mail object. --- MailDaemon/functions/Invoke-MDDaemon.ps1 | 24 ++++++++++++++++++++++-- MailDaemon/functions/Send-MDMail.ps1 | 18 ++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/MailDaemon/functions/Invoke-MDDaemon.ps1 b/MailDaemon/functions/Invoke-MDDaemon.ps1 index 59e4068..121ce68 100644 --- a/MailDaemon/functions/Invoke-MDDaemon.ps1 +++ b/MailDaemon/functions/Invoke-MDDaemon.ps1 @@ -36,7 +36,7 @@ process { #region Send mails - foreach ($item in (Get-ChildItem -Path (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath'))) + foreach ($item in (Get-ChildItem -Path (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath') -Filter "*.clixml")) { $email = Import-Clixml -Path $item.FullName # Skip emails that should not yet be processed @@ -57,7 +57,23 @@ else { $parameters["Subject"] = "" } if ($email.Body) { $parameters["Body"] = $email.Body } if ($null -ne $email.BodyAsHtml) { $parameters["BodyAsHtml"] = $email.BodyAsHtml } - if ($email.Attachments) { $parameters["Attachments"] = $email.Attachments } + if ($email.Attachments) { + if ($email.AttachmentsBinary) { + $tempAttachmentParentDir = New-Item (join-path $item.Directory $item.BaseName) -Force -ItemType Directory + $attachmentCounter = 0 + $parameters["Attachments"] = @() + # Using multiple subfolders to allow for duplicate attachment names + foreach ($binaryAttachment in $email.AttachmentsBinary) { + $tempAttachmentDir = new-item (join-path $tempAttachmentParentDir $attachmentCounter) -Force -ItemType Directory + $tempAttachmentPath = join-path $tempAttachmentDir $binaryAttachment.Name + $null = [System.IO.File]::WriteAllBytes($tempAttachmentPath, $binaryAttachment.Data) + $parameters["Attachments"] = @($parameters["Attachments"]) + $tempAttachmentPath + $attachmentCounter = $attachmentCounter + 1 + } + } else { + $parameters["Attachments"] = $email.Attachments + } + } if ($script:_Config.SenderCredentialPath) { $parameters["Credential"] = Import-Clixml (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.SenderCredentialPath') } Write-PSFMessage -Level Verbose -String 'Invoke-MDDaemon.SendMail.Start' -StringValues @($email.Taskname, $parameters['Subject'], $parameters['From'], ($parameters['To'] -join ",")) -Target $email.Taskname @@ -73,6 +89,10 @@ Remove-Item $attachment -Force } } + # Remove temp deserialized attachments if used + if ($email.AttachmentsBinary) { + $null = remove-item -Path $tempAttachmentParentDir -Recurse -Force + } # Update the timestamp (the timeout for deletion uses this) and move it to the sent items folder $item.LastWriteTime = Get-Date diff --git a/MailDaemon/functions/Send-MDMail.ps1 b/MailDaemon/functions/Send-MDMail.ps1 index 63ab0a7..685dcca 100644 --- a/MailDaemon/functions/Send-MDMail.ps1 +++ b/MailDaemon/functions/Send-MDMail.ps1 @@ -11,6 +11,9 @@ Name of the task that is sending the email. Used in the name of the file used to queue messages in order to reduce likelyhood of accidental clash. + .PARAMETER PersistAttachments + Attachments will be serialized with the queued email allowing the source files to be removed immediately. + .EXAMPLE PS C:\> Send-MDMail -TaskName "Logrotate" @@ -20,7 +23,8 @@ Param ( [Parameter(Mandatory = $true)] [string] - $TaskName + $TaskName, + [switch]$PersistAttachments ) begin @@ -42,9 +46,19 @@ $script:mail['Taskname'] = $TaskName + if ($PersistAttachments) { + # Add the attachments bytes to the mail object + if (-not $script:mail["AttachmentsBinary"]) { + $script:mail["AttachmentsBinary"] = @() + } + foreach ($attachment in $script:mail['Attachments']) { + $script:mail['AttachmentsBinary'] = @($script:mail['AttachmentsBinary']) + @{Name = (split-path -Path $attachment -Leaf); Data = [System.IO.File]::ReadAllBytes($attachment)} + } + } + # Send the email Write-PSFMessage -String 'Send-MDMail.Email.Sending' -StringValues $TaskName -Target $TaskName - try { [PSCustomObject]$script:mail | Export-Clixml -Path "$(Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath')\$($TaskName)-$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').clixml" -ErrorAction Stop } + try { [PSCustomObject]$script:mail | Export-Clixml -Path "$(Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath')\$($TaskName)-$(Get-Date -Format 'yyyy-MM-dd_HH-mm-ss').clixml" -Depth 4 -ErrorAction Stop } catch { Stop-PSFFunction -String 'Send-MDMail.Email.SendingFailed' -StringValues $TaskName -ErrorRecord $_ -Cmdlet $PSCmdlet -EnableException $true -Target $TaskName From 7c6cf880e117e466bbbad8614dc5e56c31c74ddf Mon Sep 17 00:00:00 2001 From: GJEBB Date: Tue, 10 Sep 2024 14:05:44 +0100 Subject: [PATCH 2/4] Set-MDMail: To and Attachments accept arrays. Bcc and Priority (with enum) added. --- MailDaemon/functions/Invoke-MDDaemon.ps1 | 2 ++ MailDaemon/functions/Set-MDMail.ps1 | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/MailDaemon/functions/Invoke-MDDaemon.ps1 b/MailDaemon/functions/Invoke-MDDaemon.ps1 index 121ce68..77e5a08 100644 --- a/MailDaemon/functions/Invoke-MDDaemon.ps1 +++ b/MailDaemon/functions/Invoke-MDDaemon.ps1 @@ -53,8 +53,10 @@ if ($email.From) { $parameters["From"] = $email.From } else { $parameters["From"] = Get-PSFConfigValue -FullName 'MailDaemon.Daemon.SenderDefault' } if ($email.Cc) { $parameters["Cc"] = $email.Cc } + if ($email.Bcc) { $parameters["Bcc"] = $email.Bcc } if ($email.Subject) { $parameters["Subject"] = $email.Subject } else { $parameters["Subject"] = "" } + if ($email.Priority) {$parameters["Priority"] = $email.Priority} if ($email.Body) { $parameters["Body"] = $email.Body } if ($null -ne $email.BodyAsHtml) { $parameters["BodyAsHtml"] = $email.BodyAsHtml } if ($email.Attachments) { diff --git a/MailDaemon/functions/Set-MDMail.ps1 b/MailDaemon/functions/Set-MDMail.ps1 index 1567aed..30728b5 100644 --- a/MailDaemon/functions/Set-MDMail.ps1 +++ b/MailDaemon/functions/Set-MDMail.ps1 @@ -1,4 +1,10 @@ -function Set-MDMail +enum MailPriority { + Normal + Low + High +} + +function Set-MDMail { <# .SYNOPSIS @@ -49,12 +55,15 @@ [string] $From, - [string] + [string[]] $To, [string[]] $Cc, + [string[]] + $Bcc, + [string] $Subject, @@ -64,14 +73,17 @@ [switch] $BodyAsHtml, - [string] + [string[]] $Attachments, [switch] $RemoveAttachments, [datetime] - $NotBefore + $NotBefore, + + [MailPriority] + $Priority ) begin @@ -86,11 +98,13 @@ if ($From) { $script:mail["From"] = $From } if ($To) { $script:mail["To"] = $To } if ($Cc) { $script:mail["Cc"] = $Cc } + if ($Bcc) { $script:mail["Bcc"] = $Bcc } if ($Subject) { $script:mail["Subject"] = $Subject } if ($Body) { $script:mail["Body"] = $Body } if ($BodyAsHtml.IsPresent) { $script:mail["BodyAsHtml"] = ([bool]$BodyAsHtml) } if ($Attachments) { $script:mail["Attachments"] = $Attachments } if ($RemoveAttachments.IsPresent) { $script:mail["RemoveAttachments"] = ([bool]$RemoveAttachments) } if ($NotBefore) { $script:mail["NotBefore"] = $NotBefore } + if ($Priority) { $script:mail["Priority"] = $Priority } } } From be070852f1ac9902538b6dd57fb10b459b12cc1c Mon Sep 17 00:00:00 2001 From: Friedrich Weinmann Date: Mon, 11 Nov 2024 16:55:59 +0100 Subject: [PATCH 3/4] version update --- MailDaemon/MailDaemon.psd1 | 4 ++-- MailDaemon/changelog.md | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/MailDaemon/MailDaemon.psd1 b/MailDaemon/MailDaemon.psd1 index ccdf431..e1ef503 100644 --- a/MailDaemon/MailDaemon.psd1 +++ b/MailDaemon/MailDaemon.psd1 @@ -3,7 +3,7 @@ RootModule = 'MailDaemon.psm1' # Version number of this module. - ModuleVersion = '1.0.1' + ModuleVersion = '1.1.3' # ID used to uniquely identify this module GUID = 'd5ba333f-5210-4d69-83f0-150dd0909139' @@ -26,7 +26,7 @@ # Modules that must be imported into the global environment prior to importing # this module RequiredModules = @( - @{ ModuleName='PSFramework'; ModuleVersion='1.9.310' } + @{ ModuleName='PSFramework'; ModuleVersion='1.12.346' } ) # Assemblies that must be loaded prior to importing this module diff --git a/MailDaemon/changelog.md b/MailDaemon/changelog.md index 3416125..876d751 100644 --- a/MailDaemon/changelog.md +++ b/MailDaemon/changelog.md @@ -1,5 +1,10 @@ # Changelog +## 1.1.3 (2024-11-11) + ++ Upd: Added ability to directly embed attachments in the email task, rather than only providing a path to them. (thanks @jebbster88 ; #10) ++ Upd: Added ability to specify mail priority. (thanks @jebbster88 ; #10) + ## 1.0.1 (2023-10-06) + Fix: Invoke-MDDaemon - errors trying to multiply timespan From c201c736ecb3dd04a24e2f416b2d57bd989b0604 Mon Sep 17 00:00:00 2001 From: Friedrich Weinmann Date: Tue, 12 Nov 2024 01:38:26 +0100 Subject: [PATCH 4/4] test fixes --- MailDaemon/functions/Invoke-MDDaemon.ps1 | 2 +- MailDaemon/functions/Send-MDMail.ps1 | 2 +- MailDaemon/functions/Set-MDMail.ps1 | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/MailDaemon/functions/Invoke-MDDaemon.ps1 b/MailDaemon/functions/Invoke-MDDaemon.ps1 index 77e5a08..e626d3d 100644 --- a/MailDaemon/functions/Invoke-MDDaemon.ps1 +++ b/MailDaemon/functions/Invoke-MDDaemon.ps1 @@ -91,7 +91,7 @@ Remove-Item $attachment -Force } } - # Remove temp deserialized attachments if used + # Remove temp deserialized attachments if used if ($email.AttachmentsBinary) { $null = remove-item -Path $tempAttachmentParentDir -Recurse -Force } diff --git a/MailDaemon/functions/Send-MDMail.ps1 b/MailDaemon/functions/Send-MDMail.ps1 index 685dcca..d17aac8 100644 --- a/MailDaemon/functions/Send-MDMail.ps1 +++ b/MailDaemon/functions/Send-MDMail.ps1 @@ -50,7 +50,7 @@ # Add the attachments bytes to the mail object if (-not $script:mail["AttachmentsBinary"]) { $script:mail["AttachmentsBinary"] = @() - } + } foreach ($attachment in $script:mail['Attachments']) { $script:mail['AttachmentsBinary'] = @($script:mail['AttachmentsBinary']) + @{Name = (split-path -Path $attachment -Leaf); Data = [System.IO.File]::ReadAllBytes($attachment)} } diff --git a/MailDaemon/functions/Set-MDMail.ps1 b/MailDaemon/functions/Set-MDMail.ps1 index 30728b5..42da978 100644 --- a/MailDaemon/functions/Set-MDMail.ps1 +++ b/MailDaemon/functions/Set-MDMail.ps1 @@ -22,6 +22,9 @@ function Set-MDMail .PARAMETER Cc Additional addresses to keep in the information flow. + .PARAMETER Bcc + Additional addresses to keep silently informed + .PARAMETER Subject The subject to send the email under. @@ -44,6 +47,9 @@ function Set-MDMail .PARAMETER NotBefore Do not send this email before this timestamp has come to pass. + .PARAMETER Priority + The priority of the email + .EXAMPLE PS C:\> Set-MDMail -From 'script@contoso.com' -To 'support@contoso.com' -Subject 'Daily Update Report' -Body $body @@ -80,7 +86,7 @@ function Set-MDMail $RemoveAttachments, [datetime] - $NotBefore, + $NotBefore, [MailPriority] $Priority