0

I am using this power-shell script to send emails. My problem is with the $body variable. I don't want to attach the log file in the email, I want the contents of the log file to be in the body of the email.

Write-Host "Setting Variables" -ForegroundColor Green
    $user    = "username"
    $pass    = Get-Content "$PSScriptRoot\pass.txt" | ConvertTo-SecureString
    $cred    = New-Object -Typename System.Management.Automation.PSCredential -argumentlist $user,$pass
    $from    = "sender@domain.net"
    $to      = "recipient@domain.net"
    $subject = "Email Subject"
    $body    = Get-Content C:\backup\log.txt
    $smtp    = "smtp.gmail.com"

    Write-Host "Sending Email" -ForegroundColor Green
    Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtp -Port 587 -UseSsl -Credential $cred

When I run the script I get a message saying:

Send-MailMessage : Cannot convert 'System.Object[]' to the type 
'System.String' required by parameter 'Body'. Specified method is not 
supported.

How can I read the contents of the log file and put it in the body of the email?

  • What do you get in powershell when issuing Get-Content C:\backup\log.txt? Maybe an encoding problem? Did you try casting to [string]? – LotPings May 17 '17 at 18:51
2

this solved it

-Body ($body | Out-String)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.