Solved

Parsing an Email header

Posted on 2003-11-02
8
354 Views
Last Modified: 2010-04-03
Hello, I want to Parse the Date from a header, so I need a parser which looks for ";" in the header and Save the Value after ";" as a String.

Here is a header;:
---------------------------------------------------------------------------------------------
Return-Path: <wwwrun@server5.rhs-it.de>
Received: from smtpsrv1.hrz.uni-oldenburg.de (smtpsrv1.hrz.uni-oldenburg.de [134.106.87.8])
      by mailsrv1.hrz.uni-oldenburg.de (8.12.8/8.12.8) with ESMTP id h9OFOduM018338
      for <gbitest@uni-oldenburg.de>; Fri, 24 Oct 2003 17:24:39 +0200
Received: from smtpsrv1.hrz.uni-oldenburg.de (localhost.localdomain [127.0.0.1])
      by smtpsrv1.hrz.uni-oldenburg.de (8.12.8/8.12.8) with ESMTP id h9OFOcag028455
      for <gbitest@uni-oldenburg.de>; Fri, 24 Oct 2003 17:24:38 +0200
Received: from loadb02.uni-oldenburg.de ([134.106.87.17])
      by smtpsrv1.hrz.uni-oldenburg.de (MailMonitor for SMTP v1.2.2 ) ;
      Fri, 24 Oct 2003 17:24:38 +0200 (CEST)
Received: from server5.rhs-it.de (server5.rhs-it.de [217.172.182.111])
      by mailgate.uni-oldenburg.de (8.12.8/8.12.8) with ESMTP id h9OFOcAO010270
      (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO)
      for <gbitest@uni-oldenburg.de>; Fri, 24 Oct 2003 17:24:38 +0200
Received: (from wwwrun@localhost)
      by server5.rhs-it.de (8.11.3/8.11.3/SuSE Linux 8.11.1-0.5) id h9OFOce24381;
      Fri, 24 Oct 2003 17:24:38 +0200
Date: Sat, 30 Dec 1899 01:00:00 +0100
Message-Id: <200310241524.h9OFOce24381@server5.rhs-it.de>
To: gbitest@uni-oldenburg.de
Subject: test
Content-Type: text/html; charset=windows-1254
From: "Arash Mahini" <Hallo@yahoo.com>
X-Mailer: System33r
X-Spam-Status: No, hits=4.2 required=5.0 tests=FORGED_YAHOO_RCVD,HTML_MESSAGE,
      HTML_MIME_NO_HTML_TAG,MIME_HEADER_CTYPE_ONLY,MIME_HTML_ONLY
      autolearn=no version=2.60
X-Spam-Level: ****
X-Spam-Checker-Version: SpamAssassin 2.60 (1.212-2003-09-23-exp) on
      smtpsrv1.hrz.uni-oldenburg.de
In-Reply-To: test
-------------------------------------------------------------------------------------
I want to get "Fri, 24 Oct 2003 17:24:39 +0200" in a String (Or TDateTime).


0
Comment
Question by:k4hvd66
8 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 113 total points
ID: 9667200
The Indy components have a component called TidMessage. If you load this source into such a component then the properties allow you access to most of these fields.
0
 
LVL 4

Expert Comment

by:k4hvd77
ID: 9669160
My problem is the TIdMessage, I cannot get the right Date of message with TIdmessage, it changes the date to "Now"!!
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9669928
And what if you use TIdMessage.Headers.Values['Date'] instead of the Date property? The Date property is not the same as the Date value in the header.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 26

Assisted Solution

by:EddieShipman
EddieShipman earned 112 total points
ID: 9673937
The problem with the appraach you are taking in searching for a ';' is that other headers may and do have that character in them.

What you are trying to parse, from what I can see, is the Received headers.

Even TidMessage doesn't parse the headers of an email like we would like. It just places them into a stringlist.

What you can do is to write a handler to take the headers, look for the word "Received:" . Something a little like this using
the NameValue Pairs on the headers:

        idPop31.Retrieve(i, msg);
        msg.Headers.NameValueSeparator := ':';
        if Length(msg.Headers.Text) > 0 then
        begin
          for x := 0 to msg.Headers.Count-1 do
          begin
            s := msg.Headers.Names[x];
            if s = 'Received' then
            begin
              s := msg.Headers.Values[s];
              iPos := Pos(';', s);
              s := Trim(Copy(s, iPos+1, Length(s)));
               // s now contains the date from the received header
            end;
          end;


Its too bad that there isn't a standard that these haders should be formatted. That is one beef I have with W3C because it makes
fighting spam a lot more difficult.

0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9673959
But, your header may have multiple Received haeders and you would get a date for each one.
If you really are looking for just the Date header, then Workshop_Alex had the right answer
above.

But then again, you asked to get the "Fri, 24 Oct 2003 17:24:39 +0200" datetime and it is in
your Received header....


0
 

Author Comment

by:k4hvd66
ID: 9673967
what do you mean to this:
----------------------------------------------------------------------------------------
procedure TForm1.Parse(S : String; var List : TStringList);
VAR
L : TstringList;
I : Integer;
begin
 L := TstringList.Create; // use another TStringList to parse source string on subelements
 L.Text :=S;

 If L.Count > 0 Then
  Begin
    If List = Nil Then List := TStringList.Create Else List.Clear;
    For I := 0 To L.Count-1 Do
      List.Values[Copy(Trim(L[I]),1,Pos(';',Trim(L[I]))-1)] := Trim(Copy(Trim(L[I]),Pos(';',Trim(L[I]))+1,Length(Trim(L[I]))));
  End;

 L.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR S:STring; L1:TStringList;
begin
L1 :=TStringList.Create;
Parse (Memo1.Lines.Text, L1);
ShowMessage('Date: '+L1.Values['k4hvd@web.de']);

end;
----------------------------------------------------------------------------------------

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 60
Delphi: how to send PJL commands to printer 3 99
Delphi 2 59
Is there any TOOL for creating database *.CDS[ClientDataSet] ,*.FDS,*.FDB,*.ADB? 6 35
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question