I have files sitting in a folder that need parsed and contents of those files emailed. The files contain the eye-catchers
that I need to parse to create the email. The Who,Subject and END
DATA sample:
>Who:Jim
>Subject: Process Run
TEXT DATA
....
... (Many Rows) just 80 columns
...
END
My Code:
Function Write-Log($LogFile,$LogText) { If($LogFile -match '_fax'){ $LogEntry=@" $($LogText)"@ }else{ $LogEntry=@" $((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@ } $LogEntry $LogEntry | Add-Content $LogFile } $writelogdir = 'c:\' $datapath='c:\' $archive='c:\Archive\' $def = "DONOTREPLY@bbt66.com" gci $datapath*.mai | %{$testdata=gc $_.PSPath for($i=0;$i -le $testdata.count;$i++){ if($testdata[$i] -match '<Who:'){$sender=($testdata[$i] -replace '<Who:(.+)','$1')} if($testdata[$i] -match '<Subject:'){$subject=($testdata[$i] -replace '<Subject:(.+)','$1')} if($testdata[$i] -notmatch '<Subject:|<Who:|END'){$body+=$testdata[$i] + "`r`n" } if($testdata[$i] -match 'END'){ $sendto = ($sender)+'@zzz.com' # Defalut a Sender $from = $def # added check if no SendTo Then Drop to ELSE If($sendto){ #Send-MailMessage -from $from -To $sendto -subject $sub -body $body -SmtpServer 'mailsrv' -ErrorAction Continue if($?){ Write-Log "$writelogdir\$($_.Name)_email.log" "Email Sent Successfully" Write-Log "$writelogdir\$($_.Name)_email.log" "Moved file $($_.Name) to $rootfolder\archive\" } else{ Write-Log "$writelogdir\$($_.Name)_email.log" "Email Send failed: $($error[0])";break } } Else { # added Message about No Email address Found Write-Log "$writelogdir\$($_.Name)_email.log" "Email Not Sent No Address Defined in ---> $_.Name " Write-Log "$writelogdir\$($_.Name)_email.log" "Moved file $($_.Name) to $rootfolder\archive\" } $sender='';$subject='';$body='';$sendto='' } } $_.MoveTo($archive + $_.Name) }
I need to store all lines after SUBJECT and before END in the variable $body. There could be 100+ lines that needs captured.
For some reason it's breaking down and creating multiple emails and not parsing correctly.
How can I make sure each line is ended with a crlf before sending to variable $body
Thanks.