Skip to content

Commit ec37d48

Browse files
author
Friedrich Weinmann
committed
handling failed emails
1 parent 9468e09 commit ec37d48

8 files changed

Lines changed: 204 additions & 99 deletions

File tree

MailDaemon/MailDaemon.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RootModule = 'MailDaemon.psm1'
44

55
# Version number of this module.
6-
ModuleVersion = '1.1.7'
6+
ModuleVersion = '1.2.13'
77

88
# ID used to uniquely identify this module
99
GUID = 'd5ba333f-5210-4d69-83f0-150dd0909139'
@@ -26,7 +26,7 @@
2626
# Modules that must be imported into the global environment prior to importing
2727
# this module
2828
RequiredModules = @(
29-
@{ ModuleName='PSFramework'; ModuleVersion='1.12.346' }
29+
@{ ModuleName='PSFramework'; ModuleVersion='1.13.416' }
3030
)
3131

3232
# Assemblies that must be loaded prior to importing this module

MailDaemon/changelog.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 1.2.13 (2026-02-11)
4+
5+
+ New: Emails that could not be sent will no longer be permanently attempted to resend - after 14 days
6+
+ Upd: Install-MDDaemon - now supports remote-deployment of the daemon task using credentials
7+
+ Upd: Install-MDDaemon - now installs eventlog for MailDaemon
8+
+ Upd: Invoke-MDDaemon - adds logging to the Windows Eventlog by default
9+
+ Fix: Install-MDDaemon - no longer fails when the daemon task already exists
10+
+ Fix: Invoke-MDDaemon - fails to use authentication for SMTP
11+
312
## 1.1.7 (2026-02-10)
413

514
+ Upd: Invoke-MDDaemon - implements `UseSSL` as configured
@@ -25,7 +34,7 @@
2534
## 0.1.1 (2019-02-09)
2635

2736
+ Fix: Bug in Install-MDDaemon causing errors during installation
28-
37+
2938
## 0.1.0 (2019-02-09)
3039

3140
+ New: Everything

