Skip to content

Commit 7f2f3a1

Browse files
author
GJEBB
committed
Added -PersistAttachments switch to Send-MDMail which stores the attachment's binary in the serialised mail object.
1 parent bea245b commit 7f2f3a1

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

MailDaemon/functions/Invoke-MDDaemon.ps1

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
process
3737
{
3838
#region Send mails
39-
foreach ($item in (Get-ChildItem -Path (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath')))
39+
foreach ($item in (Get-ChildItem -Path (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.MailPickupPath') -Filter "*.clixml"))
4040
{
4141
$email = Import-Clixml -Path $item.FullName
4242
# Skip emails that should not yet be processed
@@ -57,7 +57,23 @@
5757
else { $parameters["Subject"] = "<no subject>" }
5858
if ($email.Body) { $parameters["Body"] = $email.Body }
5959
if ($null -ne $email.BodyAsHtml) { $parameters["BodyAsHtml"] = $email.BodyAsHtml }
60-
if ($email.Attachments) { $parameters["Attachments"] = $email.Attachments }
60+
if ($email.Attachments) {
61+
if ($email.AttachmentsBinary) {
62+
$tempAttachmentParentDir = New-Item (join-path $item.Directory $item.BaseName) -Force -ItemType Directory
63+
$attachmentCounter = 0
64+
$parameters["Attachments"] = @()
65+
# Using multiple subfolders to allow for duplicate attachment names
66+
foreach ($binaryAttachment in $email.AttachmentsBinary) {
67+
$tempAttachmentDir = new-item (join-path $tempAttachmentParentDir $attachmentCounter) -Force -ItemType Directory
68+
$tempAttachmentPath = join-path $tempAttachmentDir $binaryAttachment.Name
69+
$null = [System.IO.File]::WriteAllBytes($tempAttachmentPath, $binaryAttachment.Data)
70+
$parameters["Attachments"] = @($parameters["Attachments"]) + $tempAttachmentPath
71+
$attachmentCounter = $attachmentCounter + 1
72+
}
73+
} else {
74+
$parameters["Attachments"] = $email.Attachments
75+
}
76+
}
6177
if ($script:_Config.SenderCredentialPath) { $parameters["Credential"] = Import-Clixml (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.SenderCredentialPath') }
6278

6379
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 @@
7389
Remove-Item $attachment -Force
7490
}
7591
}
92+
# Remove temp deserialized attachments if used
93+
if ($email.AttachmentsBinary) {
94+
$null = remove-item -Path $tempAttachmentParentDir -Recurse -Force
95+
}
7696

7797
# Update the timestamp (the timeout for deletion uses this) and move it to the sent items folder
7898
$item.LastWriteTime = Get-Date

MailDaemon/functions/Send-MDMail.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
Name of the task that is sending the email.
1212
Used in the name of the file used to queue messages in order to reduce likelyhood of accidental clash.
1313
14+
.PARAMETER PersistAttachments
15+
Attachments will be serialized with the queued email allowing the source files to be removed immediately.
16+
1417
.EXAMPLE
1518
PS C:\> Send-MDMail -TaskName "Logrotate"
1619
@@ -20,7 +23,8 @@
2023
Param (
2124
[Parameter(Mandatory = $true)]
2225
[string]
23-
$TaskName
26+
$TaskName,
27+
[switch]$PersistAttachments
2428
)
2529

2630
begin
@@ -42,9 +46,19 @@
4246

4347
$script:mail['Taskname'] = $TaskName
4448

49+
if ($PersistAttachments) {
50+
# Add the attachments bytes to the mail object
51+
if (-not $script:mail["AttachmentsBinary"]) {
52+
$script:mail["AttachmentsBinary"] = @()
53+
}
54+
foreach ($attachment in $script:mail['Attachments']) {
55+
$script:mail['AttachmentsBinary'] = @($script:mail['AttachmentsBinary']) + @{Name = (split-path -Path $attachment -Leaf); Data = [System.IO.File]::ReadAllBytes($attachment)}
56+
}
57+
}
58+
4559
# Send the email
4660
Write-PSFMessage -String 'Send-MDMail.Email.Sending' -StringValues $TaskName -Target $TaskName
47-
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 }
61+
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 }
4862
catch
4963
{
5064
Stop-PSFFunction -String 'Send-MDMail.Email.SendingFailed' -StringValues $TaskName -ErrorRecord $_ -Cmdlet $PSCmdlet -EnableException $true -Target $TaskName

0 commit comments

Comments
 (0)