Jump to content

IMAP & Regex Help


cute_girl

Recommended Posts

Friends...

 

I am using IMAP functions in PHP. I want to access Email Server, read all messages in INBOX then display each message subject, its body(contents), and sender Email Address. After doing it, I want to seperate those messages which contain URLs having specific files only, not all URLs.

 

After this I want to display only URLs having those files (v.php & u.php only) in those messages, not complete message. Final output should be like this....

 

1. Message Subject

      http://www.domain.com/u/v.php?id=1&id2=4

      a. v.php

      b. u.php

      From: [email protected]

 

2. Message Subject2

      http://www.domain2com/u/v.php?id=1&id2=4

      a. v.php

      b. u.php

      From: [email protected]

 

 

 

 

<?php

//Connection

$mbox=imap_open("{mail server}","user","paswd") or die("Error! Couldn't Connect");

 

 

//read header informations

 

$msg=imap_mailboxmsginfo($mbox);

 

//sort messages by date

$message_count = imap_num_msg($mbox);

$messages = imap_sort($mbox, SORTDATE,0);

 

//function to extract domain from messages

function extract_domains_from($string){

    preg_match_all("/(http:\/\/[a-zA-Z0-9._%-?&\/:]+)/i", $string, $matches);

    return $matches[0];

}

 

 

//Loop to display Message Subject, Domain Name and Files

  foreach ($messages as $message) {

 

    for ($i = 1; $i <= $message_count; ++$i) {

 

//printing subjects

 

      $header = imap_header($mbox, $i);

  echo "<br>";

      print "<b>".$header->Subject."</b>";

 

  //finding bodies

        $header = imap_header($mbox, $i);

        $body = trim(substr(imap_body($mbox, $i), 0));

 

 

//function calls to extract domain from message

$domainss = extract_domains_from($body);

echo "<br>";

 

//Joined all domains and the print

$allurls=(implode(" ",$domainss));

 

echo "<br><br>";

 

//Print single URL

$emailmsg=$allurls;

 

$singleurl=(explode(" ",$allurls));

$size = count($singleurl);

echo "<br><br>";

 

  for ($u=0; $u<$size; $u++)

  {

$spliturl=(explode("?",$singleurl[$u]));

echo "<br>".$spliturl[0]."<br>";

  }

 

echo "<br><br>";

 

for($p=0; $p<$size; $p++)

{

echo "<br><br>";

 

$emailmsg=$singleurl[$p];

 

//Removing Last part from URL

 

if (eregi("http://", $emailmsg))

{

$emailmsg = explode("http://", $emailmsg);

$emailmsg = array_reverse($emailmsg);

 

$domain = $emailmsg[0];

$domain = explode(".", $domain);

if ($domain[0] == "www")

{

$var=explode("/",$domain[2]);

$extracted = $domain[1].".".$var[0];

echo $extracted;

}

else

{

$var=explode("/",$domain[1]);

$extracted = $domain[0].".".$var[0];

echo $extracted;

}

}

}

 

 

//printing from email address 

$header = imap_header($mbox, $i);

 

$from = $header->from;

foreach ($from as $id => $object) {

 

    $fromaddress = $object->mailbox . "@" . $object->host;

echo "<br><br><u>".$fromaddress."</u>";

 

}

 

  echo "<br><br><br><br>-------------<br><br>";

 

  }

 

 

    }

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/116361-imap-regex-help/
Share on other sites

please use this forums code tags...

 

like so...

<?php
//Connection
$mbox=imap_open("{mail server}","user","paswd") or die("Error! Couldn't Connect");


//read header informations

$msg=imap_mailboxmsginfo($mbox);

//sort messages by date
$message_count = imap_num_msg($mbox);
$messages = imap_sort($mbox, SORTDATE,0);

//function to extract domain from messages
function extract_domains_from($string){
    preg_match_all("/(http:\/\/[a-zA-Z0-9._%-?&\/:]+)/i", $string, $matches);
    return $matches[0];
}


//Loop to display Message Subject, Domain Name and Files
   foreach ($messages as $message) {

    for ($i = 1; $i <= $message_count; ++$i) {
   
   //printing subjects

      $header = imap_header($mbox, $i);
     echo "
";
      print "".$header->Subject."";

     //finding bodies
        $header = imap_header($mbox, $i);
        $body = trim(substr(imap_body($mbox, $i), 0));
      

//function calls to extract domain from message
$domainss = extract_domains_from($body);
echo "
";

//Joined all domains and the print
$allurls=(implode(" ",$domainss));

echo "

";

//Print single URL
$emailmsg=$allurls;

$singleurl=(explode(" ",$allurls));
$size = count($singleurl);
echo "

";

  for ($u=0; $u<$size; $u++)
  {
   $spliturl=(explode("?",$singleurl[$u]));
   echo "
".$spliturl[0]."
";
  }

echo "

";

for($p=0; $p<$size; $p++)
{
echo "

";

$emailmsg=$singleurl[$p];

//Removing Last part from URL

if (eregi("http://", $emailmsg))
{
$emailmsg = explode("http://", $emailmsg);
$emailmsg = array_reverse($emailmsg);

$domain = $emailmsg[0];
$domain = explode(".", $domain);
if ($domain[0] == "www")
{
$var=explode("/",$domain[2]);
$extracted = $domain[1].".".$var[0];
echo $extracted;
}
else
{
$var=explode("/",$domain[1]);
$extracted = $domain[0].".".$var[0];
echo $extracted;
}
}
}
    
    
   //printing from email address 
$header = imap_header($mbox, $i);

$from = $header->from;
foreach ($from as $id => $object) {

    $fromaddress = $object->mailbox . "@" . $object->host;
   echo "

".$fromaddress."";
   
}
    
     echo "



-------------

";
    
   }

      
    }


