Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Nov 17, 2019
1 parent b442dcf commit f405464
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
4 changes: 2 additions & 2 deletions PSSharedGoods.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Przemyslaw Klys
#
# Generated on: 11.11.2019
# Generated on: 17.11.2019
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = 'PSSharedGoods.psm1'

# Version number of this module.
ModuleVersion = '0.0.106'
ModuleVersion = '0.0.107'

# Supported PSEditions
CompatiblePSEditions = 'Desktop', 'Core'
Expand Down
67 changes: 43 additions & 24 deletions Public/Email/Send-Email.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ function Send-Email {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[alias('EmailParameters')][System.Collections.IDictionary] $Email,
[string] $Body = "",
[string] $Body,
[string[]] $Attachment,
[System.Collections.IDictionary] $InlineAttachments,
[string] $Subject = "",
[string] $Subject,
[string[]] $To,
[PSCustomObject] $Logger
)
Expand All @@ -28,25 +28,30 @@ function Send-Email {
EmailServerLogin = $Email.Login
EmailServerEnableSSL = $Email.EnableSsl
EmailEncoding = $Email.Encoding
EmailEncodingSubject = $Email.EncodingSubject
EmailEncodingBody = $Email.EncodingBody
EmailSubject = $Email.Subject
EmailPriority = $Email.Priority
EmailDeliveryNotifications = $Email.DeliveryNotifications
EmailUseDefaultCredentials = $Email.UseDefaultCredentials
# EmailAlternativeClient = $Email.AlternativeClient
}
}
} catch {
return @{
Status = $False
Error = $($_.Exception.Message)
SentTo = ""
SentTo = ''
}
}
$SmtpClient = New-Object -TypeName System.Net.Mail.SmtpClient
$SmtpClient = [System.Net.Mail.SmtpClient]::new()
if ($EmailParameters.EmailServer) {
$SmtpClient.Host = $EmailParameters.EmailServer
} else {
return @{
Status = $False
Error = "Email Server Host is not set."
SentTo = ""
SentTo = ''
}
}
# Adding parameters to login to server
Expand All @@ -56,11 +61,11 @@ function Send-Email {
return @{
Status = $False
Error = "Email Server Port is not set."
SentTo = ""
SentTo = ''
}
}

