-
Notifications
You must be signed in to change notification settings - Fork 106
/
EmailFunction.ps1
73 lines (73 loc) · 1.53 KB
/
EmailFunction.ps1
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
<#
.NOTES
===========================================================================
Created on: 12/14/2016 12:55 PM
Created by: Vikas Sukhija
Organization:
Filename:
===========================================================================
.DESCRIPTION
Send-Email function for PS2.0
Example:
-subject "Test Email Function" -body "Test" -smtpserver smtpserver -cc "[email protected]","[email protected]"
-bcc "[email protected]","[email protected]" -attachment "c:\attach.txt"
#>
function Send-Email
{
[CmdletBinding()]
param
(
$From,
[array]$To,
[array]$bcc,
[array]$cc,
$body,
$subject,
$attachment,
$smtpserver
)
$message = new-object System.Net.Mail.MailMessage
$message.From = $from
if ($To -ne $null)
{
$To | ForEach-Object{
$to1 = $_
$to1
$message.To.Add($to1)
}
}
if ($cc -ne $null)
{
$cc | ForEach-Object{
$cc1 = $_
$cc1
$message.CC.Add($cc1)
}
}
if ($bcc -ne $null)
{
$bcc | ForEach-Object{
$bcc1 = $_
$bcc1
$message.bcc.Add($bcc1)
}
}
$message.IsBodyHtml = $True
if ($subject -ne $null)
{
$message.Subject = $Subject
}
if ($attachment -ne $null)
{
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
}
if ($body -ne $null)
{
$message.body = $body
}
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
}
##################################################################################