Page 1 of 1

Get header, body,a ttachments from EML

Posted: Sun Sep 30, 2007 3:07 pm
by Fangbeast
I needed not only to decode and save embedded base64 encoded attachments from raw EML files, but some way of storing the headers and body text and writing out the saved attachments, leaving only the link to the attachments in the stored EML file.

The code isn't optimised, probably buggy, a few things hard coded but it works for me. Maybe somebody ha a use for it and can show me how to do it better:):)

Code: Select all

Global FileIdIn.l, FileIdOut.l, NewEmlFile.l

InFile.s = OpenFileRequester("File to decode", "D:\", "Raw EML (*.eml)|*.eml", 0)           ; Get a file to read

NewEmlFile.l = CreateFile(#PB_Any, "D:\NewEMLFile.txt")                                     ; Create the new EML file from the old

If InFile.s                                                                                 ; Did we get a file?

  FileIdIn.l  = ReadFile(#PB_Any, InFile.s)                                                 ; Try to open it

  If FileIdIn.l                                                                             ; Did we get a handle?

    While Eof(FileIdIn.l) = 0                                                               ; Continue till the end of the file

      TestString.s = ReadString(FileIdIn.l)                                                 ; Get a string to test

      If TestString.s = ""                                                                  ; If the test string is empty

        Break                                                                               ; Return to the previous routine

      Else                                                                                  ; Otherwise do the below

        Debug TestString.s                                                                  ; Show the header lines

      EndIf                                                                                 ; End the test condition

    Wend                                                                                    ; Continue till the end

    While Eof(FileIdIn.l) = 0                                                               ; Continue till the end of the file

      TestString.s = ReadString(FileIdIn.l)                                                 ; Get a string to test

      If FindString(TestString.s, "Content-Transfer-Encoding: quoted-printable", 1) <> 0    ; Find the body

        Break                                                                               ; Found body header, quit

      EndIf                                                                                 ; No more tests

    Wend                                                                                    ; Continue the loop

    While Eof(FileIdIn.l) = 0                                                               ; Continue till the end of the file

      TestString.s = ReadString(FileIdIn.l)                                                 ; Get a string to test

      If TestString.s <> "--25146873-POCO-50454511"                                         ; Are we up to the binary boundary?

        Debug TestString.s                                                                  ; Show the text lines

      Else                                                                                  ; Otherwise do the below

        Break                                                                               ; Finished, out of here

      EndIf                                                                                 ; No more tests

    Wend                                                                                    ; Next part of loop

    While Eof(FileIdIn.l) = 0                                                               ; Continue till the end of the file

      TestString.s = ReadString(FileIdIn.l)                                                 ; Get a string to test

      If FindString(TestString.s, "Content-Disposition: attachment;", 1) <> 0               ; Find the file identifier

        FileName.s = Mid(TestString.s, 44, Len(TestString.s) - 44)                          ; Extract the filename

        WriteStringN(NewEmlFile.l, "file:///D:\" + FileName.s)                              ; Write html reference string

        FileIdOut.l = CreateFile(#PB_Any, "D:\" + FileName.s)                               ; Create the attachment file

        If FileIdIn.l And FileIdOut.l                                                       ; Are both files open at this time

          BlankString.s = ReadString(FileIdIn.l)                                             ; Throw the blank line away

          While Eof(FileIdIn.l) = 0                                                         ; Read till the end of the file

            InputString.s = ReadString(FileIdIn.l)                                           ; Read in the line to be decoded

            If InputString.s                                                                 ; If we actually got a string

              InputSize.l = Len(InputString)                                                 ; Check the length of the string

              *OutputBuffer = AllocateMemory(InputSize.l)                                    ; Create a buffer for the decoded string

              ActualBytes.l = Base64Decoder(@InputString.s, InputSize.l, *OutputBuffer, InputSize.l)  ; Did we actually get a decoded line

              WriteData(FileIdOut.l, *OutputBuffer, ActualBytes.l)                           ; Write the line to disk

              FreeMemory(*OutputBuffer)                                                      ; Free the decoder memory

              InputString.s = ""                                                             ; Clear the input string

            Else                                                                             ; Otherwise do the below

              Break                                                                          ; Return from here on blank line

            EndIf                                                                            ; End the conditional test

          Wend                                                                               ; Continue till the end of the file

          CloseFile(FileIdOut.l)                                                              ; Close the output file

        EndIf                                                                                ; End the conditional test

      Else                                                                                   ; Otherwise write the necessary line to disk

        WriteStringN(NewEmlFile.l, TestString.s)                                             ; Write the line to disk

      EndIf                                                                                  ; End the conditional test

    Wend                                                                                     ; Continue till the end of the file

    CloseFile(FileIdIn.l)                                                                    ; Close the file now

  EndIf                                                                                      ; End the conditional test

EndIf                                                                                        ; End the conditional test

End                                                                                            ; End the program