if ($EmailParameters.EmailServerLogin -ne '') {
if ($EmailParameters.EmailServerLogin) {

$Credentials = Request-Credentials -UserName $EmailParameters.EmailServerLogin `
-Password $EmailParameters.EmailServerPassword `
Expand All @@ -69,9 +74,10 @@ function Send-Email {
-NetworkCredentials #-Verbose
$SmtpClient.Credentials = $Credentials
}

$SmtpClient.EnableSsl = $EmailParameters.EmailServerEnableSSL
$MailMessage = New-Object -TypeName System.Net.Mail.MailMessage
if ($EmailParameters.EmailServerEnableSSL) {
$SmtpClient.EnableSsl = $EmailParameters.EmailServerEnableSSL
}
$MailMessage = [System.Net.Mail.MailMessage]::new()
$MailMessage.From = $EmailParameters.EmailFrom
if ($To) {
foreach ($T in $To) { $MailMessage.To.add($($T)) }
Expand All @@ -80,17 +86,14 @@ function Send-Email {
foreach ($To in $EmailParameters.Emailto) { $MailMessage.To.add($($To)) }
}
}
if ($EmailParameters.EmailCC -ne "") {
if ($EmailParameters.EmailCC) {
foreach ($CC in $EmailParameters.EmailCC) { $MailMessage.CC.add($($CC)) }
}
if ($EmailParameters.EmailBCC -ne "") {
if ($EmailParameters.EmailBCC) {
foreach ($BCC in $EmailParameters.EmailBCC) { $MailMessage.BCC.add($($BCC)) }
}
$Exists = Test-Key $EmailParameters "EmailParameters" "EmailReplyTo" -DisplayProgress $false
if ($Exists -eq $true) {
if ($EmailParameters.EmailReplyTo -ne "") {
$MailMessage.ReplyTo = $EmailParameters.EmailReplyTo
}
if ($EmailParameters.EmailReplyTo) {
$MailMessage.ReplyTo = $EmailParameters.EmailReplyTo
}
$MailMessage.IsBodyHtml = $true
if ($Subject -eq '') {
Expand All @@ -102,8 +105,22 @@ function Send-Email {
$MailMessage.Priority = [System.Net.Mail.MailPriority]::$($EmailParameters.EmailPriority)

# Encoding
$MailMessage.BodyEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncoding)
$MailMessage.SubjectEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncoding)
if ($EmailParameters.EmailEncodingSubject) {
$MailMessage.SubjectEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncodingSubject)
} else {
$MailMessage.SubjectEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncoding)
}
if ($EmailParameters.EmailEncodingBody) {
$MailMessage.BodyEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncodingBody)
} else {
$MailMessage.BodyEncoding = [System.Text.Encoding]::$($EmailParameters.EmailEncoding)
}
if ($EmailParameters.EmailUseDefaultCredentials) {
$SmtpClient.UseDefaultCredentials = $EmailParameters.EmailUseDefaultCredentials
}
if ($EmailParameters.EmailDeliveryNotifications) {
$MailMessage.DeliveryNotificationOptions = $EmailParameters.EmailDeliveryNotifications
}

# Inlining attachment (s)
if ($PSBoundParameters.ContainsKey('InlineAttachments')) {
Expand All @@ -120,12 +137,14 @@ function Send-Email {
Invoke-WebRequest -Uri $Entry.Value -OutFile $FilePath
}
$ContentType = Get-MimeType -FileName $FilePath
$InAttachment = New-Object Net.Mail.LinkedResource($FilePath, $ContentType )
$InAttachment = [Net.Mail.LinkedResource]::new($FilePath, $ContentType )
$InAttachment.ContentId = $Entry.Key
$BodyPart.LinkedResources.Add( $InAttachment )
} catch {
$MailMessage.Dispose()
throw
#$MailMessage.Dispose()
#throw
$ErrorMessage = $_.Exception.Message -replace "`n", " " -replace "`r", " "
Write-Error "Error inlining attachments: $ErrorMessage"
}
}
} else {
Expand All @@ -137,8 +156,8 @@ function Send-Email {
foreach ($Attach in $Attachment) {
if (Test-Path -LiteralPath $Attach) {
try {
$File = New-Object Net.Mail.Attachment($Attach)
Write-Verbose "Send-Email - Attaching file $Attach"
$File = [Net.Mail.Attachment]::new($Attach)
#Write-Verbose "Send-Email - Attaching file $Attach"
$MailMessage.Attachments.Add($File)
} catch {
# non critical error
Expand Down
7 changes: 4 additions & 3 deletions Public/Email/Set-EmailBodyTableReplacement.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
function Set-EmailBodyReplacementTable {
[CmdletBinding()]
[alias('Set-EmailBodyTableReplacement')]
param (
$Body,
$TableName,
$TableData
[string] $Body,
[string] $TableName,
[Array] $TableData
)
$TableData = $TableData | ConvertTo-Html -Fragment | Out-String
$Body = $Body -replace "<<$TableName>>", $TableData
Expand Down
3 changes: 2 additions & 1 deletion Public/Objects/Get-ObjectProperties.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ $Test = Get-Process
Get-ObjectProperties -Object $Test
#>
function Get-ObjectProperties {
[CmdletBinding()]
param (
[object] $Object,
[System.Collections.ICollection] $Object,
[string[]] $AddProperties, # provides ability to add some custom properties
[switch] $Sort,
[bool] $RequireUnique = $true
Expand Down

0 comments on commit f405464

Please sign in to comment.