Skip to content
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
4 changes: 2 additions & 2 deletions MailDaemon/MailDaemon.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions MailDaemon/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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
28 changes: 24 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 All @@ -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.

Expand All @@ -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 '[email protected]' -To '[email protected]' -Subject 'Daily Update Report' -Body $body

Expand All @@ -49,12 +61,15 @@
[string]
$From,

[string]
[string[]]
$To,

[string[]]
$Cc,

[string[]]
$Bcc,

[string]
$Subject,

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

[string]
[string[]]
$Attachments,

[switch]
$RemoveAttachments,

[datetime]
$NotBefore
$NotBefore,

[MailPriority]
$Priority
)

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