20

How do I retrieve the email address from an email with imap_open?

If the sender name is known I get the sender name instead of the email address if I use the 'from' parameter.

Code: http://gist.github.com/514207

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
Frederik Heyninck
  • 221
  • 1
  • 2
  • 4

7 Answers7

44
$header = imap_headerinfo($imap_conn, $msgnum);
$fromaddr = $header->from[0]->mailbox . "@" . $header->from[0]->host;
dlo
  • 1,580
  • 15
  • 24
  • How would this be achieved using Zend_Mail ? – beingalex Jun 27 '12 at 17:29
  • Isn't Zend_Mail for composing and *sending* emails? This question is about extracting an address from a received message, in this case accessed via imap. – dlo Jul 11 '12 at 19:16
4

I battled with this as well but the following works:

// Get email address
$header = imap_header($imap, $result); // get first mails header
echo '<p>Name: ' . $header->fromaddress . '<p>';
echo '<p>Email: ' . $header->senderaddress . '<p>';

I had used imap_fetch_overview() but the imap_header() gave me all the information I needed.

andrebruton
  • 2,268
  • 4
  • 29
  • 36
3

Worst case, you can parse the headers yourself with something like:

<?php
$headers=imap_fetchheader($imap, $msgid);
preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $headers, $matches);
?>

$matches will contain 3 arrays:

$matches[0] are the full-lines (such as "To: user@user.com\r\n")
$matches[1] will be the header (such as "To")
$matches[2] will be the value (user@user.com)

Got this from: http://www.php.net/manual/en/function.imap-fetchheader.php#82339

Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • This is my header: MIME-Version: 1.0 Received: by 10.227.37.212; Wed, 21 Jul 2010 12:21:40 -0700 (PDT) Date: Wed, 21 Jul 2010 12:21:40 -0700 Message-ID: Subject: Customise Gmail with colours and themes From: Gmail Team To: Frederik Heyninck Content-Type: multipart/alternative; boundary=0016e6d5fc53164fb6048beab667 So no email address. That is the problem. – Frederik Heyninck Aug 09 '10 at 21:43
2

Had same issue as you....had to piece it together, don't know why it's such gonzoware.

Untested example here:

$mbox = imap_open(....)

$MN=$MC->Nmsgs;
$overview=imap_fetch_overview($mbox,"1:$MN",0);
$size=sizeof($overview);
for($i=$size-1;$i>=0;$i--){
    $val=$overview[$i];
    $msg=$val->msgno;
    $header = imap_headerinfo ( $mbox, $msg);
    echo '<p>Name  / Email Address: ' . $header->from[0]->personal ." ".
    $header->from[0]->mailbox ."@". $header->from[0]->host. '<p></br>';
}
imap_close($mbox);
0

imap_fetch_overview could be what you're looking for: http://www.php.net/manual/en/function.imap-fetch-overview.php

An example of use can be found here: http://davidwalsh.name/gmail-php-imap, specifically

echo $overview[0]->from;

This function is simple, but has limitations. A more exhaustive version is in imap_headerinfo ( http://www.php.net/manual/en/function.imap-headerinfo.php ) which can return detailed arrays of all header data.

Ben
  • 54,723
  • 49
  • 178
  • 224
0

Had trouble until I spotted that the $header is an array of stdClass Objects. The following 2 lines worked:

    $header=imap_fetch_overview($imap,$countClients,FT_UID);
    $strAddress_Sender=$header[0]->from;
walter1957
  • 49
  • 1
  • 10
0

Full working code with an online example

Extract email addresses list from inbox using PHP and IMAP inbox-using-php-and-imap

I think all you need is just to copy the script.

I am publishing two core functions of the code here as well (thanks to Eineki's comment)

         function getAddressText(&$emailList, &$nameList, $addressObject) { 
            $emailList = '';
            $nameList = '';
            foreach ($addressObject as $object) {
                $emailList .= ';';
                if (isset($object->personal)) { 
                     $emailList .= $object->personal;
                } 
                $nameList .= ';';
                if (isset($object->mailbox) && isset($object->host)) { 
                    $nameList .= $object->mailbox . "@" . $object->host;
                }    
            }    
            $emailList = ltrim($emailList, ';');
            $nameList = ltrim($nameList, ';');
        } 

        function processMessage($mbox, $messageNumber) { 
            echo $messageNumber;
            // get imap_fetch header and put single lines into array
            $header = imap_rfc822_parse_headers(imap_fetchheader($mbox, $messageNumber));
            $fromEmailList = '';
            $fromNameList = '';
            if (isset($header->from)) { 
                getAddressText($fromEmailList, $fromNameList, $header->from); 
            }
            $toEmailList = '';
            $toNameList = '';
            if (isset($header->to)) {
                getAddressText($toEmailList, $toNameList, $header->to); 
            }    
            $body = imap_fetchbody($mbox, $messageNumber, 1);
            $bodyEmailList = implode(';', extractEmail($body));
            print_r(
               ',' . $fromEmailList . ',' . $fromNameList 
                . ',' . $toEmailList . ',' . $toNameList 
                . ',' . $bodyEmailList . "\n"
            );
        } 
Eugene Lycenok
  • 603
  • 6
  • 14
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18526256) – Eineki Jan 16 '18 at 02:02
  • Done. Appreciate the review! – Eugene Lycenok Jan 16 '18 at 02:16