0

I currently have a process that recurses through some directories on the network, saves some information to a CSV, and then I import the CSV once to send it out again using Out-File so it looks readable by doing the following:

$CSV = Import-Csv -Path $LogPath
$CSV | Out-File -FilePath $LogPath2

This code works fine. $Logpath is the csv, $LogPath2 is a .txt. I use the following code to send the results in an email via bmail.exe:

Start-Process -FilePath 'D:\Scheduled\BMAIL\bmail.exe' -ArgumentList '-s SMPTServerName -t ToThisEmail@test.com -f FromThisEmail@test.com -h -a "Email Subject Line" -c -m FileToShowContentInEmail'

The FileToShowContentInEmail is the .txt file generated from the Out-File line above, but the result in the email it sends is this:

ÿþ

I've tried specifying -Encoding ascii,default,utf8,utf7,oem, all of the encoding options for that Out-File command, but they all either return those weird characters in the email or it will actually have the information in the email but it is unaligned and hard to read/follow.

This process is being ran as a .ps1 on a ftp server as a scheduled task. How can I get this to send the .txt file's content in an email and it be formatted correctly? When I go and view the .txt, it is formatted fine just not in the email. I can't use PowerShell's Send-MailMessage due to some policies/rules put in place on our domain so bmail.exe was the workaround.

cet51
  • 1,196
  • 2
  • 11
  • 29
  • look at just using `Send-MailMessage` ? – colsw Aug 02 '17 at 15:21
  • my apologies, I will edit. That's what I meant when I put `Send-Email` – cet51 Aug 02 '17 at 15:35
  • @Ansgar Wiechers That question, that is marked as the duplicate, does not answer my question. I have already tried all of the options for the -Encoding parameter as stated in my question. – cet51 Aug 02 '17 at 15:37
  • It's clearly not possible that a file saved via `Out-File -Encoding Ascii` would have a Unicode BOM. If anything, you may want to change your conversion pipeline to `Import-Csv | Out-String | Out-File -Encoding Ascii`. Are you sure that `bmail` is reading the correct file and not some artifact that still contains Unicode text? – Ansgar Wiechers Aug 02 '17 at 15:49
  • Try using the same variable you used for the data export. Change your argument string to something like this: ``"... -a `"...`" -c -m `"$LogFile2`""`` or (better yet) use [splatting](https://stackoverflow.com/a/40105646/1630171). – Ansgar Wiechers Aug 02 '17 at 15:57
  • For your second comment, entering the $LogFile2 variable breaks the bmail.exe process. It has to be the exact file path, which I checked again for you and it is correct. Your first comment mentions changing the conversion pipeling, something like this? `Import-Csv -Path $LogPath -delimiter "," | Out-String | Out-File -FilePath $LogPath2` – cet51 Aug 02 '17 at 16:28
  • I figured it out. In Outlook 2016 go to File < Options < Mail < Stationery and Fonts... then click on Font for the Composing and reading plain text messages. I changed the font to Courier New and the formatting fixed itself in the emails. Outlook was the culprit! – cet51 Aug 02 '17 at 16:36

0 Answers0