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 diff --git a/MailDaemon/functions/Invoke-MDDaemon.ps1 b/MailDaemon/functions/Invoke-MDDaemon.ps1 index 59e4068..e626d3d 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 @@ -53,11 +53,29 @@ 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) { $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 +91,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..d17aac8 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 diff --git a/MailDaemon/functions/Set-MDMail.ps1 b/MailDaemon/functions/Set-MDMail.ps1 index 1567aed..42da978 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 @@ -16,6 +22,9 @@ .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. @@ -38,6 +47,9 @@ .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 @@ -49,12 +61,15 @@ [string] $From, - [string] + [string[]] $To, [string[]] $Cc, + [string[]] + $Bcc, + [string] $Subject, @@ -64,14 +79,17 @@ [switch] $BodyAsHtml, - [string] + [string[]] $Attachments, [switch] $RemoveAttachments, [datetime] - $NotBefore + $NotBefore, + + [MailPriority] + $Priority ) begin @@ -86,11 +104,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 } } }