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 = "firstname.lastname@example.org" $to = "email@example.com" $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?