SendEmail.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
Function Send-Email
{
<#
 .Synopsis
  Send email notification
 
 .Description
  Send email notification using .Net System.Net.Mail namespace
 
 .Parameter -SMTPServer
  SMTP Server Address.
 
 .Parameter -Port
  SMTP Server Port.
 
 .Parameter -UseSSL
  Whether or not the SMTP serer requires SSL.
 
 .Parameter -AuthenticationMethod
  SMTP Server authentication method. possible values: Anonymous, Integrate or Credential
 
 .Parameter -Credential
  Specify a PSCredential object if SMTP server requires credentials (when AuthenticationMethod is set to Credential).
 
  .Parameter -SenderName
  Sender name for the email message
 
  .Parameter -SenderAddress
  Sender address for the email message
 
  .Parameter -SMTPSettings
  Alternatively, specify a hash table that contacts all of the SMTP settings above
 
  .Parameter -To
  "To" addresses for the email message. for multiple addresses, separate them using ";".
 
  .Parameter -Cc
  "Cc" addresses for the email message. for multiple addresses, separate them using ";".
 
  .Parameter -Bcc
  "Bcc" addresses for the email message. for multiple addresses, separate them using ";".
 
 .Parameter -Subject
  Subject text for the email message.
 
 .Parameter -Body
  Email body text.
 
 .Parameter -HTMLBody
  Whether or not the email body is HTML formatted.
 
  .Parameter -Attachments
  File path for each attachment for the message. for multiple attachments, separate them using ";".
 
 .Example
  # Send an email message by specifying each individual SMTP setting when SMTP supports anonymous autentication:
  $SMTPServer = "SMTP.yourcompany.com"
  $Port = 25
  $AuthMethod = "Anonymous"
  $UseSSL = $false
  $SenderName = "Your System Name"
  $SenderAddress = "your.system@yourcompany.com"
  $Recipient = "you@yourcompany.com"
  $Cc = "user1@yourcompany.com"
  $Bcc = "user2@yourcompany.com"
  $Subject = "Greeting from your Automated email script"
  $Body = @"
Hello,
 
this is a system generated message
 
Best Regards,
Your System
"@
  $HTMLBody = $false
  $SendEmail = Send-Email -SMTPServer $SMTPServer -Port $Port -UseSSL $UseSSL -AuthenticationMethod $AuthMethod -SenderName $SenderName -SenderAddress $SenderAddress -To $Recipient -Cc $Cc -Bcc $Bcc -Subject $Subject -Body $Body -HTMLBody $HTMLBody
     
 .Example
  # Send an email message by specifying each individual SMTP setting when SMTP requires a valid credential:
  $SMTPServer = "SMTP.yourcompany.com"
  $Port = 25
  $AuthMethod = "Credential"
  $Username = "YourDomain\YourUserName"
  $SecurePassword = ConvertTo-SecureString "password1234" -AsPlainText -Force
  $MyPSCred = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword)
  $UseSSL = $false
  $SenderName = "Your System Name"
  $SenderAddress = "your.system@yourcompany.com"
  $Recipient = "you@yourcompany.com"
  $Cc = "user1@yourcompany.com"
  $Bcc = "user2@yourcompany.com"
  $Subject = "Greeting from your Automated email script"
  $Body = @"
Hello,
 
this is a system generated message
 
Best Regards,
Your System
"@
  $HTMLBody = $false
  $SendEmail = Send-Email -SMTPServer $SMTPServer -Port $Port -UseSSL $UseSSL -AuthenticationMethod $AuthMethod -Credential $MyPSCred -SenderName $SenderName -SenderAddress $SenderAddress -To $Recipient -Cc $Cc -Bcc $Bcc -Subject $Subject -Body $Body -HTMLBody $HTMLBody
    
 .Example
  # Send an email message by passing a hashtable containing SMTP settings:
  $SMTPSettings = @{
                    "SMTPServer" = "SMTP.yourcompany.com"
                    "Port" = 25
                    "AuthenticationMethod" = "Credential"
                    "UserName" = "domain\username"
                    "Password" = "password1234"
                    "UseSSL" = $false
                    "SenderName" = "Your System Name"
                    "SenderAddress" = "your.system@yourcompany.com"
                   }
  $Recipient = "you@yourcompany.com"
  $Cc = "user1@yourcompany.com"
  $Bcc = "user2@yourcompany.com"
  $Subject = "Greeting from your Automated email script"
  $Body = @"
Hello,
 
this is a system generated message
 
Best Regards,
Your System
"@
  $HTMLBody = $false
  $SendEmail = Send-Email -SMTPSettings $SMTPSettings -To $Recipient -Cc $Cc -Bcc $Bcc -Subject $Subject -Body $Body -HTMLBody $HTMLBody
  
