PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
45.59k stars 7.31k forks source link

Send-MailMessage: Encoding parameter default value specified in official documentation is incorrect #18808

Closed jeffrey-livongo closed 1 year ago

jeffrey-livongo commented 1 year ago

Prerequisites

Steps to reproduce

I tested this with the latest stable version of PowerShell 7.3.1 on Windows and Ubuntu. Using Send-MailMessage without specifying an encoding and including foreign characters in the email body causes the foreign characters to not be rendered correctly when viewing the email in Outlook. Specifying (Encoding = 'utf8NoBOM') as a parameter makes the email body appear correctly in Outlook. If 'utf8NoBOM' was actually the default value of the parameter, than the behaviour should be identical in both cases whereas it clearly is not. I recommend having someone fix the actual PowerShell code to make 'utf8NoBOM' the default encoding for this function in the next release of PowerShell, since imo 'utf8NoBOM' is a much better default value than ASCII or whatever the current function actually uses that causes foreign characters to render incorrectly.

Example 1 (incorrectly rendered email body): Outlook email body after the script runs: "This is a test email to J?ffr?y Ex?mple"

# no encoding parameter specified
$params = @{
    SmtpServer = 'smtp.office365.com'
    Port = 587
    From = $FromEmail
    To = $ToEmail
    Subject = 'PowerShell Script test'
    Body = 'This is a test email to Jéffréy Exámple'
    UseSsl = $True
    Credential = $Credential
}

[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Send-MailMessage @params -ErrorAction Stop

Example 2 (correctly rendered email body): Outlook email body after the script runs: "This is a test email to Jéffréy Exámple"

# encoding parameter specified as 'utf8NoBOM' which should already be the default according to the documentation
$params = @{
    SmtpServer = 'smtp.office365.com'
    Port = 587
    Encoding = 'utf8NoBOM'
    From = $FromEmail
    To = $ToEmail
    Subject = 'PowerShell Script test'
    Body = 'This is a test email to Jéffréy Exámple'
    UseSsl = $True
    Credential = $Credential
}

[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
Send-MailMessage @params -ErrorAction Stop

Expected behavior

Outlook email body after Example 2 runs: "This is a test email to Jéffréy Exámple"

Actual behavior

Outlook email body after Example 1 runs: "This is a test email to J?ffr?y Ex?mple"

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.1
PSEdition                      Core
GitCommitId                    7.3.1
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

turbedi commented 1 year ago

I have opened a PR to fix the docs, because according to this comment, there is a reason why the default encoding is ASCII.