Skip to content

Commit

Permalink
Improvements to logging
Browse files Browse the repository at this point in the history
- added LogConsole
- added LogObject
Improvements to Troublshooting
- added LocalDomain parameter
  • Loading branch information
PrzemyslawKlys committed Jan 22, 2022
1 parent cc1233b commit e36db5d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### Mailozaurr ChangeLog

#### - 0.0.23 - 2021.01.22
- Added option to `Send-EmailMessage` to specify the `LocalDomain` to be able to troubleshoot issue [#1314](https://github.com/jstedfast/MailKit/issues/1314)
- Added option to `Send-EmailMessage` - `LogConsole` and `LogObject` which joins `LogPath` in saving whole conversation to console, or to final object as Message property

#### - 0.0.22 - 2021.01.21
- Upgraded `MailKit\MimeKit` to 3.1.0
- Added support to save MimeMessage in `Send-EmailMessage` using `MimeMessagePath`
Expand Down
76 changes: 65 additions & 11 deletions Public/Send-EmailMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
.PARAMETER LogPath
When defined save the communication with server to file
.PARAMETER LogObject
When defined save the communication with server to object as message property
.PARAMETER LogConsole
When defined display the communication with server to console
.PARAMETER LogTimestamps
Configures whether log should use timestamps
Expand All @@ -135,6 +141,9 @@
.PARAMETER MimeMessagePath
Adds ability to save email message to file for troubleshooting purposes
.PARAMETER LocalDomain
Specifies the local domain name.
.EXAMPLE
if (-not $MailCredentials) {
$MailCredentials = Get-Credential
Expand Down Expand Up @@ -386,6 +395,16 @@
[Parameter(ParameterSetName = 'Compatibility')]
[string[]] $LogPath,

[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[switch] $LogConsole,

[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[switch] $LogObject,

[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
Expand Down Expand Up @@ -414,7 +433,12 @@
[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[string] $MimeMessagePath
[string] $MimeMessagePath,

[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[string] $LocalDomain
)
$StopWatch = [system.diagnostics.stopwatch]::StartNew()
if ($Email) {
Expand Down Expand Up @@ -588,9 +612,16 @@
$Message.Body = $BodyBuilder.ToMessageBody()

### SMTP Part Below

if ($LogPath) {
$ProtocolLogger = [MailKit.ProtocolLogger]::new($LogPath)
$OutputMessage = $null
if ($LogPath -or $LogConsole -or $LogObject) {
if ($LogPath) {
$ProtocolLogger = [MailKit.ProtocolLogger]::new($LogPath)
} elseif ($LogConsole) {
$ProtocolLogger = [MailKit.ProtocolLogger]::new([System.Console]::OpenStandardOutput())
} else {
$Stream = [System.IO.MemoryStream]::new()
$ProtocolLogger = [MailKit.ProtocolLogger]::new($Stream)
}
$ProtocolLogger.LogTimestamps = $LogTimestamps.IsPresent
$ProtocolLogger.RedactSecrets = -not $LogSecrets.IsPresent
if ($LogTimeStampsFormat) {
Expand All @@ -606,7 +637,9 @@
} else {
$SmtpClient = [MySmtpClient]::new()
}

if ($LocalDomain) {
$SmtpClient.LocalDomain = $LocalDomain
}
if ($SkipCertificateRevocation) {
$SmtpClient.CheckCertificateRevocation = $false
}
Expand Down Expand Up @@ -641,12 +674,15 @@
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
Write-Warning "Send-EmailMessage - Possible issue: Port? ($Port was used), Using SSL? ($SecureSocketOptions was used). You can also try SkipCertificateValidation or SkipCertificateRevocation. "
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand All @@ -665,12 +701,15 @@
} else {
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand All @@ -690,12 +729,15 @@
} else {
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand All @@ -714,12 +756,15 @@
} else {
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand All @@ -733,6 +778,9 @@
if ($PSCmdlet.ShouldProcess("$MailSentTo", 'Send-EmailMessage')) {
$OutputMessage = $SmtpClient.Send($Message)
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
[PSCustomObject] @{
Status = $True
Error = ''
Expand All @@ -746,12 +794,15 @@
}
} else {
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
[PSCustomObject] @{
Status = $false
Error = 'Email not sent (WhatIf)'
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand All @@ -766,12 +817,15 @@
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
}
if (-not $Suppress) {
if ($LogObject) {
$OutputMessage = [System.Text.Encoding]::ASCII.GetString($stream.ToArray());
}
[PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
Expand Down

0 comments on commit e36db5d

Please sign in to comment.