#>

    [CmdletBinding(DefaultParameterSetName='SMTPIndividualSettings')]
    Param (
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='Please specify the SMTP Server FQDN')][string]$SMTPServer,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='Please specify the SMTP Server Port')][Int32]$Port,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='Please specify if the SMTP Server requires SSL')][Boolean]$UseSSL,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='SMTP Server Authentication Method, Possible value: Anonymous, Integrated, Credential')][ValidateSet('Anonymous', 'Integrated', 'Credential')][string]$AuthenticationMethod,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$false,HelpMessage='Please specify a PSCredential object if the SMTP server requires valid credential')][ValidateScript({if ($AuthenticationMethod -ieq 'Credential'){$_}})][PSCredential]$Credential,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='Please specify the Sender Name')][string]$SenderName,
        [Parameter (ParameterSetName='SMTPIndividualSettings',Mandatory=$true,HelpMessage='Please specify the Sender Address')][string]$SenderAddress,
        [Parameter (ParameterSetName='SMTPHashTableSettings',Mandatory=$true,HelpMessage='Please specify the SMTP Server settings')][object]$SMTPSettings,
        [Parameter (Mandatory=$true,HelpMessage="Please specify the To Recipients address, separated by ';'")][string]$To,
        [Parameter (Mandatory=$False,HelpMessage="Please specify the Cc Recipients address, separated by ';'")][string]$Cc,
        [Parameter (Mandatory=$False,HelpMessage="Please specify the Bcc Recipients address, separated by ';'")][string]$Bcc,
        [Parameter (Mandatory=$true,HelpMessage='Please specify the email subject')][string]$Subject,
        [Parameter (Mandatory=$true,HelpMessage='Please specify the email subject')][string]$Body,
        [Parameter (Mandatory=$true,HelpMessage='Please specify if the email body is in HTML format')][Boolean]$HTMLBody,
        [Parameter (Mandatory=$false,HelpMessage="Please specify the paths of attachments, separated by ';'")][string]$Attachments
    )

    If ($SMTPSettings)
    {
        [String]$SMTPServer = $SMTPSettings.SMTPServer
        [Int32]$Port = $SMTPSettings.Port
        [Boolean]$UseSSL = $SMTPSettings.UseSSL
        [String]$AuthenticationMethod = $SMTPSettings.AuthenticationMethod
        [String]$UserName = $SMTPSettings.UserName
        [String]$Password = $SMTPSettings.Password
        [String]$SenderName = $SMTPSettings.SenderName
        [String]$SenderAddress = $SMTPSettings.SenderAddress
        If ($UserName -and $Password)
        {
            $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
            $Credential = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword)
        }
    }
    Write-Verbose 'SMTP Connection Settings:'
    Write-Verbose "SMTP Server: $SMTPServer"
    Write-Verbose "Port: $Port"
    Write-Verbose "UseSSL: $UseSSL"
    Write-Verbose "Authentication Method: $AuthenticationMethod"
    Write-Verbose "UserName: $UserName"
    Write-Verbose "SenderName: $SenderName"
    Write-Verbose "SenderAddress: $SenderAddress"

    $MailMessage = New-Object System.Net.Mail.MailMessage
    $MailMessage.IsBodyHtml = $HTMLBody
    $SMTPClient = New-Object System.Net.Mail.smtpClient
    $SMTPClient.host =  $SMTPServer
    $SMTPClient.port = $Port
    $SMTPClient.EnableSsl = $UseSSL
    if ($UseSSL)
    {
        $SMTPClient.EnableSSL = $true
    } elseif ($bSSL -ieq 'false') {
        $SMTPClient.EnableSSL = $false
    }

    #Get authentication method
    Switch ($AuthenticationMethod)
    {
        'Anonymous' {$SMTPClient.UseDefaultCredentials = $false}
        'Integrated' {$SMTPClient.UseDefaultCredentials = $true}
        'Credential' {
            #$SMTPClient.UseDefaultCredentials = $true
            $SMTPClient.Credentials = $Credential.GetNetworkCredential()
        }
    }
        $Sender = New-Object System.Net.Mail.MailAddress($SenderAddress, $SenderName)
        $MailMessage.Sender = $Sender
        $MailMessage.From = $Sender
    
    $MailMessage.Subject = $Subject
    $MailMessage.Body = $Body

    #Get all "To" Recipients
    $arrToRecipients = $To.split(';')
    Foreach ($item in $arrToRecipients)
    {
        $MailMessage.To.Add($item)
    }

    #Get all "Cc" Recipients
    if ($Cc)
    {
        $arrCcRecipients = $Cc.split(';')
        Foreach ($item in $arrCcRecipients)
        {
            $MailMessage.CC.Add($item)
        }    
    }

    #Get all "Bcc" Recipients
    if ($Bcc)
    {
        $arrBccRecipients = $Bcc.split(';')
        Foreach ($item in $arrBccRecipients)
        {
            $MailMessage.BCC.Add($item)
        }    
    }
    
    #Get all attachments
    if ($Attachments)
    {
        Write-Verbose "Adding attachments."
        $arrAttachments = $Attachments.split(';')
        Foreach ($item in $arrAttachments)
        {
            $MailMessage.Attachments.Add($item)
        }    
    }
    
    Write-Verbose "Sending email message."
    $SMTPClient.Send($MailMessage)
}