Can you be certain that your data are always ordered
as from-subject-date? You are using Date to trigger your
printing, so if your first message's headers happen to be in the order from-date-subject,
it will print the From and Date, then collect the Subject from the
first message, then the From and Date from the second message, which
then triggers the printing of From2-Subject1-Date2, as you observed. It is
just a guess as I don't have your data, but from casual observation I know
that you can't rely on the order of these header lines, so using a specific line
essentially as an end-of-header marker is dangerous.
Since you only store the most recent from-subject-date lines
in your @buffer (you keep overwriting the array), that is all that is available to
be written to your database, and hence you will only store your
final three such lines. Actually you are only trying to store the first three
values from your array - how could you expect more than one e-mail to be recorded in the database? I'm
guessing you intend this database section as a plug-in replacement for your
print TEXTOUT line where at least it would save a record for each
e-mail, but still suffer from the data order problem I described above.