简体   繁体   中英

PHP - IMAP - Is it possible to get receivers email address from a sent email?

I'm trying to retrieve the receivers email address from an email sent from my email client. I use PHP and IMAP to get the job done, but i'm stuck at the moment since i can't get the receivers email address from the email. It only seems possible to get the name of the receiver.

Does anyone know if this can be done and how to do it?

It's possible to get the receiver's email address from the message header information. You need to call the imap_headerinfo() function for each message.

imap_headerinfo($connection, $msg);

This would return an object with all the headers in it like this:

stdClass Object
(
    .
    .
    [to] => Array
        (
            [0] => stdClass Object
                (
                    [mailbox] => testemail
                    [host] => gmail.com
                )
        )
)

You can concatenate the mailbox and host of the to property to get the email of the receiver. Here's an example:

$connection = imap_open($host, $uname, $pwd) or die(imap_last_error());
$messages = imap_search($connection, 'ALL');

foreach($messages as $msg) {
    $header = imap_headerinfo($connection, $msg);

    foreach($header->to as $receiver){
        echo $receiver->mailbox.'@'.$receiver->host.'<br/>';
    }
}

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM