system-email.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
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
#
# system_email.ps1
#

function Get-SitecoreEmailParameters
{
    Add-Type -Assembly  Sitecore.Kernel

    $mailServer = [Sitecore.Configuration.Settings]::GetSetting("MailServer")
    $mailServerUserName = [Sitecore.Configuration.Settings]::GetSetting("MailServerUserName")
    $mailServerPassword = [Sitecore.Configuration.Settings]::GetSetting("MailServerPassword")
    $mailServerPort = [Sitecore.Configuration.Settings]::GetSetting("MailServerPort")

    return @{
        SmtpServer = $mailServer
        SmtpPort = $mailServerPort
        SmtpUsername = $mailServerUserName
        SmtpPassword = new-object SecureString($mailServerPassword)
        UseSsl = $false
        From = ""
        To = ""
        Bcc = ""
        Subject = ""
        Body = ""      
    }
}



function Get-EmptyEmailParameters
{
  return @{
    SmtpServer = ""
    SmtpPort = ""
    SmtpUsername = ""
    SmtpPassword = new-object SecureString
    UseSsl = $false
    From = ""
    To = ""
    Bcc = ""
    Subject = ""
    Body = ""      
    }
}


function Send-Email
{
    <#
 .Example
 $params = Get-EmailParameters
 
 $params.SmtpServer = "localhost"
 $params.From = "powershell@domain.com"
 $params.To = "me@domain.com"
 $params.Subject = "Test"
 $params.Body = "Test"
 
 Send-Email @params -Verbose
 
 #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
        [string]$SmtpServer,
        [parameter()]
        [string]$SmtpPort = 25,
        [parameter()]
        [string]$SmtpUsername,
        [parameter()]
        [Security.SecureString]$SmtpPassword,
        [parameter()]
        [bool]$UseSsl = $false,
        [parameter(Mandatory=$true)]
        [string]$From,
        [parameter(Mandatory=$true)]
        [string]$To,
        [parameter()]
        [string]$Bcc,
        [parameter(Mandatory=$true)]
        [string]$Subject,
        [parameter(Mandatory=$true)]
        [string]$Body
    )

    Write-Verbose "Server: $SmtpServer : $SmtpPort"
    $smtp = new-object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
    $smtp.EnableSsl = $UseSsl 

    if( $SmtpUsername -ne '' -and $SmtpPassword.Length -ne 0)
    {
        $smtp.Credentials = new-object Net.NetworkCredential($SmtpUsername, $SmtpPassword)
    }

    $msg = new-object Net.Mail.MailMessage
    $msg.From = $From

    $recipients = $To.Split(";")
    foreach( $recipient in $recipients)
    {
        $msg.To.Add($recipient)
    }
    
    
    if( $Bcc -ne '' )
    {
        $msg.Bcc.Add($Bcc)
    }

    $msg.Subject = $Subject
    $msg.Body = $Body
    $smtp.Send($msg)
}

Export-ModuleMember -Function Send-Email
Export-ModuleMember -Function Get-EmptyEmailParameters
Export-ModuleMember -function Get-SitecoreEmailParameters