Click here to Skip to main content
15,671,318 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to extract all reply-to emails from headers specially from outlook email service , i tried this code , but it not work very slow and extract just 4000 email although i have more than 300000 email in my account.

I want to extract all reply-to headers from all 300000 outlook emails in my account , please do you have some suggestions ?

What I have tried:

$hostname = '{}INBOX';
$username = 'my outlook email';
$password = 'my password';

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to outlook mailbox ' . imap_last_error());

$emails = imap_search($inbox,'ALL');


$n_msgs = imap_num_msg($inbox);

for ($i=1; $i<$n_msgs; $i++) 
  $header = imap_header($inbox ,$i);
  $replyto = $header->reply_to;
  if (is_array($replyto) || is_object($replyto))
      foreach ($replyto as $id => $object) {

       $replytoaddress = $object->mailbox . "@" . $object->host;
       $output  .="\n". $replytoaddress; 
Updated 2-Apr-17 23:32pm
Peter_in_2780 2-Apr-17 18:45pm    
"Reply-to" is an optional header, so you shouldn't expect it in every message.

1 solution

Requesting the headers for about 300,000 IMAP mails will off course require a signifcant amount of time.

My suggestions:

Why do you call imap_search()?
You want to process all messages. Then there is no need to do so. You even don't use the returned array anymore besides calling rsort() which is useless.

I would try (untested):
for ($i = $n_msgs - 1; i >= 0; $i--) 
    $header = imap_header($inbox ,$i);
    // ...
Note that I have changed the loop to start with the highest message number (which you might have tried using rsort()) and that it includes index zero while your code starts at index one.

You might also try to use PHP: imap_headers - Manual[^] instead using a foreach loop and a regular expression to extract the Reply-to header line. This should be faster because it retrieves all headers at once while your solution fetches the headers of each message.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900