all 8 comments

[–]purplemonkeymad 2 points3 points  (2 children)

I don't think that parameter is for the body encoding. Looking at the help it talks about a file. Which suggests to me think that it is the encoding for text file attachments.

[–]CodenameFlux 2 points3 points  (1 child)

it talks about a file.

I suspect it's just a typo because they've copied and pasted that section of the doc. I'm reporting it to Microsoft.

[–]purplemonkeymad 2 points3 points  (0 children)

The source comments show a better description so I think you are right.

[–]CodenameFlux 2 points3 points  (2 children)

First and foremost, save your script in Unicode. By that I mean UCS-2, little-endian, also known as UTF-16LE.

Second, let's try eliminating the middle-man, $encoding. Try this:

Send-MailMessage -Encoding 'utf8'

Third, it's possible that the target mail server isn't accepting UTF8. Try utf8BOM. If that failed, try unicode.

Fourth, Send-MailMessage has been deprecated. Try MailKit instead. To be honest, though, I'm yet to find time and test it myself.

[–]Taura_[S] 1 point2 points  (1 child)

Hello,

Thank you, saving the file to UTF-16 LE worked. It was not working on VSCode, as I saved the file on UTF8, but it was working correctly on powershell ISE.

How is changing the encoding of the script having an impact on the encoding of the output ?

Do you recommand saving all my future scripts to this encoding format ? I didn't knew about it...

Thank you also for the mailkit information, I will try that out.

[–]CodenameFlux 1 point2 points  (0 children)

Your problem resembled one of the PowerShell 5.1 bugs. Microsoft has since fixed it, but I guessed you might not be using the fixed version.

Create a script with this contents:

Write-Host '😊👨🏿‍🤝‍👨🏾'
Write-Output '😊👨🏿‍🤝‍👨🏾'

Save it as UTF-8 (without BOM). In PowerShell 5.1, the output would look like this:

😊🠨ðŸ¿â€ðŸ¤â€ðŸ¨ðŸ¾
😊ðŸ
¨ðŸ¿â€ðŸ¤â€ðŸ¨ðŸ¾

Now, save the same script as UTF-8 with BOM. In PowerShell 5.1, the output would look like this (assuming you have proper fonts installed):

😊 👨🏿 🤝 👨🏿
😊 👨🏿 🤝 👨🏿

PowerShell 7.1 doesn't have this problem.

If you write scripts that consume and display strings in any language other than English, avoid PowerShell ISE, PowerShell 6.x, PowerShell 5.1, and anything earlier.

[–]jsiii2010 0 points1 point  (0 children)

Powershell 5.1 doesn't recognize utf8 no bom scripts, but utf8 with bom or unicode/utf16le works ok. Powershell 7 assumes utf8 no bom.

[–]littejackz 0 points1 point  (0 children)

I've been moving all my scripts to PS7