MailDaemon/en-us/strings.psd1

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22
# Write-PSFMessage, Stop-PSFFunction or the PSFramework validation scriptblocks
33
@{
44
# General
5-
'General.ModuleMissing' = 'The MailDaemon module could not be found in sufficient version on: {0}. Terminating Execution. To install or update to the current version, use Install-MDDaemon or access the PSGallery directly using "Install-Module MailDaemon".'
5+
'General.ModuleMissing' = 'The MailDaemon module could not be found in sufficient version on: {0}. Terminating Execution. To install or update to the current version, use Install-MDDaemon or access the PSGallery directly using "Install-Module MailDaemon".'
66

77
# Invoke-MDDaemon
8-
'Invoke-MDDaemon.SendMail.Start' = '{0} - Sending Mail: "{1}" From {2} to {3}'
9-
'Invoke-MDDaemon.SendMail.Failed' = '{0} - Failed to send email!'
10-
'Invoke-MDDaemon.SendMail.Success' = '{0} - Email sent successfully'
11-
'Invoke-MDDaemon.ManageSuccessJob.Failed' = '{0} - Failed to move mail task to the "sent" folder!'
8+
'Invoke-MDDaemon.SendMail.Abandon' = '{0} - Abandoning email after failing to send it within the configured timespan ({1})' # $email.Taskname, $abandonThreshold
9+
'Invoke-MDDaemon.SendMail.Start' = '{0} - Sending Mail: "{1}" From {2} to {3}'
10+
'Invoke-MDDaemon.SendMail.Failed' = '{0} - Failed to send email!'
11+
'Invoke-MDDaemon.SendMail.Success' = '{0} - Email sent successfully'
12+
'Invoke-MDDaemon.ManageSuccessJob.Failed' = '{0} - Failed to move mail task to the "sent" folder!'
1213

1314
# Copy-Module
14-
'Copy-Module.ReceivingModule' = 'Receiving Module from {0}: {1}'
15-
'Copy-Module.ReceivingModule.Failed' = 'Failed to receive Module from {0}: {1}'
16-
'Copy-Module.InstallingModule' = 'Installing module {0} on: {1}'
15+
'Copy-Module.ReceivingModule' = 'Receiving Module from {0}: {1}'
16+
'Copy-Module.ReceivingModule.Failed' = 'Failed to receive Module from {0}: {1}'
17+
'Copy-Module.InstallingModule' = 'Installing module {0} on: {1}'
1718

1819
# Send-MDMail
19-
'Send-MDMail.Folder.CreationFailed' = 'Failed to create outgoing mail folder: {0}'
20-
'Send-MDMail.Email.NotRegisteredYet' = 'No email has been set up yet!'
21-
'Send-MDMail.Email.Sending' = 'Queueing email for sending: {0}'
22-
'Send-MDMail.Email.SendingFailed' = 'Failed to queue email for sending: {0}'
23-
'Send-MDMail.Email.TriggerFailed' = 'Failed to trigger the Mail Daemon task to send {0}'
20+
'Send-MDMail.Folder.CreationFailed' = 'Failed to create outgoing mail folder: {0}'
21+
'Send-MDMail.Email.NotRegisteredYet' = 'No email has been set up yet!'
22+
'Send-MDMail.Email.Sending' = 'Queueing email for sending: {0}'
23+
'Send-MDMail.Email.SendingFailed' = 'Failed to queue email for sending: {0}'
24+
'Send-MDMail.Email.TriggerFailed' = 'Failed to trigger the Mail Daemon task to send {0}'
2425

2526
# Set-MDDaemon
26-
'Set-MDDaemon.UpdatingSettings' = 'Starting Daemon configuration update on {0}'
27-
'Set-MDDaemon.UpdateSetting' = 'Updating the Daemon configuration setting {0} to {1}'
27+
'Set-MDDaemon.UpdatingSettings' = 'Starting Daemon configuration update on {0}'
28+
'Set-MDDaemon.UpdateSetting' = 'Updating the Daemon configuration setting {0} to {1}'
2829

2930
# Update-MDFolderPermission
3031
'Update-MDFolderPermission.Granting.DaemonUser' = 'Assigning write permissions as daemon account to {0} on "{1}" and "{2}"'

MailDaemon/functions/Install-MDDaemon.ps1

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
3131
.PARAMETER SentPath
3232
The folder in which emails that were successfully sent are stored for a specified time before being deleted.
33+
34+
.PARAMETER FailedPath
35+
The path where mails that could repeatedly not be sent are moved to.
3336
3437
.PARAMETER DaemonUser
3538
The user to grant permissions needed to function as the Daemon account.
@@ -41,6 +44,12 @@
4144
4245
.PARAMETER MailSentRetention
4346
The time to keep successfully sent emails around.
47+
48+
.PARAMETER MailAbandonThreshold
49+
How long we attempt to send an email before abandoning it and moving it to -FailedPath.
50+
51+
.PARAMETER MailFailedRetention
52+
How long we keep an abandoned email around before removing it entirely.
4453
4554
.PARAMETER SmtpServer
4655
The mailserver to use for sending emails.
@@ -59,6 +68,10 @@
5968
.PARAMETER UseSSL
6069
Use SSL for sending emails.
6170
71+
.PARAMETER NoLogging
72+
Disables logging.
73+
Unless specified, this setup step will also prepare the windows eventlog by creating a dedicated eventlog for MailDaemon.
74+
6275
.EXAMPLE
6376
PS C:\> Install-MDDaemon -ComputerName DC1, DC2, DC3 -TaskUser $cred -DaemonUser "DOMAIN\MailDaemon" -SmtpServer 'mail.domain.org' -SenderDefault 'daemon@domain.org' -RecipientDefault 'helpdesk-t2@domain.org'
6477
@@ -85,6 +98,9 @@
8598

8699
[string]
87100
$SentPath,
101+
102+
[string]
103+
$FailedPath,
88104

89105
[string]
90106
$DaemonUser,
@@ -94,6 +110,12 @@
94110

95111
[Timespan]
96112
$MailSentRetention,
113+
114+
[Timespan]
115+
$MailAbandonThreshold,
116+
117+
[Timespan]
118+
$MailFailedRetention,
97119

98120
[string]
99121
$SmtpServer,
@@ -108,7 +130,10 @@
108130
$RecipientDefault,
109131

110132
[switch]
111-
$UseSSL
133+
$UseSSL,
134+
135+
[switch]
136+
$NoLogging
112137
)
113138

