Sending email is a common task.. here is a simple but functional …Function 🙂
function sendMail($BODYemail,$SUBemail,$TOemail) {
Write-Host "Sending Email"
#SMTP server name
$smtpServer = "smtpserver.MYDOMAIN.COM"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
#Email structure
$msg.From = "FROMEMAIL@MYDOMAIN.COM"
$msg.ReplyTo = "REPLYTOEMAIL@MYDOMAIN.COM"
$msg.To.Add("$TOemail")
$msg.subject = "$SUBemail"
$msg.body = "$BODYemail"
$msg.IsBodyHTML = $true
#Sending email
$smtp.Send($msg)
}
Send some info
# To the User
$TOemail = "$OBJ_ContactPointEmail"
$BODYemail = $MSG_Header + $MSG_USERBODY + $MSG_Foot
$SUBemail = "Follow-up information on your request [$INVAR_SR]"
sendMail $BODYemail $SUBemail $TOemail
If ($INT_CountSAG -ne 0 )
{
$TOemail = "MYEMAIL@MYDOMAIN.COM"
$BODYemail = $SAGMessage
$SUBemail = "SAG Follow up"
sendMail $BODYemail $SUBemail $TOemail
}
If ($INT_CountErrors -ne 0 )
{
$TOemail = "MYEMAIL@MYDOMAIN.COM"
$BODYemail = $MSG_ERRORBODY + $MSG_SAGADMIN
$SUBemail = "Add users to groups errors"
sendMail $BODYemail $SUBemail $TOemail
}