Skip to main content
Home
badllama.com
  • Search
  • Log In

Powershell automate send mail with attachment

psnyder's picture

Wed, 01/22/2014 - 06:50 by psnyder

If you require the need in Powershell (if you must) to send an automated report and attach it with priority, try this thinned out script for sending your message with an attachment. Note: The email address must be encapsulated in angled brackets

#Prints contents of file to the body of the email
$report = Get-content c:\your-outfile.txt | Out-string
[string[]]$recipients = "User  user@mail.com , Second Recipient second@mail.com"

#Create and add attachment
Get-ChildItem "Path\to\attachment" | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from "user@badllama.com" `
            -to "$recipients" `
            -subject "Subject Here" `
            -body "$report" `
            -BodyAsHtml `
            -priority  High `
            -dno onSuccess, onFailure `
            -smtpServer  relay.yoursmtpbox.com
Tags: 
Powershell
smtp
scripting
attachment
Powered by Backdrop CMS