{"id":67,"date":"2018-01-24T15:58:24","date_gmt":"2018-01-24T15:58:24","guid":{"rendered":"https:\/\/www.atlanta247.com\/blog\/?p=67"},"modified":"2018-02-20T16:27:51","modified_gmt":"2018-02-20T16:27:51","slug":"powershell-function-for-sending-email","status":"publish","type":"post","link":"https:\/\/www.atlanta247.com\/blog\/2018\/01\/24\/powershell-function-for-sending-email\/","title":{"rendered":"Powershell function for sending email"},"content":{"rendered":"<p>Sending email is a common task.. here is a simple but functional &#8230;Function \ud83d\ude42 <\/p>\n<p><code><br \/>\nfunction sendMail($BODYemail,$SUBemail,$TOemail) {<\/p>\n<p>     Write-Host \"Sending Email\"<\/p>\n<p>     #SMTP server name<br \/>\n     $smtpServer = \"smtpserver.MYDOMAIN.COM\"<\/p>\n<p>     #Creating a Mail object<br \/>\n     $msg = new-object Net.Mail.MailMessage<\/p>\n<p>     #Creating SMTP server object<br \/>\n     $smtp = new-object Net.Mail.SmtpClient($smtpServer)<\/p>\n<p>     #Email structure<br \/>\n     $msg.From = \"FROMEMAIL@MYDOMAIN.COM\"<br \/>\n     $msg.ReplyTo = \"REPLYTOEMAIL@MYDOMAIN.COM\"<br \/>\n     $msg.To.Add(\"$TOemail\")<br \/>\n     $msg.subject = \"$SUBemail\"<br \/>\n     $msg.body = \"$BODYemail\"<br \/>\n\t $msg.IsBodyHTML = $true<\/p>\n<p>     #Sending email<br \/>\n     $smtp.Send($msg)<\/p>\n<p>}<\/p>\n<p>Send some info<\/p>\n<p># To the User<br \/>\n\t$TOemail = \"$OBJ_ContactPointEmail\"<br \/>\n\t$BODYemail = $MSG_Header + $MSG_USERBODY + $MSG_Foot<br \/>\n\t$SUBemail = \"Follow-up information on your request [$INVAR_SR]\"<\/p>\n<p>\tsendMail $BODYemail $SUBemail $TOemail<\/p>\n<p>If ($INT_CountSAG -ne 0 )<br \/>\n\t{<br \/>\n\t$TOemail = \"MYEMAIL@MYDOMAIN.COM\"<br \/>\n\t$BODYemail = $SAGMessage<br \/>\n\t$SUBemail = \"SAG Follow up\"<br \/>\n\tsendMail $BODYemail $SUBemail $TOemail<br \/>\n\t}<\/p>\n<p>If ($INT_CountErrors -ne 0 )<br \/>\n\t{<br \/>\n\t$TOemail = \"MYEMAIL@MYDOMAIN.COM\"<br \/>\n\t$BODYemail = $MSG_ERRORBODY + $MSG_SAGADMIN<br \/>\n\t$SUBemail = \"Add users to groups errors\"<br \/>\n\tsendMail $BODYemail $SUBemail $TOemail<br \/>\n\t}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sending email is a common task.. here is a simple but functional &#8230;Function \ud83d\ude42 function sendMail($BODYemail,$SUBemail,$TOemail) { Write-Host &#8220;Sending Email&#8221; #SMTP server name $smtpServer = &#8220;smtpserver.MYDOMAIN.COM&#8221; #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 = &#8220;FROMEMAIL@MYDOMAIN.COM&#8221; $msg.ReplyTo = &#8220;REPLYTOEMAIL@MYDOMAIN.COM&#8221; $msg.To.Add(&#8220;$TOemail&#8221;) $msg.subject = &#8220;$SUBemail&#8221; $msg.body &hellip; <a href=\"https:\/\/www.atlanta247.com\/blog\/2018\/01\/24\/powershell-function-for-sending-email\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Powershell function for sending email<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-67","post","type-post","status-publish","format-standard","hentry","category-powershell"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":1,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions\/68"}],"wp:attachment":[{"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.atlanta247.com\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}