Skip to content

Commit

Permalink
Merge pull request #25 from EvotecIT/Update-mailkit/mimekit
Browse files Browse the repository at this point in the history
Update mailkit/mimekit
  • Loading branch information
PrzemyslawKlys authored Jan 21, 2022
2 parents 5aeaf9d + fb07afd commit 1f8ebf1
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 61 deletions.
10 changes: 10 additions & 0 deletions Examples/Example-SendEmail-01-Text.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Import-Module $PSScriptRoot\..\Mailozaurr.psd1 -Force

if (-not $MailCredentials) {
$MailCredentials = Get-Credential
}

$Text = Get-Content -Path "$PSScriptRoot\Input\Test.txt" -Raw

# this is simple replacement (drag & drop to Send-MailMessage)
Send-EmailMessage -To '[email protected]' -Subject 'Test' -Text $Text -SmtpServer 'smtp.office365.com' -From '[email protected]' -Priority High -Credential $MailCredentials -UseSsl -Port 587 -Verbose
3 changes: 3 additions & 0 deletions Examples/Input/Test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello
My Friend
What's up
Binary file modified Lib/Core/BouncyCastle.Crypto.dll
Binary file not shown.
Binary file modified Lib/Core/MailKit.dll
Binary file not shown.
Binary file modified Lib/Core/MimeKit.dll
Binary file not shown.
Binary file modified Lib/Default/BouncyCastle.Crypto.dll
Binary file not shown.
Binary file modified Lib/Default/MailKit.dll
Binary file not shown.
Binary file modified Lib/Default/MimeKit.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Mailozaurr.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Description = 'Mailozaurr is a PowerShell module that aims to provide SMTP, POP3, IMAP and few other ways to interact with Email. Underneath it uses MimeKit and MailKit and EmailValidation libraries written by Jeffrey Stedfast. '
FunctionsToExport = @('Connect-IMAP', 'Connect-oAuthGoogle', 'Connect-oAuthO365', 'Connect-POP', 'ConvertTo-GraphCredential', 'ConvertTo-OAuth2Credential', 'ConvertTo-SendGridCredential', 'Disconnect-IMAP', 'Disconnect-POP', 'Find-DKIMRecord', 'Find-DMARCRecord', 'Find-DNSBL', 'Find-MxRecord', 'Find-SPFRecord', 'Get-IMAPFolder', 'Get-IMAPMessage', 'Get-MailFolder', 'Get-MailMessage', 'Get-POPMessage', 'Resolve-DnsQuery', 'Resolve-DnsQueryRest', 'Save-MailMessage', 'Save-POPMessage', 'Send-EmailMessage', 'Test-EmailAddress')
GUID = '2b0ea9f1-3ff1-4300-b939-106d5da608fa'
ModuleVersion = '0.0.21'
ModuleVersion = '0.0.22'
PowerShellVersion = '5.1'
PrivateData = @{
PSData = @{
Expand Down
12 changes: 2 additions & 10 deletions Private/ConvertTo-MailboxAddress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
param(
[Array] $MailboxAddress
)
<#
MimeKit.MailboxAddress new(System.Text.Encoding encoding, string name, System.Collections.Generic.IEnumerable[string] route, string address)
MimeKit.MailboxAddress new(string name, System.Collections.Generic.IEnumerable[string] route, string address)
MimeKit.MailboxAddress new(System.Collections.Generic.IEnumerable[string] route, string address)
MimeKit.MailboxAddress new(System.Text.Encoding encoding, string name, string address)
MimeKit.MailboxAddress new(string name, string address)
MimeKit.MailboxAddress new(string address)
#>
foreach ($_ in $MailboxAddress) {
if ($_ -is [string]) {
$SmtpTo = [MimeKit.MailboxAddress]::new("$_")
$SmtpTo = [MimeKit.MailboxAddress]::new("$_", "$_")
} elseif ($_ -is [System.Collections.IDictionary]) {
$SmtpTo = [MimeKit.MailboxAddress]::new($_.Name, $_.Email)
} elseif ($_ -is [MimeKit.MailboxAddress]) {
Expand All @@ -22,7 +14,7 @@
if ($_.Name -and $_.Email) {
$SmtpTo = [MimeKit.MailboxAddress]::new($_.Name, $_.Email)
} elseif ($_.Email) {
$SmtpTo = [MimeKit.MailboxAddress]::new($_.Email)
$SmtpTo = [MimeKit.MailboxAddress]::new($_.Email, $_.Email)
}
}
$SmtpTo
Expand Down
32 changes: 20 additions & 12 deletions Private/Send-GraphMailMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function Send-GraphMailMessage {
[alias('Attachments')][string[]] $Attachment,
[PSCredential] $Credential,
[alias('Importance')][ValidateSet('Low', 'Normal', 'High')][string] $Priority,
[switch] $DoNotSaveToSentItems
[switch] $DoNotSaveToSentItems,
[System.Diagnostics.Stopwatch] $StopWatch
)
if ($Credential) {
$AuthorizationData = ConvertFrom-GraphCredential -Credential $Credential
Expand Down Expand Up @@ -87,18 +88,23 @@ function Send-GraphMailMessage {
$null = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users/$FromField/sendMail" -Headers $Authorization -Method POST -Body $Body -ContentType 'application/json; charset=UTF-8' -ErrorAction Stop
if (-not $Suppress) {
[PSCustomObject] @{
Status = $True
Error = ''
SentTo = $MailSentTo
SentFrom = $FromField
Status = $True
Error = ''
SentTo = $MailSentTo
SentFrom = $FromField
Message = ''
TimeToExecute = $StopWatch.Elapsed
}
}
} else {
if (-not $Suppress) {
[PSCustomObject] @{
Status = $false
Error = 'Email not sent (WhatIf)'
SentTo = $MailSentTo
Status = $false
Error = 'Email not sent (WhatIf)'
SentTo = $MailSentTo
SentFrom = $FromField
Message = ''
TimeToExecute = $StopWatch.Elapsed
}
}
}
Expand Down Expand Up @@ -127,10 +133,12 @@ function Send-GraphMailMessage {
}
if (-not $Suppress) {
[PSCustomObject] @{
Status = $False
Error = if ($RestError) { "$($RestMessage) $($ErrorText)" } else { $RestMessage }
SentTo = $MailSentTo
SentFrom = $FromField
Status = $False
Error = if ($RestError) { "$($RestMessage) $($ErrorText)" } else { $RestMessage }
SentTo = $MailSentTo
SentFrom = $FromField
Message = ''
TimeToExecute = $StopWatch.Elapsed
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions Private/Send-SendGridMailMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[alias('Attachments')][string[]] $Attachment,
[PSCredential] $Credential,
[alias('Importance')][ValidateSet('Low', 'Normal', 'High')][string] $Priority,
[switch] $SeparateTo
[switch] $SeparateTo,
[System.Diagnostics.Stopwatch] $StopWatch
)
# https://sendgrid.api-docs.io/v3.0/mail-send/v3-mail-send
if ($Credential) {
Expand Down Expand Up @@ -104,10 +105,12 @@
$null = Invoke-RestMethod @InvokeRestMethodParams
if (-not $Suppress) {
[PSCustomObject] @{
Status = $True
Error = ''
SentTo = $MailSentList
SentFrom = $SendGridMessage.From.Email
Status = $True
Error = ''
SentTo = $MailSentList
SentFrom = $SendGridMessage.From.Email
Message = ''
TimeToExecute = $StopWatch.Elapsed
}
}
}
Expand All @@ -126,10 +129,12 @@
}
if (-not $Suppress) {
[PSCustomObject] @{
Status = $False
Error = -join ( $($_.Exception.Message), $ErrorDetails)
SentTo = $MailSentTo
SentFrom = $SendGridMessage.From.Email
Status = $False
Error = -join ( $($_.Exception.Message), $ErrorDetails)
SentTo = $MailSentTo
SentFrom = $SendGridMessage.From.Email
Message = ''
TimeToExecute = $StopWatch.Elapsed
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Public/Resolve-DnsRequestRest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$Name = $Name -replace '^(\d+)\.(\d+)\.(\d+)\.(\d+)$', '$4.$3.$2.$1.in-addr.arpa'
}
if ($DNSProvider -eq 'Cloudflare') {
$Q = Invoke-RestMethod -Uri "https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=$Name&type=$Type"
$Q = Invoke-RestMethod -Uri "https://cloudflare-dns.com/dns-query?name=$Name&type=$Type" -Headers @{ accept = 'application/dns-json' }
} else {
$Q = Invoke-RestMethod -Uri "https://dns.google.com/resolve?name=$Name&type=$Type"
}
Expand Down
122 changes: 94 additions & 28 deletions Public/Send-EmailMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
.PARAMETER Graph
Send email via Office 365 Graph API
.PARAMETER AsSecureString
Informs command that password provided is secure string, rather than clear text
.PARAMETER SendGrid
Send email via SendGrid API
Expand All @@ -111,8 +114,26 @@
.PARAMETER Suppress
Do not display summary in [PSCustomObject]
.PARAMETER AsSecureString
Informs command that password provided is secure string, rather than clear text
.PARAMETER LogPath
When defined save the communication with server to file
.PARAMETER LogTimestamps
Configures whether log should use timestamps
.PARAMETER LogTimeStampsFormat
Configures the format of the timestamps in the log file.
.PARAMETER LogSecrets
Configures whether log should include secrets
.PARAMETER LogClientPrefix
Sets log prefix for client to specific value.
.PARAMETER LogServerPrefix
Sets log prefix for server to specific value.
.PARAMETER MimeMessagePath
Adds ability to save email message to file for troubleshooting purposes
.EXAMPLE
if (-not $MailCredentials) {
Expand Down Expand Up @@ -388,8 +409,14 @@
[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[string] $LogServerPrefix
[string] $LogServerPrefix,

[Parameter(ParameterSetName = 'SecureString')]
[Parameter(ParameterSetName = 'oAuth')]
[Parameter(ParameterSetName = 'Compatibility')]
[string] $MimeMessagePath
)
$StopWatch = [system.diagnostics.stopwatch]::StartNew()
if ($Email) {
# Following code makes sure both formats are accepted.
if ($Email.EmailTo) {
Expand Down Expand Up @@ -470,6 +497,7 @@
Priority = $Priority
ReplyTo = $ReplyTo
DoNotSaveToSentItems = $DoNotSaveToSentItems.IsPresent
StopWatch = $StopWatch
}
Remove-EmptyValue -Hashtable $sendGraphMailMessageSplat
return Send-GraphMailMessage @sendGraphMailMessageSplat
Expand All @@ -488,6 +516,7 @@
Priority = $Priority
ReplyTo = $ReplyTo
SeparateTo = $SeparateTo.IsPresent
StopWatch = $StopWatch
}
Remove-EmptyValue -Hashtable $sendGraphMailMessageSplat
return Send-SendGridMailMessage @sendGraphMailMessageSplat
Expand All @@ -507,8 +536,6 @@
#void Authenticate(string userName, string password, System.Threading.CancellationToken cancellationToken)
}



$Message = [MimeKit.MimeMessage]::new()

# Doing translation for compatibility with Send-MailMessage
Expand Down Expand Up @@ -562,7 +589,7 @@

### SMTP Part Below

if ($LogPath){
if ($LogPath) {
$ProtocolLogger = [MailKit.ProtocolLogger]::new($LogPath)
$ProtocolLogger.LogTimestamps = $LogTimestamps.IsPresent
$ProtocolLogger.RedactSecrets = -not $LogSecrets.IsPresent
Expand Down Expand Up @@ -615,9 +642,14 @@
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) {
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
Expand All @@ -634,9 +666,14 @@
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
Expand All @@ -654,9 +691,14 @@
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
Expand All @@ -673,9 +715,14 @@
Write-Warning "Send-EmailMessage - Error: $($_.Exception.Message)"
if (-not $Suppress) {
return [PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
Expand All @@ -684,20 +731,30 @@
$SmtpClient.Timeout = $Timeout
try {
if ($PSCmdlet.ShouldProcess("$MailSentTo", 'Send-EmailMessage')) {
$SmtpClient.Send($Message)
$OutputMessage = $SmtpClient.Send($Message)
if (-not $Suppress) {
[PSCustomObject] @{
Status = $True
Error = ''
SentTo = $MailSentTo
Status = $True
Error = ''
SentTo = $MailSentTo
SentFrom = $From
Message = $OutputMessage
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
} else {
if (-not $Suppress) {
[PSCustomObject] @{
Status = $false
Error = 'Email not sent (WhatIf)'
SentTo = $MailSentTo
Status = $false
Error = 'Email not sent (WhatIf)'
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
Expand All @@ -710,11 +767,20 @@
}
if (-not $Suppress) {
[PSCustomObject] @{
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
Status = $False
Error = $($_.Exception.Message)
SentTo = $MailSentTo
SentFrom = $From
Message = ''
TimeToExecute = $StopWatch.Elapsed
Server = $Server
Port = $Port
}
}
}
$SmtpClient.Disconnect($true)
if ($MimeMessagePath) {
$Message.WriteTo($MimeMessagePath)
}
$StopWatch.Stop()
}

0 comments on commit 1f8ebf1

Please sign in to comment.