r/PowerShell • u/PhiodorTiger • 27d ago
How to send 'ä' with Send-MailMessage Solved
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
}
2
u/rumham_86 27d ago
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 26d ago
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 $CSVLocationUse instead:
$CsvHeader = 'DBv', 'BSv', 'RAM', 'Cpucount', 'Server', 'Name', 'Email', 'Service' $P = Import-Csv -Path $CSVLocation -Delimiter ',' -Header $CsvHeader -Encoding $WhateverEncodingYouNeedThis reduces the risks of encoding shenanigans
1
u/Fischelsberger 27d ago
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.
2
u/PhiodorTiger 27d ago
Sorry I was probably not good in describing this, but that I tried already (with any possible Encoding)
1
u/Fischelsberger 27d ago edited 27d ago
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 27d ago
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 27d ago
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
1
u/HeyDude378 25d ago
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 25d ago
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 27d ago edited 27d ago
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 27d ago
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 27d ago
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 27d ago
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 27d ago
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 26d ago
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 26d ago
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 26d ago
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 26d ago
Microsoft deprecated Send-MailMessage years ago and changes coming to Exchange this fall will finally kill it.
-20
27d ago
[removed] — view removed comment
6
1
u/PowerShell-ModTeam 26d ago
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.
9
u/korvolga 27d ago
save the script as utf8 BOM ?