Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with an OR statement?

Status
Not open for further replies.

yim11

MIS
Jun 26, 2000
35
US
Hello,<br>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 (&quot; &quot;).<br>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.<br>TIA!<br>-Jim<br>---Begin Code Section---<br>foreach(@mbox) {<br>foreach(@$_) {<br>if ($current_key eq &quot;contents&quot;) {<br>if ($_=~m&quot;Content-Disposition: attachment&quot;) {<br>$bad=1;<br>}<br>if (!$bad) {<br>if (exists($message{$current_key})) {<br>$message{$current_key}=&quot;$message{$current_key} $_&quot;;<br>} else {<br>$message{$current_key}=$_;<br>}<br>}<br>} else {<br>@pieces=split(': ',$_,2);<br>if ($pieces[1] ne &quot;&quot;) { # Is this a unique entry or a continuation of the previous<br>$current_key=$pieces[0];<br>if (($current_key eq &quot;Content-Type&quot;) ¦ ¦ ($current_key eq &quot;Content-Disposition&quot;) ) {<br>$current_key=&quot;contents&quot;;<br>}<br>chomp($pieces[1]);<br>if (exists($message{$current_key})) {<br>$message{$current_key} = &quot;$message{$current_key} $pieces[1]&quot;;<br>} else {<br>$message{$current_key}=&quot;$pieces[1]&quot;;<br>}<br>} else {<br>chomp($pieces[0]);<br>if (($current_key eq &quot;To&quot;)¦ ¦($current_key eq &quot;cc&quot;)¦ ¦($current_key eq &quot;Subject&quot;)) {<br>$result=($pieces[0]=~s&quot; &quot;&quot;g);<br>}<br>if (exists($message{$current_key})) {<br>$message{$current_key} = &quot;$message{$current_key} $pieces[0]&quot;;<br>} else {<br>$message{$current_key}=&quot;$pieces[0]&quot;;<br>}<br>}<br>}<br>}<br># We need to remove the extraneous information from the addressing fields<br>@tmp=split('&quot;',$message{'From'});<br>$message{'From'}=$tmp[1];<br><br>if ($message{'To'}=~m&quot;&lt;&quot;) {<br>@tmp=split(',',$message{'To'});<br><br>foreach(@tmp) {<br>@tmp2=split('&lt;',$_,2);<br>@tmp3=split('&gt;',$tmp2[1],2);<br>$_=@tmp3[0];<br>}<br>$message{'To'}=join(',',@tmp);<br>}<br>if ($message{'cc'}) {<br>@tmp=split(',',$message{'cc'});<br>foreach(@tmp) {<br>@tmp2=split('&lt;',$_,2);<br>@tmp3=split('&gt;',$tmp2[1],2);<br>$_=@tmp3[0];<br>}<br>$message{'cc'}=join(',',@tmp);<br>} else {<br>$message{'cc'}=&quot; &quot;;<br>}<br># Date needs to be corrected to the dd-Mon-yyyy format<br>$result=($message{'Date'}=~s&quot;([0-9]) (...) ([0-9])&quot;$1-$2-$3&quot;);<br>@tmp=split(' ',$message{'Date'});<br>$message{'Date'}=$tmp[1];<br># Contents and subject must be escaped<br>$result=($message{'Subject'}=~s&quot;'&quot;''&quot;g);<br>$result=($message{'Subject'}=~s&quot;`&quot;``&quot;g);<br>$result=($message{'contents'}=~s&quot;'&quot;''&quot;g);<br>$result=($message{'contents'}=~s&quot;`&quot;``&quot;g);<br>$result=($message{'contents'}=~s&quot;\\&quot;\\\\&quot;g);<br>@tmp=split(' ',$message{'Subject'});<br>$catno=&quot;&quot;;<br>foreach(@tmp) {<br>if ($_=~m&quot;[0-9]-&quot;) {<br>$catno=$_;<br>}<br>}<br><br>$sent=$message{'Date'};<br>$subject=$message{'Subject'};<br>$whofrom=$message{'From'};<br>$sendto=$message{'To'};<br>$cc=$message{'cc'};<br>$contents=$message{'contents'};<br>---End Code Section---<br>
 
Use a regular expression.&nbsp;&nbsp;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.<br> <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Thank you very much for your reply Tom,<br>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!!<br>-Jim<br><br>---Begin Code Section---<br># We need to remove the extraneous information from the <br># addressing fields. These 2 lines grab the first &quot;From&quot; <br># address from the From list.<br>@tmp=split('&quot;',$message{'From'});<br>$message{'From'}=$tmp[1];<br><br># these 2 lines splits the up the &quot;To&quot; addresses, if there # is a &lt; character in there.<br>if ($message{'To'}=~m&quot;&lt;&quot;) {<br>@tmp=split(',',$message{'To'});<br><br># this section chops the &lt; and &gt;<br>foreach(@tmp) {<br>@tmp2=split('&lt;',$_,2);<br>@tmp3=split('&gt;',$tmp2[1],2);<br>$_=@tmp3[0];<br>}<br>$message{'To'}=join(',',@tmp);<br>}<br>if ($message{'cc'}) {<br>@tmp=split(',',$message{'cc'});<br>foreach(@tmp) {<br>@tmp2=split('&lt;',$_,2);<br>@tmp3=split('&gt;',$tmp2[1],2);<br>$_=@tmp3[0];<br>}<br>$message{'cc'}=join(',',@tmp);<br>} else {<br>$message{'cc'}=&quot; &quot;;<br>}<br><br># Date needs to be corrected to the dd-Mon-yyyy format<br># (matches the date fields and reformats)<br>$result=($message{'Date'}=~s&quot;([0-9]) (...) ([0-9])&quot;$1-$2-$3&quot;);<br>@tmp=split(' ',$message{'Date'});<br>$message{'Date'}=$tmp[1];<br><br># Contents and subject must be escaped<br># (this section 'escapes' certian characters so they will<br># insert correctly into the database)<br>$result=($message{'Subject'}=~s&quot;'&quot;''&quot;g);<br>$result=($message{'Subject'}=~s&quot;`&quot;``&quot;g);<br>$result=($message{'contents'}=~s&quot;'&quot;''&quot;g);<br>$result=($message{'contents'}=~s&quot;`&quot;``&quot;g);<br>$result=($message{'contents'}=~s&quot;\\&quot;\\\\&quot;g);<br>@tmp=split(' ',$message{'Subject'});<br>$catno=&quot;&quot;;<br>foreach(@tmp) {<br>if ($_=~m&quot;[0-9]-&quot;) {<br>$catno=$_;<br>---End Code Section---<br>
 
Ok, well that doesn't help much, but let's see if I can get a solution for you anyway.<br><br>$message{'To'} should consist of email addresses and perhaps other extraneous data such as names, commas, and other symbols.<br><br># lets create an array to store the addresses<br>my @addresses;<br><br># and lets copy the hash item to an easier to write var<br>my $to = $message{'To'};<br><br># strip any newlines<br>$to =~ s/\n//g;<br><br># counter<br>my $x = 0;<br><br># loop through the string until all addresses are found<br>while ($to)<br>{<br>&nbsp;&nbsp;# match first address<br>&nbsp;&nbsp;$to =~ /(.*?)(&lt;¦,¦\s¦&quot;)(([a-zA-Z\-_]+)((\.([a-zA-Z\-_]+))*)@(([a-zA-Z\-_]+)\.)+([a-zA-Z]+))(&gt;¦,¦\s¦&quot;)(.*?)/;<br><br>&nbsp;&nbsp;# 3rd element represents the address<br>&nbsp;&nbsp;$addresses[$x++] = $3;<br><br>&nbsp;&nbsp;# the remaining part<br>&nbsp;&nbsp;$to = $12;<br>}<br><br># sort the addresses;<br>@addresses = sort @addresses;<br><br># print them out to see that it worked<br>foreach $address (@addresses)<br>{<br>&nbsp;&nbsp;print &quot;$address\n&quot;;<br>}<br><br>This should give you everything which looks like an address (<A HREF="mailto:localpart@host.exte">localpart@host.exte</A>nsion) and is between either spaces, brackets &lt;&gt;, commas, or quotes &quot;&quot;, or any combination of those delimiters.&nbsp;&nbsp;Syntax may not be perfect, but give it a try and see if it works. <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Thank you very much!! I am closer to fixing this than ever before thanks to you!!<br><br>I wanted to ask one more thing.<br>If this line is the one looking for the address,<br>-<br>@tmp=split('&quot;',$message{'From'});<br>-<br>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.<br><br>Any thoughts would be greatly appreciated. You have been a ton of help and I really appreciate it!!<br>-Jim
 
You could do the same thing in the From field as in the To field... it will match all email addresses.&nbsp;&nbsp;But, if you want to match EVERYTHING after from, I would do this:<br><br>$message{'From'} =~/^(.*)from(.*)$/i;<br>my $from = $2;<br> <p> Sincerely,<br><a href=mailto: > </a><br><a href= Anderson</a><br>CEO, Order amid Chaos, Inc.<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top