Powershell – Email Function

Many a times I have seen that admins are finding it difficult to send multiple emails using powershell version 1.0 or 2.0.

This is because they need to use multiple $message.To.Add

To make it super easy for legacy powershell version I am sharing the send-email function that you can embed in your scripts & than just call it as below example:

Send-Email -From “user2@labtest.com” -To “user1@labtest.com”,”user2@labtest.com” -subject “Test Email Function” -body “Test” -smtpserver smtpserver -cc “user1@labtest.com”,”user2@labtest.com” -bcc “user1@labtest.com”,”user2@labtest.com” -attachment “c:\attach.txt”

Download Link:

https://gallery.technet.microsoft.com/scriptcenter/Email-Function-2960b260

Let me know if you want to alter or add some thing to the function.

<#     
    .NOTES 
    =========================================================================== 
     Created on:       12/14/2016 12:55 PM 
     Created by:       Vikas Sukhija 
     Organization:      
     Filename:          
    =========================================================================== 
    .DESCRIPTION 
        Send-Email function for PS2.0 
        Example: 
        Send-Email -From "user2@labtest.com" -To "user1@labtest.com","user2@labtest.com"  
        -subject "Test Email Function" -body "Test" -smtpserver smtpserver -cc "user1@labtest.com","user2@labtest.com"  
        -bcc "user1@labtest.com","user2@labtest.com" -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) 
} 
##################################################################################

Thanks for reading and downloading

Tech Wizard

https://techwizard.cloud

https://syscloudpro.com/

 

Advertisement

2 thoughts on “Powershell – Email Function

  1. Hi Vikas, I really like your post on scripts. Do you have any script which can be used for logging the output in any other script ? I just need the functions for logging purposes so that I can use those functions in any other script.

    Regards Jai

    On Wed, Dec 14, 2016 at 8:16 PM, Microsoft Technologies Blog wrote:

    > Vikas Sukhija posted: “Many a times I have seen that admins are finding it > difficult to send multiple emails using powershell version 1.0 or 2.0. This > is because they need to use multiple $message.To.Add To make it super easy > for legacy powershell version I am sharing the” >

Leave a Reply to Jai Prakash Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s