Gossamer Forum
Home : Products : DBMan : Customization :

Perl and DBMan abilities

Quote Reply
Perl and DBMan abilities
Well, as it always seems I am trying to stretch perl and DBMan to its limits. What I would currently like to know is if perl can read incoming email and add an entry into DBMan with the results. For example, sender, replay address, subject, message.

I think this can be done because I have heard of another script that can do it, but I've never gotten that script to run. Does someone have a script like this or know how to make one.
Quote Reply
Re: Perl and DBMan abilities In reply to
There is a POP3Client Perl module that seems like it might do what you want, but I don't know anything about how it works. You would have to have the module installed by your server, though.


------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
The server part is no problem because this is an in-house server. Do you know where I can find this client?

--------------
Never mind, I found the module. I'm not sure if it will work yet, but thanks for your help. I will let you know if anything becomes of it.

[This message has been edited by Helpdesk (edited June 24, 1999).]
Quote Reply
Re: Perl and DBMan abilities In reply to
Please do. I know others would be interested.

------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
Well, I installed the package and it works. But, I'm not sure how exactly to make this work because I'm still learning perl.

The code I currently have is as follows:
Code:
#!/usr/bin/perl

use Mail::POP3Client;
$pop = new Mail::POP3Client( USER => "user",
PASSWORD => "password",
HOST => "my.domain.com" );
for( $i = 1; $i <= $pop->Count(); $i++ ) {
foreach ( $pop->HeadAndBody( $i ) ) {
print $_, "\n";
}
print "\n";
}
$pop->Close();

This list all messages and the full header, subject, and body.

