Gossamer Forum
Quote Reply
GT::Mail Question
I've been slowly working my way through the functionality of the GT library and have been experimenting with parsing mail. I have this question about this very simple test.

I just want to get the 'from' header and do something with it depending who the mail is from. So this code snippet is to just extract the "from" header from mail passed to the
script via an alias (/etc/aliases)

------------------------------------
my $mail = new GT::Mail(debug => 0) ; #create mail object
# reading mail from STDIN
my $parser = $mail->parse (\*STDIN) or die "Error: $GT::Mail::error";
# get From: info
my @from = $parser->split_field ('from');
---------------------
Now when I drive this script from an alias in /etc/aliases the email actually has a 'From' header preceding the 'from' header I want to parse.
eg first 'From' is: (note no colen after the From)

From somebody@somedomain.com Thu Jun 6 15:19 PST 2001
then later on the usual from header.
From: Doug Robb <doug@somewhere.com>

(I think this is just a sendmail thing where the first
From header is tacked on *before* the actual From: header
that comes later on. You'll notice there is no ":" on the
first From. Check out http://www.usenix.org/publications/perl/perl17.html to see
and example of a typical email header that shows this behaviour.

So the question is can I get $parser->split_field ('from');
to just pull out the "From:" header (the one I want) and skip past the first "From"? If I use the @from array returned now I have date and time as elements of the array and I don't have the senders full name!

I realise this is a bit off track but I feel anyone wanting to write interesting plug-ins will need to be able to drive the GT library a bit harder that usual.

Thanks doug




Quote Reply
Re: GT::Mail Question In reply to
Why did you post this in the Links SQL plugins forum?

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: GT::Mail Question In reply to
I thought I covered that in the post. You write pluggins so you'd want to use the GT library right? After all it comes as part of Links SQL. The doco for the GT library is in the developer section for writers of plugins. I'n not sure who else would use it other than writers of pluggins.

I did search all forums and it doesn't appear to be any other area where people needing to know more about the GT library can comment. Perhaps there should be a developer forum but plugin authors is pretty close. Its really an excellent library of functionality so I'm suprised it doesn't generate more discussion.

regards doug








Quote Reply
Re: GT::Mail Question In reply to
Sorry ignore me. I misread the thread title. I read it as GMail not GT::Mail



Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: GT::Mail Question In reply to
Hi,

Even though this is a GT::Mail thing, you are referring to the gossamer mail program, right? Or are you trying to get the GT::Mail system to read incoming messages?

The way Links is set up, it does outgoing mail only, so any "incoming" mail has to be a ground-up hack. Gossamer Mail, on the other hand, does mail both ways.

The GT:: libraries are being developed to be cross-product, so they will share functionality, and eventually, one "core" library, with add on program libraries for each specific product.

Right now, not much has been done with GT::Mail, since sending an outgoing message is fairly trivial, and that is all a "links" program/system really needs to do.

The newsletter system, if it wants to check for bounced mail, and has access to a catch-all account, ... well, that might be a good reason to try to read a pop box or mailbox file, but it 's not a thing a lot of people are doing right now :)



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Forum:http://LinkSQL.com/forum
Quote Reply
Re: GT::Mail Question In reply to
Hi,

Yes, we've seen some mail servers add:

>From

at the beginning of the headers which causes a bit of a headache. If all you want is the from, you should do:

my $mail = new GT::Mail;
my $head = $mail->parse_head (\*STDIN);
my $from = $head->get('from');

as parse_head() method won't parse the entire body and is quite a bit quicker. If you need the body, then do the same thing but do ->parse(\*STDIN) instead of parse_head().

Give this a try and let me know if it works. If it doesn't, I can have Scott take a look (he wrote the GT::Mail set while doing Gossamer Mail).

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: GT::Mail Question In reply to
In Reply To:
Its really an excellent library of functionality so I'm suprised it doesn't generate more discussion.
Thanks! I'm surprised too. =) Maybe a dedicated forum would be useful, but I'm not sure. Our next product due out will be shareware and have the GT libs as part of it so that may peak intrest significantly.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: GT::Mail Question In reply to
Thanks for the speed tip Alex but
my $from = $head->get('from');
Still returns the first 'From' .....
The sendmail seems to be adding a line of the form
From me@somedomain.com Thu Jun 6 15:19 PST 2001 as the first line of a post thats re-directed from an alias. (ie the date and time are included as part of an additional From line that is generated - what a pain!)

I'd suggest the quick fix is to only look for the 'From:' when looking for the 'from' header (ie ignore a mal-formed
header which in this case is a 'From' with no colon after it). One could also recognise a date and time in the From string and remove that (as it shouldn't be in a 'normal' from header) but that would be more work. (ie if you assume
that the only difference between the real from and the extra from is the addition of the date and time in the string.

The other option I suppose is if there is more than one 'From' line then to return them all and let the programmer decide which one to use. It seems the way it is now it just finds the first one.

Anyway thanks for looking into this.

regards doug



Quote Reply
Re: GT::Mail Question In reply to
According to the rfc (2822 and the older 822), there has to be a colon in every actual header line. Since "some mailers" (to quote Alex) have a tendency to add a /^From / line at the top, it looks like Scott added that into the while loop at the top of GT::Mail::Parts::extract, because it appears to stop looping on the first non-header line.

Also, there is nothing that says the /^From: / header has to be present. Now, I do not have a copy of GMail to verify this with (anyone want to donate one?), but it is possible that the described functionality was intentional, so that GMail would have a field to fall back on so that it at least something to display when there was no /^From: / header in the message.

Either way, the regex should probably be changed, and the /^From / lines ignored, unless there is some other reason to keep them. As far as I know, they are only used to separate messages stored in mbox format.


Quote Reply
Re: GT::Mail Question In reply to
If you do:

my @from_lines = $head->get('from');
my $from = pop @from_lines;

That would get you the last from line. When you use get in list context, you get one entry per From field found.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: GT::Mail Question In reply to
Great Alex thanks that works fine ....
(I actually tried reading it as an array at one stage but made a mistake in unloading it ....)