Skip to content

Commit 39a8ef4

Browse files
Merge pull request #10 from jebbster88/development
Allow storing attachment binary data within the clixml file
2 parents bea245b + 7c6cf88 commit 39a8ef4

3 files changed

Lines changed: 58 additions & 8 deletions

File tree

MailDaemon/functions/Invoke-MDDaemon.ps1

Lines changed: 24 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
@@ -53,11 +53,29 @@
5353
if ($email.From) { $parameters["From"] = $email.From }
5454
else { $parameters["From"] = Get-PSFConfigValue -FullName 'MailDaemon.Daemon.SenderDefault' }
5555
if ($email.Cc) { $parameters["Cc"] = $email.Cc }
56+
if ($email.Bcc) { $parameters["Bcc"] = $email.Bcc }
5657
if ($email.Subject) { $parameters["Subject"] = $email.Subject }
5758
else { $parameters["Subject"] = "<no subject>" }
59+
if ($email.Priority) {$parameters["Priority"] = $email.Priority}
5860
if ($email.Body) { $parameters["Body"] = $email.Body }
5961
if ($null -ne $email.BodyAsHtml) { $parameters["BodyAsHtml"] = $email.BodyAsHtml }
60-
if ($email.Attachments) { $parameters["Attachments"] = $email.Attachments }
62+
if ($email.Attachments) {
63+
if ($email.AttachmentsBinary) {
64+
$tempAttachmentParentDir = New-Item (join-path $item.Directory $item.BaseName) -Force -ItemType Directory
65+
$attachmentCounter = 0
66+
$parameters["Attachments"] = @()
67+
# Using multiple subfolders to allow for duplicate attachment names
68+
foreach ($binaryAttachment in $email.AttachmentsBinary) {
69+
$tempAttachmentDir = new-item (join-path $tempAttachmentParentDir $attachmentCounter) -Force -ItemType Directory
70+
$tempAttachmentPath = join-path $tempAttachmentDir $binaryAttachment.Name
71+
$null = [System.IO.File]::WriteAllBytes($tempAttachmentPath, $binaryAttachment.Data)
72+
$parameters["Attachments"] = @($parameters["Attachments"]) + $tempAttachmentPath
73+
$attachmentCounter = $attachmentCounter + 1
74+
}
75+
} else {
76+
$parameters["Attachments"] = $email.Attachments
77+
}
78+
}
6179
if ($script:_Config.SenderCredentialPath) { $parameters["Credential"] = Import-Clixml (Get-PSFConfigValue -FullName 'MailDaemon.Daemon.SenderCredentialPath') }
6280

6381
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 @@
7391
Remove-Item $attachment -Force
7492
}
7593
}
94+
# Remove temp deserialized attachments if used
95+
if ($email.AttachmentsBinary) {
96+
$null = remove-item -Path $tempAttachmentParentDir -Recurse -Force
97+
}
7698

7799
# Update the timestamp (the timeout for deletion uses this) and move it to the sent items folder
78100
$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

MailDaemon/functions/Set-MDMail.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
function Set-MDMail
1+
enum MailPriority {
2+
Normal
3+
Low
4+
High
5+
}
6+
7+
function Set-MDMail
28
{
39
<#
410
.SYNOPSIS
@@ -49,12 +55,15 @@
4955
[string]
5056
$From,
5157

52-
[string]
58+
[string[]]
5359
$To,
5460

5561
[string[]]
5662
$Cc,
5763

64+
[string[]]
65+
$Bcc,
66+
5867
[string]
5968
$Subject,
6069

@@ -64,14 +73,17 @@
6473
[switch]
6574
$BodyAsHtml,
6675

67-
[string]
76+
[string[]]
6877
$Attachments,
6978

7079
[switch]
7180
$RemoveAttachments,
7281

7382
[datetime]
74-
$NotBefore
83+
$NotBefore,
84+
85+
[MailPriority]
86+
$Priority
7587
)
7688

7789
begin
@@ -86,11 +98,13 @@
8698
if ($From) { $script:mail["From"] = $From }
8799
if ($To) { $script:mail["To"] = $To }
88100
if ($Cc) { $script:mail["Cc"] = $Cc }
101+
if ($Bcc) { $script:mail["Bcc"] = $Bcc }
89102
if ($Subject) { $script:mail["Subject"] = $Subject }
90103
if ($Body) { $script:mail["Body"] = $Body }
91104
if ($BodyAsHtml.IsPresent) { $script:mail["BodyAsHtml"] = ([bool]$BodyAsHtml) }
92105
if ($Attachments) { $script:mail["Attachments"] = $Attachments }
93106
if ($RemoveAttachments.IsPresent) { $script:mail["RemoveAttachments"] = ([bool]$RemoveAttachments) }
94107
if ($NotBefore) { $script:mail["NotBefore"] = $NotBefore }
108+
if ($Priority) { $script:mail["Priority"] = $Priority }
95109
}
96110
}

0 commit comments

Comments
 (0)