?>

Link to comment
https://forums.phpfreaks.com/topic/116361-imap-regex-help/#findComment-598314
Share on other sites

Sorry 1st post was mistakenly posted before completing my question...and thanks @master, i will now use code tags

 

Friends...

 

I am using IMAP functions in PHP. I want to access Email Server, read all messages in INBOX then display each message subject, its body(contents), and sender Email Address. After doing it, I want to seperate those messages which contain URLs having specific files only, not all URLs.

 

After this I want to display only URLs having those files (v.php & u.php only) in those messages, not complete message. Final output should be like this....

 

1. Message Subject

      http://www.domain.com/u/v.php?id=1&id2=4

      a. v.php

      b. u.php

      From: [email protected]

 

2. Message Subject2

      http://www.domain2com/u/v.php?id=1&id2=4

      a. v.php

      b. u.php

      From: [email protected]

      |

      |

      |

N. Message Subject N

      http://www.domainN.com/u/v.php?id=1&id2=4

      a. v.php

      b. u.php

      From: [email protected]

 

and so on...(Looped)

 

The problem with me is that, I cant manage all these, I wrote code, but as I am new to PHP, I m mixed up.

I am pasting code in next post, which can access INBOX, display each message Subject, URLs from message and From address. but there some problem in nested looping. but it finds me just URLs not URLs of that specific files..

 

I know I m asking too much and even I mixed u people too. but if some one know this, then kindly help me, my project manager is asking me to complete the task quickly.

Link to comment
https://forums.phpfreaks.com/topic/116361-imap-regex-help/#findComment-598325
Share on other sites

<?php
//Connection
$mbox=imap_open("{mail server}","user","paswd") or die("Error! Couldn't Connect");


//read header informations

$msg=imap_mailboxmsginfo($mbox);

//sort messages by date
$message_count = imap_num_msg($mbox);
$messages = imap_sort($mbox, SORTDATE,0);

//function to extract domain from messages
function extract_domains_from($string){
   preg_match_all("/(http:\/\/[a-zA-Z0-9._%-?&\/:]+)/i", $string, $matches);
   return $matches[0];
}


//Loop to display Message Subject, Domain Name and Files
  foreach ($messages as $message) {

   for ($i = 1; $i <= $message_count; ++$i) {

//printing subjects

     $header = imap_header($mbox, $i);
  echo "<br>";
     print "<b>".$header->Subject."</b>";

  //finding bodies
       $header = imap_header($mbox, $i);
       $body = trim(substr(imap_body($mbox, $i), 0));


//function calls to extract domain from message
$domainss = extract_domains_from($body);
echo "<br>";

//Joined all domains and the print
$allurls=(implode(" ",$domainss));

echo "<br><br>";

//Print single URL
$emailmsg=$allurls;

$singleurl=(explode(" ",$allurls));
$size = count($singleurl);
echo "<br><br>";

 for ($u=0; $u<$size; $u++)
 {
$spliturl=(explode("?",$singleurl[$u]));
echo "<br>".$spliturl[0]."<br>";
 }

echo "<br><br>";

for($p=0; $p<$size; $p++)
{
echo "<br><br>";

$emailmsg=$singleurl[$p];

//Removing Last part from URL

if (eregi("http://", $emailmsg))
{
$emailmsg = explode("http://", $emailmsg);
$emailmsg = array_reverse($emailmsg);

$domain = $emailmsg[0];
$domain = explode(".", $domain);
if ($domain[0] == "www")
{
$var=explode("/",$domain[2]);
$extracted = $domain[1].".".$var[0];
echo $extracted;
}
else
{
$var=explode("/",$domain[1]);
$extracted = $domain[0].".".$var[0];
echo $extracted;
}
}
}






//printing from email address  
$header = imap_header($mbox, $i);

$from = $header->from;
foreach ($from as $id => $object) {

   $fromaddress = $object->mailbox . "@" . $object->host;
echo "<br><br><u>".$fromaddress."</u>";

}

  echo "<br><br><br><br>-------------<br><br>";

  }


   }







?>

Link to comment
https://forums.phpfreaks.com/topic/116361-imap-regex-help/#findComment-598326
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.