114139
begin {
@@ -161,6 +186,7 @@
161186
#region Setup Task Configuration
162187
if (-not $NoTask) {
163188
$action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -Command Invoke-MDDaemon"
189+
if ($NoLogging) { $action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -Command Invoke-MDDaemon -NoLogging" }
164190
$triggers = @()
165191
$triggers += New-ScheduledTaskTrigger -AtStartup -RandomDelay "00:15:00"
166192
$triggers += New-ScheduledTaskTrigger -At "00:00:00" -Daily
@@ -184,7 +210,7 @@
184210
#endregion Setup Task Configuration
185211

186212
#region Preparing Parameters
187-
$parameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include 'PickupPath', 'SentPath', 'MailSentRetention', 'SmtpServer', 'SenderDefault', 'RecipientDefault', 'UseSSL'
213+
$parameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include 'PickupPath', 'SentPath', 'FailedPath', 'MailSentRetention', 'MailAbandonThreshold', 'MailFailedRetention', 'SmtpServer', 'SenderDefault', 'RecipientDefault', 'UseSSL'
188214

189215
$paramMainInstallCall = @{
190216
ArgumentList = $parameters
@@ -246,21 +272,35 @@
246272
if ($TaskUser) { $parametersSave['AccessAccount'] = $TaskUser }
247273
Save-MDCredential @parametersSave
248274

249-
$parametersInvoke = @{ $parametersInvoke['ComputerName'] = $ComputerName }
275+
$parametersInvoke = @{ ComputerName = $ComputerName }
276+
if ($Credential) { $parametersInvoke['Credential'] = $Credential }
250277
Invoke-PSFCommand @parametersInvoke -ScriptBlock {
251278
Set-MDDaemon -SenderCredentialPath "C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml"
252279
}
253280
}
254281
#endregion Securely store credentials
282+
283+
#region Setup Logging
284+
if (-not $NoLogging) {
285+
Invoke-PSFCommand @parametersInvoke -ScriptBlock {
286+
if ($PSVersionTable.PSVersion.Major -gt 5 -and -not $IsWindows) { return }
287+
Set-PSFLoggingProvider -Name eventlog -InstanceName MailDaemonInvoke -LogName MailDaemon -Source MailDaemon -Enabled $true -Wait
288+
Write-PSFMessage -Message "Setting up MailDaemon logging"
289+
Disable-PSFLoggingProvider -Name eventlog -InstanceName MailDaemonInvoke
290+
}
291+
}
292+
#endregion Setup Logging
255293

256294
#region Setup Task
257295
if (-not $NoTask) {
258-
foreach ($computerObject in $ComputerName) {
259-
if ($ComputerName.Type -like 'CimSession') { $parametersRegister["CimSession"] = $computerObject.InputObject }
260-
elseif (-not $ComputerName.IsLocalhost) { $parametersRegister["CimSession"] = $ComputerName }
261-
262-
$null = Register-ScheduledTask @parametersRegister
263-
}
296+
Invoke-PSFCommand @parametersInvoke -ScriptBlock {
297+
param ($ParametersRegister)
298+
299+
$taskObject = Get-ScheduledTask -TaskName $ParametersRegister.TaskName -ErrorAction Ignore
300+
if ($taskObject) { $taskObject | Unregister-ScheduledTask }
301+
302+
$null = Register-ScheduledTask @ParametersRegister
303+
} -ArgumentList $parametersRegister
264304
}
265305
#endregion Setup Task
266306
}

0 commit comments

Comments
 (0)