Home > Back-end >  Umlauts and special characters preservation (Send-MailMessage)
Umlauts and special characters preservation (Send-MailMessage)

Time:06-18

I am having a problem with the formatting of an e-mail. I have special characters in the body and they are not being displayed correctly, I have tried several solutions but the e-mail keeps arriving with a format that does not interpret special characters. The body of the e-mail has to be sent in plain text, so I cannot use [-BodyAsHtml]. Here is my function. I hope someone can help me. Best regards

function SendMail {
    $PDFName = $settings.pdf.Name
    $WebResponseObj = Invoke-WebRequest -Uri $PDFUrl
    $getLastModified = $WebResponseObj.Headers.'Last-Modified'
    $PDFdate = [DateTime]$getLastModified
    $dateTimePDF = $PDFdate.DateTime
    $plainTextBody = "
    Meldung über das aktualisierte Dokument `r`n
    File Name: $PDFName `r`n
    File Url: $PDFUrl `r`n
    Letzte Aktualisierung: $dateTimePDF `r`n
    Letzte Aktualisierungsprüfung: $getDateTime
    "
    $param = @{ 
        SmtpServer = $settings.mail.SmtpServer
        Port = $settings.mail.Port
        UseSsl = $true
        Credential = $userCredential
        From = $settings.mail.FromUser
        To = $settings.mail.ToUser
        Subject = $settings.mail.Subject
        Body = $plainTextBody
        Priority = 'High'
        DeliveryNotificationOption = 'OnFailure'
        ErrorAction = 'Stop'
        Encoding = ([System.Text.Encoding]::UTF8)
    }
    Send-MailMessage @param
}

CodePudding user response:

I have found the problem, the .ps1 file was in UTF-8 format and to be interpreted correctly it has to be UTF-8 with BOM, that's it!

  • Related