Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding not working when email send by Powershell

Im sending a testemail via powershell like

$messageParameters = @{
    Subject    = "Email Tool"
    Body       = Get-Content "C:\body.txt" | out-string
    From       = "Info <[email protected]>"
    To         = "Me <[email protected]>"
    SmtpServer = "mail.xy.de"
    Encoding   = New-Object System.Text.UTF8Encoding
}

send-mailmessage @messageParameters -BodyAsHtml

everything is working find except the encoding.

if i don't use encoding some characters are send as ??

and if i use it, what i actually want to do, than i get this Ä Ö Ü

but it should be ä ö ü and not this above.

If i don't send the mail as HTML it works.

How can i send the mail with the right encoding AND as html ?

like image 710
Dwza Avatar asked Sep 11 '26 03:09

Dwza


1 Answers

I believe the problem is that your text-file is getting jumbled when you are reading it into a variable as non-utf8.

I would try getting the text file as UTF-8 and keeping the Encoding line.

Body = Get-Content "C:\body.txt" -Encoding UTF8 | Out-String

EDIT: Added Out-String per Dwza.

like image 70
Jim Avatar answered Sep 15 '26 01:09

Jim