Send email with optional attachments


PowerShelll script for a function to send email with optional attachments. It accepts a comma-separated list of attachments, or no attachments at all.

For dot-source inclusion to work if the script is hosted on a UNC path, you may need to add the url "file:\\unc-path-to-share" to the Internet Explorer local Intranet site list on the server running the script.






PS> .\Send-Email.ps1
Send-Email -EmailTo "" -EmailSubject "Log files attached" -EmailBody "Please find attached the log files" -EmailAttachments "C:\logs\Logfile.txt","C:\logs\Logfile2.txt"

This command sends an email with 2 attachments


PS> .\Send-Email.ps1
$EmailRecipients = @("","","")
Send-Email -EmailTo $EmailRecipients -EmailSubject "Test message" -EmailBody "Hello"

These commands send an email to multiple recipients


PS> .\Send-Email.ps1
Send-Email -EmailTo "" -EmailFrom "" -EmailSMTPServer "" -EmailSubject "Log file attached" -EmailBody "Please find attached the log file" -EmailAttachments "C:\logs\Logfile.txt"

This command shows all parameters



#        Script: Send-Email.ps1
#      Author: Dan Tonge,
#     Version:  1.0 2013-04-11
#     Version:  1.1 2015-06-01  Added multiple recipient syntax examples
#     Version:  1.2 2020-06-08  Added comment-based help for the script

Function Send-Email
            [parameter(Mandatory=$true,HelpMessage="Email To Address")] $EmailTo,
            [parameter(HelpMessage="Email From Address")] $EmailFrom = [String]$env:computerName.ToLower() + '',
            [parameter()] $EmailSMTPServer = '',
            [parameter(Mandatory=$true,HelpMessage="Email subject line")] $EmailSubject,
            [parameter(Mandatory=$true,HelpMessage="Email content")] $EmailBody,
            [parameter(HelpMessage="Optional Attachments")] $EmailAttachments
         ) #end Param

    # build a "splat" parameter: a hash of {parameter name, parameter value} to avoid passing the Attachments parameter when not needed
    $attachments = @()
        if ($EmailAttachments) 
                $attachments += $EmailAttachments

    $params = @{}
        if ($attachments.Length -gt 0)
                $params['Attachments'] = $attachments

    Send-MailMessage @params -To $EmailTo -From $EmailFrom -SmtpServer $EmailSMTPServer -Subject $EmailSubject -Body $EmailBody
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter