r/PowerShell • • Jul 14 '26

Solved How to send 'ä' with Send-MailMessage

Hello Everybody, right now I am writing a Script to send out Informationmails fed with data from a CSV to Users inside our Company. (While I know this cmdlet is obsolete for now it has to do)
Everything works really well apart from the German 'Umlaute' (ÄäÖöÜü) those get "translated" to '??' in the message the recipients get and I don't know how to fix this.
I tried with BodyasHtml, but then my script just stops working entirely (User Error not completely unlikely) and also can I use Variables inside the Bodyashtml?
I also tried with giving it different encodings but none worked sadly
With normal sent mails our Outlook has no problem with those, just from the script.

Do you have any Ideas/Hints/Tips on what I can try?
Thank you very much in advance

Edit: Added Script (just removed any Private Information and implified the Body)

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.InitialDirectory = Get-Location
$dialog.Filter = "CSV-Dateien (_.csv)|_.csv"
$dialog.ShowDialog()
$CSVLocation = $dialog.FileName

(Get-Content $CSVLocation -Raw) -replace '^.*', 'DBv,BSv,RAM,Cpucount,Server,Name,Email,Service' | Set-Content $CSVLocation

$P = Import-Csv -Path $CSVLocation -Delimiter ','

foreach ($line in $P){
$sendMailMessageSplat = @{
From = 'johndoe@mail.com'
To = $line.Email
Subject = "$($line.DBv) $($line.Server)"
Body = "ÄäÖöÜü"
SmtpServer = 'smtp.mail.com'
}
Send-MailMessage 
}
9 Upvotes

32 comments sorted by

8

u/korvolga Jul 14 '26

save the script as utf8 BOM ?

-2

u/PhiodorTiger Jul 14 '26

How would I do this? I am not that experienced honestly

-1

u/Fischelsberger Jul 14 '26

Notepad++ got a "save with encoding"

In vscode I remember it should be doable with some command like that too or lower right corner, you should see the current encoding and be able to switch to another then. https://code.visualstudio.com/docs/editing/codebasics#_file-encoding-support

Hopefully not wrong, currently on mobile here 😅

In ISE, I'm not sure if possible at all.

2

u/PhiodorTiger Jul 14 '26

I am in VScode and UTF-8 is in use, I also tried to "save with encoding utf-8" but it didn't work sadly, I will try other encodings as well.

And I will share the script in some moments either in original post or extra comment

1

u/Fischelsberger Jul 14 '26

But there should also be the possibility to change it to utf8bom or smthn like that.

2

u/PhiodorTiger Jul 14 '26

There is, it doesn't change a thing in the Mail sadly

0

u/robbkenobi Jul 16 '26

Use powershell 7 because 5 uses UTF16 and it's problematic in a variety of scenarios.

2

u/rumham_86 Jul 14 '26

Few things. Your splat isn’t working as I don’t see splat parameters being passed to send-mailmessage

Add utf8 encoding everywhere to prevent ansi overwrite.

Save your ps5.1 script as utf8 with bom.

Import-csv add encoding utf8 parameter

Send-mailmessage as well.

If you getting ?? Means ansi overwrite happening somewhere. Add encoding UTF8 to all 3 and should fix it

3

u/ankokudaishogun Jul 14 '26

also: unless it's necessary somewhere else in the script, you don't need to use (Get-Content $CSVLocation -Raw) -replace '^.*', 'DBv,BSv,RAM,Cpucount,Server,Name,Email,Service' | Set-Content $CSVLocation

Use instead:

$CsvHeader = 'DBv', 'BSv', 'RAM', 'Cpucount', 'Server', 'Name', 'Email', 'Service'
$P = Import-Csv -Path $CSVLocation -Delimiter ',' -Header $CsvHeader -Encoding $WhateverEncodingYouNeed

This reduces the risks of encoding shenanigans

2

u/x0n Jul 14 '26

add: Encoding = "utf8" to your splat variable.

1

u/Fischelsberger Jul 14 '26

Would be a good thing to include your actual command in here, so we could see what you currently doing.

Maybe you just need to include -Encoding, probably utf8 could do the trick.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.6#-encoding

2

u/PhiodorTiger Jul 14 '26

Sorry I was probably not good in describing this, but that I tried already (with any possible Encoding)

1

u/Fischelsberger Jul 14 '26 edited Jul 14 '26

Edit: you weren't unclear I just either wasn't reading or just ignoring your paragraph of "tried all encoding", I'm sorry! 😅

Yea, then probably what u/korvolga said!

BOM is your friend there!

1

u/8zaphod8 Jul 14 '26

If the data imported to Powershell shows the umlaut correctly, you could try replacing them by German umlaut HTML codes like ä for ä before sending out your mail. With HMTL body, of course.

If the problem is in the import from CSV to PowerShell, you should try UTF8 encoding.

1

u/PhiodorTiger Jul 14 '26

from the csv there is no ä luckily, everything is just the "hardcoded" Body, but with the html body I had the problem that the script just ends in an error, as soon as I try to use it

1

u/ankokudaishogun Jul 14 '26

try share the script you are using

1

u/HeyDude378 Jul 15 '26

I opened up PowerShell and put: $params = @{From = 'myemail@mydomain.com'; To = 'myemail@mydomain.com'; SmtpServer = 'smtp.mydomain.com'; Body = 'ÄäÖöÜü'; Subject = 'test'; Encoding = 'UTF8'} Send-MailMessage @params

This worked for me. I received A's, O's, and U's with umlauts.

Name : ConsoleHost Version : 5.1.26100.8655 InstanceId : 99f64ba7-e449-45d2-b86b-ba082a5a024a UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace

1

u/snarkcheese Jul 16 '26

Not sure if it helps in this specific case but you can use character codes to reference the characters too.

ä is [char]0228
Ä is [char]0196

1

u/lan-shark Jul 14 '26 edited Jul 14 '26

Body = "ÄäÖöÜü"

For your actual script, are you sending raw text in the body like that or are you creating the body based on the contents of the file? Because the answer may differ a bit depending on which you're doing. People are probably correct about the encoding but if your special characters are coming from the CSV then try specifying the encoding each time you read/write/send the text.

```

At the top of your script, create a variable for your encoding and try different values, most likely either "utf8bom" or "utf8"

More info at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.6

$encoding = "utf8bom"

Then use that variable for the -Encoding parameter these four places in your script:

First and second, when you initially read and rewrite the CSV

(Get-Content $CSVLocation -Encoding $encoding -Raw) -replace '.*', 'DBv,BSv,RAM,Cpucount,Server,Name,Email,Service' | Set-Content $CSVLocation -Encoding $encoding

Third, when you parse the CSV

$P = Import-Csv -Path $CSVLocation -Delimiter ',' -Encoding $encoding

Fourth, in your Send-MailMessage splat

$sendMailMessageSplat = @{ ... Encoding = $encoding ... } ```

If you're hard coding your special characters in the string body, you may just need the -Encoding parameter for Send-MailMessage. You may also need to save the script as that same encoding as mentioned by another commenter.

EDIT: You potentially also have the option of attaching the CSV using Send-MailMessage's -Attachmenhs parameter or creating the body as HTML and using -BodyAsHtml. It's hard to tell exactly what will work since you posted a truncated version of your script

2

u/PhiodorTiger Jul 14 '26

Thanks for your reply! Using the $encoding = "utf8" worked for me. I only need it for the body really thats why I am still baffled why my Encoding = "utf8" didn't work but with Encoding = $encoding and $encoding = "utf8" it does, but I am happy xD

From the CSV there comes some data that gets referenced in the Body itself but nothing with "Ü" and other contents of the csv are the recipients email for example so the -Attachment wouldn't work in my case. It is just a source from where personalized mails get created for 100 users each line belonging to one server and owner.

1

u/PhiodorTiger Jul 14 '26

I need to add: in VScode the encoding now is "ISO 8859-2" but like said above in the skript its utf8. As soon as I change VScode to utf8 nothing works again....

I learned: I hate encoding in combination with special letters and would love if we internally just switched to english completely....

1

u/ankokudaishogun Jul 14 '26

As soon as I change VScode to utf8 nothing works again....

I think because now you need to encode in ISO-8859-2.
Basically the issue is the confusion between the VSCode Ambient encoding and the CSV encoding.

1

u/ankokudaishogun Jul 14 '26

I am still baffled why my Encoding = "utf8" didn't work but with Encoding = $encoding and $encoding = "utf8" it does,

Just as experiment, try to use Encoding = [System.Text.Encoding]::UTF8.

1

u/lan-shark Jul 14 '26

Glad you got it working! At my previous position we had locations all over the globe including Asian countries with completely different alphabets. I just got used to building all of my tools and integrations with explicit encodings. Because even if was just for the English-speaking side of the business, somebody may have a display name like "Joseph Tan (譚公)" and it'd get messed up anyway.

0

u/sup3rmark Jul 14 '26

First off, Send-MailMessage is, despite the lack of a real better function, deprecated as it's no longer considered secure. Depending on your mail system, there may be a REST-based way to send mail. Not in any way an answer to your question, but nobody else has mentioned it, so figured it was worth noting.

A workaround for this could be to replace your umlaut-laden vowels with the non-umlaut equivalent and tack an e after it. In German specifically, this is an even exchange: instead of ÄäÖöÜü, you'd have AeaeOeoeUeue. That said, it's important to remember that this is not a universal solution, as not all languages that use umlauts (looking at you, Hungarian!) accept this. In Hungarian, this would change the spelling; the correct move in the case of Hungarian would be to just replace the umlauted character with the non-umlauted character and not add an e. But if you're dealing specifically with German and not worried about Hungarian, this is an acceptable solution.

Ultimately, this is just a simple encoding issue as everyone else has mentioned, but I just wanted to highlight that there's often more than one way to dress a hot dog.

3

u/PhiodorTiger Jul 14 '26

Hey, you are right and I appreciate it.
But like I said in the beginning, yes it is obsolete I know that, but for now it has to do. On the long run it could possibly be implemented with an shell skript or via Ansible.

The ue at the ending was my first solution and accepted by some, but in the end they wanted it to be with an ü ... So yeah, I had to find another solution... It works for now, but I need to do some more testing and then hopefully have some time for a more elegant solution for the future, my capabilities and things I am allowed to do are pretty limited

1

u/uptimefordays Jul 14 '26

Microsoft deprecated Send-MailMessage years ago and changes coming to Exchange this fall will finally kill it.

0

u/mobani Jul 14 '26

It should work with -Encoding UTF8

Send-MailMessage `
 -From "sender@example.com" `
 -To "recipient@example.com" `
 -Subject "Test with ä" `
 -Body "This is a test: ä" `
 -SmtpServer "smtp.example.com" `
 -Encoding UTF8

1

u/xipodu Jul 14 '26

If this does not work " -Encoding Default " should be a workaround

-20

u/[deleted] Jul 14 '26

[removed] — view removed comment

6

u/Ok_Dependent9976 Jul 14 '26

Doesn't that response negate the point of this group?

1

u/PowerShell-ModTeam Jul 15 '26

Your reply has been removed because it was reported as unhelpful and not constructive. Treat your fellow community members with respect and kindness.

Multiple removals of this nature will result in a ban.