Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow storing attachment binary data within the clixml file #10

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions MailDaemon/functions/Invoke-MDDaemon.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"] = "<no 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
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions MailDaemon/functions/Send-MDMail.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -20,7 +23,8 @@
Param (
[Parameter(Mandatory = $true)]
[string]
$TaskName
$TaskName,
[switch]$PersistAttachments
)

begin
Expand All @@ -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
Expand Down
22 changes: 18 additions & 4 deletions MailDaemon/functions/Set-MDMail.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function Set-MDMail
enum MailPriority {
Normal
Low
High
}

function Set-MDMail
{
<#
.SYNOPSIS
Expand Down Expand Up @@ -49,12 +55,15 @@
[string]
$From,

[string]
[string[]]
$To,

[string[]]
$Cc,

[string[]]
$Bcc,

[string]
$Subject,

Expand All @@ -64,14 +73,17 @@
[switch]
$BodyAsHtml,

[string]
[string[]]
$Attachments,

[switch]
$RemoveAttachments,

[datetime]
$NotBefore
$NotBefore,

[MailPriority]
$Priority
)

begin
Expand All @@ -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 }
}
}
Loading