Send-MailMessage converts apostrophe to question mark

Welcome Forums General PowerShell Q&A Send-MailMessage converts apostrophe to question mark

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
6 months, 2 weeks ago.

  • Author
    Posts
  • #98097

    Participant
    Points: 0
    Rank: Member

    Hi folks. I'm trying to setup sending emails via PowerShell and have the code to do it, but every time i use Send-MailMessage to send the body, it converts apostrophe symbol to question mark. Can someone advise what i'm doing wrong? I tried to convert it to UTF8 and ASCII but that made it worse. Thank you

    $message = get-content c:\mail\sample.txt | out-string

    Send-mailmessage -To fakeuser@contso.com -From testuser@contso.com -smtpserver "196.192.1.1" -subject "Test email" -Body $message

    The sample.txt file in c:\temp has the correct message format like this below

    "Please don't send out wrong requests for support"

    But once i execute the PS script, and get the email, it is written incorrectly as below

    "Please don?t send out wrong requests for support"

  • #98169

    Participant
    Points: 0
    Rank: Member

    A couple of things to try.

    1. Check how the string looks in the variable, is it correct if you do:

    Write-Output $message
    

    2. Send-Mailmessage also has an Encoding parameter, so try:

    Send-mailmessage -To fakeuser@contso.com -From testuser@contso.com -smtpserver "196.192.1.1" -subject "Test email" -Body $message -Encoding utf8
    
    • #98304

      Participant
      Points: 0
      Rank: Member

      Hi Fredrik, it looks like your solution of adding -encoding UTF8 at the end did the trick 🙂 Thanks so much!

  • #98187

    Participant
    Points: 2
    Rank: Member

    Hmm. Instead of using Out-String, try simply supplying the -Raw parameter to Get-Content to import the file as a single block of text and see if that clears things up a bit for you. 🙂

    • #98307

      Participant
      Points: 0
      Rank: Member

      Joel, thank you much for your reply as well 🙂

The topic ‘Send-MailMessage converts apostrophe to question mark’ is closed to new replies.