×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help with an OR statement?

Help with an OR statement?

Help with an OR statement?

(OP)
Hello,
I have a perl script that looks at a users mailbox (var/spool/mail/USERNAME) and takes a message extracts the To, From, CC, BCC, Subject, and Message out and enters them as a comment in a database. The script looks for the email address in the To field by extracting everything between qoutes (" ").
Which works great for email sent from Lotus Notes [4.6] as that is the format Notes uses. Unfortunatly, MS Outlook does not use quotes in that field. So all email sent from Outlook is ignored. What I am looking for is any help that will allow me to change the script to look for quotes OR anything after FROM: so that all email will be processed the same. I have included the section of script I am referring to at the bottom of this post. Thanks VERY much in advance for any and all help on this. It has been a huge source of frustration for me and the company I work for.
TIA!
-Jim
---Begin Code Section---
foreach(@mbox) {
foreach(@$_) {
if ($current_key eq "contents") {
if ($_=~m"Content-Disposition: attachment") {
$bad=1;
}
if (!$bad) {
if (exists($message{$current_key})) {
$message{$current_key}="$message{$current_key} $_";
} else {
$message{$current_key}=$_;
}
}
} else {
@pieces=split(': ',$_,2);
if ($pieces[1] ne "") { # Is this a unique entry or a continuation of the previous
$current_key=$pieces[0];
if (($current_key eq "Content-Type") ¦ ¦ ($current_key eq "Content-Disposition") ) {
$current_key="contents";
}
chomp($pieces[1]);
if (exists($message{$current_key})) {
$message{$current_key} = "$message{$current_key} $pieces[1]";
} else {
$message{$current_key}="$pieces[1]";
}
} else {
chomp($pieces[0]);
if (($current_key eq "To")¦ ¦($current_key eq "cc")¦ ¦($current_key eq "Subject")) {
$result=($pieces[0]=~s" ""g);
}
if (exists($message{$current_key})) {
$message{$current_key} = "$message{$current_key} $pieces[0]";
} else {
$message{$current_key}="$pieces[0]";
}
}
}
}
# We need to remove the extraneous information from the addressing fields
@tmp=split('"',$message{'From'});
$message{'From'}=$tmp[1];

if ($message{'To'}=~m"<") {
@tmp=split(',',$message{'To'});

foreach(@tmp) {
@tmp2=split('<',$_,2);
@tmp3=split('>',$tmp2[1],2);
$_=@tmp3[0];
}
$message{'To'}=join(',',@tmp);
}
if ($message{'cc'}) {
@tmp=split(',',$message{'cc'});
foreach(@tmp) {
@tmp2=split('<',$_,2);
@tmp3=split('>',$tmp2[1],2);
$_=@tmp3[0];
}
$message{'cc'}=join(',',@tmp);
} else {
$message{'cc'}=" ";
}
# Date needs to be corrected to the dd-Mon-yyyy format
$result=($message{'Date'}=~s"([0-9]) (...) ([0-9])"$1-$2-$3");
@tmp=split(' ',$message{'Date'});
$message{'Date'}=$tmp[1];
# Contents and subject must be escaped
$result=($message{'Subject'}=~s"'"''"g);
$result=($message{'Subject'}=~s"`"``"g);
$result=($message{'contents'}=~s"'"''"g);
$result=($message{'contents'}=~s"`"``"g);
$result=($message{'contents'}=~s"\\"\\\\"g);
@tmp=split(' ',$message{'Subject'});
$catno="";
foreach(@tmp) {
if ($_=~m"[0-9]-") {
$catno=$_;
}
}

$sent=$message{'Date'};
$subject=$message{'Subject'};
$whofrom=$message{'From'};
$sendto=$message{'To'};
$cc=$message{'cc'};
$contents=$message{'contents'};
---End Code Section---

RE: Help with an OR statement?

Use a regular expression.  You gave me too much code to muddle through, but if you can show me the part where you are actually trying to extract the email and what types of data we could expect, then I'll show you a regexp to do it.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Help with an OR statement?

(OP)
Thank you very much for your reply Tom,
Unfortunatly this isnt my code, I 'inherited' it, along with the problems :( But I think the section below is the part that extracts the mail fields, I have also tried to include some additional comments. Thanks again for all your help! I really appreciate it!!
-Jim

---Begin Code Section---
# We need to remove the extraneous information from the
# addressing fields. These 2 lines grab the first "From"
# address from the From list.
@tmp=split('"',$message{'From'});
$message{'From'}=$tmp[1];

# these 2 lines splits the up the "To" addresses, if there # is a < character in there.
if ($message{'To'}=~m"<") {
@tmp=split(',',$message{'To'});

# this section chops the < and >
foreach(@tmp) {
@tmp2=split('<',$_,2);
@tmp3=split('>',$tmp2[1],2);
$_=@tmp3[0];
}
$message{'To'}=join(',',@tmp);
}
if ($message{'cc'}) {
@tmp=split(',',$message{'cc'});
foreach(@tmp) {
@tmp2=split('<',$_,2);
@tmp3=split('>',$tmp2[1],2);
$_=@tmp3[0];
}
$message{'cc'}=join(',',@tmp);
} else {
$message{'cc'}=" ";
}

# Date needs to be corrected to the dd-Mon-yyyy format
# (matches the date fields and reformats)
$result=($message{'Date'}=~s"([0-9]) (...) ([0-9])"$1-$2-$3");
@tmp=split(' ',$message{'Date'});
$message{'Date'}=$tmp[1];

# Contents and subject must be escaped
# (this section 'escapes' certian characters so they will
# insert correctly into the database)
$result=($message{'Subject'}=~s"'"''"g);
$result=($message{'Subject'}=~s"`"``"g);
$result=($message{'contents'}=~s"'"''"g);
$result=($message{'contents'}=~s"`"``"g);
$result=($message{'contents'}=~s"\\"\\\\"g);
@tmp=split(' ',$message{'Subject'});
$catno="";
foreach(@tmp) {
if ($_=~m"[0-9]-") {
$catno=$_;
---End Code Section---

RE: Help with an OR statement?

Ok, well that doesn't help much, but let's see if I can get a solution for you anyway.

$message{'To'} should consist of email addresses and perhaps other extraneous data such as names, commas, and other symbols.

# lets create an array to store the addresses
my @addresses;

# and lets copy the hash item to an easier to write var
my $to = $message{'To'};

# strip any newlines
$to =~ s/\n//g;

# counter
my $x = 0;

# loop through the string until all addresses are found
while ($to)
{
  # match first address
  $to =~ /(.*?)(<¦,¦\s¦")(([a-zA-Z\-_]+)((\.([a-zA-Z\-_]+))*)@(([a-zA-Z\-_]+)\.)+([a-zA-Z]+))(>¦,¦\s¦")(.*?)/;

  # 3rd element represents the address
  $addresses[$x++] = $3;

  # the remaining part
  $to = $12;
}

# sort the addresses;
@addresses = sort @addresses;

# print them out to see that it worked
foreach $address (@addresses)
{
  print "$address\n";
}

This should give you everything which looks like an address (localpart@host.extension) and is between either spaces, brackets <>, commas, or quotes "", or any combination of those delimiters.  Syntax may not be perfect, but give it a try and see if it works.

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Help with an OR statement?

(OP)
Thank you very much!! I am closer to fixing this than ever before thanks to you!!

I wanted to ask one more thing.
If this line is the one looking for the address,
-
@tmp=split('"',$message{'From'});
-
then is there a way to tell that line to look for the quotes and/or the word FROM? In other words, as it is now, it grabs anything it finds between quotes, but I want to tell it to grab anything it finds after the word FROM.

Any thoughts would be greatly appreciated. You have been a ton of help and I really appreciate it!!
-Jim

RE: Help with an OR statement?

You could do the same thing in the From field as in the To field... it will match all email addresses.  But, if you want to match EVERYTHING after from, I would do this:

$message{'From'} =~/^(.*)from(.*)$/i;
my $from = $2;

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close