Then to print just the list just the sender and subject you replace the foreach with the following:
Code:
foreach( $pop->Head( $i, 10 ) ) {
/^(From|Subject):\s+/i && print $_, "\n";

My current problem is that I don't know how to adapt these two code model to list the sender, subject, and body. Does anyone have any ideas?
Quote Reply
Re: Perl and DBMan abilities In reply to
I don't know if this will help, but here's a little bit of code from "Perl 5 for Dummies":

Code:
$Name = 'larry';
$Pass = 'abcdef';
$Serv = 'mail.yourisp.com';
$Subj = 'Subject: Daily logging report';

use Mail::POP3Client;
$Client = new Mail::POP3Client($Name,$Pass,$Serv);
$TheState = $Client ->State;
if ($TheState ez 'AUTHORIZATION')
( die "Bad user name or password.\n" )
elsif ($TheState eq 'DEAD')
(die "Mail server unreachable or unavailable.\n" )

# Find out how many messages there are

$NumMsg = $Client ->Count;

#Loop through the messages (starting at 1)
for ($i=1; $i<=$NumMsg; $i+=1) {
$Headers = $Client->Head($i);
@HeadList - split (/\n/,$Headers);
foreach $Line (@Headlist) {
if ($Line =~ /^$Subj/) {
# Found the message. Get the body, then delete
$Body = $client->Body($i);
$Client->Delete($i);
# Process the report and leave
&ProcessReport($Body);
last;
}
}
}

sub ProcessReport {
my ($Report) = pop(@_);
# Do something here that is processing the report
return;
}

I guess this code is to look for a specific subject line, which will only appear once.

I'm not sure what all that means, but maybe it will make sense to you.

Please keep us posted.


------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
That worked! I did have to change two of the sub ProcessReport lines and those I posted below:
Code:
$From = substr($line,6);
$Subject = substr($line,9);
If you don't change those lines it prints out a : and space before the information.

Now, just one thing left to do, add the info to DBMan. How can I execute an external program? What I was thinking was to add a line before last; that executed an external program, DBMan, like /dir/db.cgi?db=email&uid=user&pass=password&add_record=1&Sender=$From&Subject=$Subject&Message=$Body

Would this work?

I just found one other problem too. If some one sends an attachment the whole thing is added in text. And it adds a ReceivedFor line for each attachment. Is there a way to ignore these lines?
Quote Reply
Re: Perl and DBMan abilities In reply to
So you're getting the info you want (and then some Smile), and now you need to know what do do with it, right?

You could probably divvy up the lines in the $Report variable in sub ProcessReport. Off the top of my head -- with lots of comment lines so you know what's going on:

Code:
sub ProcessReport {
my ($Report) = pop(@_);

# split the Report up into lines
@lines = split("\n",$Report);

# Look at each line
foreach $line (@lines) {

# Is it the sender's email address?
if ($line =~ /^From:/) {

# Knock off the label and just save the address
$From = substr($line,4);
}

# Is it the subject line?
elsif ($line =~ /^Subject:/) {

# Knock off the label and just save the good stuff
$Subject = substr($line,7);
}

# Are we at the end of the header?
elsif ($line =~ /^Status/) {

# Set a flag to save the rest of it
$body_follows=1;
}

# Has the flag been set?
elsif ($body_follows) {

# Save it all into one variable
$Body .= $line;
}
}
}

At this point, you should have the sender information in a variable called $From, the subject line in a variable called $Subject, and the body of the message in a variable called $Body. Try just printing out the variables and see what you get.

------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
I added the following code after &ProcessReport($Body) and before last;
Code:
print $From, "\n";
print $Subject, "\n";
print $Body, "\n";

I then added your code to ProcessReport. What I get from the script is
Quote:
Return-Path: <sender@server.com>

Test Message
I think the message "Test Message" comes from printing $Body because $Body is defined as $Body = $Client->Body($i); which comes directly from POP3Client. So, either I did something wrong or ProcessReport isn't defining the variables properly.
Quote Reply
Re: Perl and DBMan abilities In reply to
Try changing the names of the variables, then. Instead of $From, use $sender. Instead of $Subject, use $about.

Also, what you might do with the sub ProcessReport is this:

Code:
sub ProcessReport {
my ($Report) = pop(@_);
# split the Report up into lines
@lines = split("\n",$Report);
# Look at each line
foreach $line (@lines) {
print "$line\n";
}
}

That will make sure that your message is actually getting into the @lines array.

------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
I tried changing the sub so to just print out the lines like you said, and it didn't print anything.
Quote Reply
Re: Perl and DBMan abilities In reply to
Aha! It's not getting the info. That helps.

Try changing

sub ProcessReport {
my ($Report) = pop(@_);

to

sub ProcessReport {
my ($Report) = $_[0];




------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
Ok,
I reworked all this code and came up with this:
Code:
#!/usr/bin/perl

$Name = 'user';
$Pass = 'password';
$Serv = 'server.com';

use Mail::POP3Client;

$Client = new Mail::POP3Client( USER => "$Name",
PASSWORD => "$Pass",
HOST => "$Serv" );
$Client->Connect() | | die $Client->Message();

# Find out how many messages there are
$NumMsg = $Client ->Count;

# Loop through the messages (starting at 1)
for( $i = 1; $i <= $Client ->Count; $i++) {
foreach ($Client->HeadAndBody( $i ) ) {
$Body = $Client->Body($i);
print $_, "\n";
$Client->Delete($i);
# Process the report and leave
&ProcessReport($Body);
last;
}
}
$Client->Close();

sub ProcessReport {
my ($Report) = pop(@_);
# Do something here that is processing the report

}

However, this doesn't get just what I need form the email. So I need some help. A sample of what it returns is the following:
Code:
Return-Path: <sender@server.com>
Received: from server ([192.168.0.1])
by server.com (8.8.7/8.8.7) with SMTP id CAA22644
for <test@server.com>; Sat, 26 Jun 1999 02:58:14 -0400
Message-ID: <37747959.34B5@server.com>
Date: Sat, 26 Jun 1999 02:55:39 -0400
From: Sender <sender@server.com>
Reply-To: sender@server.com
Organization: Organization
X-Mailer: Mozilla 3.04 (Win95; I)
MIME-Version: 1.0
To: test@server.com
Subject: Test Subject
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Status:

Test Message

This script returns this for each message. My thought is to modify the ProcessReport sub to instruct perl to look through each message until it finds From: then extract the address the same for Subject and then everything after the status line is the message. Then this information would add a entry in DBMan for each message. But, I don't know how to program this in perl. Any help?

----
Well, after reviewing this post I noticed that UBB deleted the blank line after status in the example message. There is actually a blank line after the status line and before the actual message.

[This message has been edited by Helpdesk (edited June 26, 1999).]
Quote Reply
Re: Perl and DBMan abilities In reply to
Well, we're getting there. Smile

Your add link might work if you have the default.cfg file set up to allow a default user to add records and you use

&uid=default

instead of

&uid=user

It won't work to add the userid and password in with the add_record command. Sorry. Smile

What you could do, though, is write to the .db file directly without going through DBMan. Take a look at the code in sub add_record for ideas on how to do this.

As for attachments, I don't know how you would deal with them.



------------------
JPD





Quote Reply
Re: Perl and DBMan abilities In reply to
Hmmm,

Well I reviewed the code their and I understand most of it, but I think this is above my head. Unless you or someone else can help me here JPD, I think I'm going to have to drop this project.

Thanks for your help so far!