Sending email using Powershell script
Below is a script that can be used to send email using Powershell.
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$currentDate = Get-Date -Format g
$HeaderOne
$HeaderTwo
$HeaderThree
$HeaderFour
$Outputreport = "<HTML><TITLE> Test E-Mail notification </TITLE> <head>
<style>body {font-family: Calibri;font-size: 14px;}table {border-collapse: collapse;}table, th, td {border: 1px solid black;font-family: Calibri,Verdana;font-size: 14px;}th {background-color: #4CAF50;color:white;}</style></head>
<BODY>
<H2> Test E-Mail notification on $($currentDate) CET </H2>
<Table cellpadding=5 cellspacing=0>
<TR>
<th>Header One</th>
<th>Header Two</th>
<th>Header Three</th>
<th>Header Four</th>
</TR>"
$Outputreport += "<TR>
<TD>$($HeaderOne)</TD>
<TD>$($HeaderTwo)</TD>
<TD>$($HeaderThree)</TD>
<TD>$($HeaderFour) in GB</TD>
</TR>"
$Outputreport += "</Table></BODY></HTML>"
$Outputreport | out-file E:\Scripts\Test.htm
Invoke-Expression E:\Scripts\Test.htm
$currentDate=Get-Date -Format g
$SendMail=$true
##Send email functionality from below line, use it if you want
if($SendMail -eq $true)
{
$smtpServer = "exchange-test.test.co.in"
$smtpFrom = "test_admin@test.com"
$smtpTo = "test@test.com,testuser@test.com"
$messageSubject = "Test E-Mail notification report on " + $currentDate
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content E:\Scripts\test.htm
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
}
Email notification will trigger as shown in the below format.
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$currentDate = Get-Date -Format g
$HeaderOne
$HeaderTwo
$HeaderThree
$HeaderFour
$Outputreport = "<HTML><TITLE> Test E-Mail notification </TITLE> <head>
<style>body {font-family: Calibri;font-size: 14px;}table {border-collapse: collapse;}table, th, td {border: 1px solid black;font-family: Calibri,Verdana;font-size: 14px;}th {background-color: #4CAF50;color:white;}</style></head>
<BODY>
<H2> Test E-Mail notification on $($currentDate) CET </H2>
<Table cellpadding=5 cellspacing=0>
<TR>
<th>Header One</th>
<th>Header Two</th>
<th>Header Three</th>
<th>Header Four</th>
</TR>"
$Outputreport += "<TR>
<TD>$($HeaderOne)</TD>
<TD>$($HeaderTwo)</TD>
<TD>$($HeaderThree)</TD>
<TD>$($HeaderFour) in GB</TD>
</TR>"
$Outputreport += "</Table></BODY></HTML>"
$Outputreport | out-file E:\Scripts\Test.htm
Invoke-Expression E:\Scripts\Test.htm
$currentDate=Get-Date -Format g
$SendMail=$true
##Send email functionality from below line, use it if you want
if($SendMail -eq $true)
{
$smtpServer = "exchange-test.test.co.in"
$smtpFrom = "test_admin@test.com"
$smtpTo = "test@test.com,testuser@test.com"
$messageSubject = "Test E-Mail notification report on " + $currentDate
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content E:\Scripts\test.htm
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
}
Email notification will trigger as shown in the below format.
Please post your queries if you any help on this.
Comments
Post a Comment