PHP IMAP retrieve all recipients in To address

荒凉一梦 提交于 2020-01-04 02:52:14

问题


Using PHP IMAP I am able to download all my messages attachments etc from gmail. But when there are multiple recipients in the to address I am only getting a single address. Let me explain it more clearly.

Suppose I have an email address example@gmail.com from where I am downloading all the messages. Now when test@gmail.com sends an email to me I am getting example@gmail.com in the toaddress using function imap_headerinfo. But when test@gmail.com sends the mail to example@gmail.com and example_1@gmail.com then I am not getting this example_1@gmail.com in toaddress or reply_toaddress or cc etc. Below is my sample output for a message

$header = imap_headerinfo($mbox, $msgno);


stdClass Object
(
  [date] => Tue, 10 Sep 2013 13:37:49 +0530
  [Date] => Tue, 10 Sep 2013 13:37:49 +0530
  [subject] => reply all test
  [Subject] => reply all test
  [message_id] => <CACYeE9OEr+9z-Q5BR69sC=nJR_qQps69-VKWxkL6j2hqKEBo2w@mail.gmail.com>
  [toaddress] => example@gmail.com
  [to] => Array
    (
        [0] => stdClass Object
            (
                [mailbox] => example
                [host] => gmail.com
            )

    )

  [fromaddress] => Test <test@gmail.com>
  [from] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test
                [mailbox] => test
                [host] => gmail.com
            )

    )

  [reply_toaddress] => Test <test@gmail.com>
  [reply_to] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test
                [mailbox] => test
                [host] => gmail.com
            )

    )

  [senderaddress] => Test <test@gmail.com>
  [sender] => Array
    (
        [0] => stdClass Object
            (
                [personal] => Test
                [mailbox] => test
                [host] => gmail.com
            )

    )

  [Recent] =>  
  [Unseen] => U
  [Flagged] =>  
  [Answered] =>  
  [Deleted] =>  
  [Draft] =>  
  [Msgno] => 34463
  [MailDate] => 10-Sep-2013 08:07:00 +0000
  [Size] => 2609
  [udate] => 1378800420
)

I want all the recipients address so that I can use them in reply all.

I can clearly see all the to address (AS Cc) in email clients like outlook, thunderbird etc when I click on reply all.


回答1:


Finally found my answer so thought of sharing. Though imap_headerinfo is giving multiple recipients in To address as answered by @Kelu but it was giving only a single address when used with imap_fetch_overview. I was using imap_fetch_overview to get the message number and then imap_headerinfo to get all the header information of that message. This was giving a single recipient address.

After searching a lot the answer was in php.net site only http://www.php.net/manual/en/function.imap-headerinfo.php#98809

Tested with imap_rfc822_parse_headers and imap_fetchheader and it is working fine.




回答2:


Please check out this link: http://www.electrictoolbox.com/php-imap-download-email/

Probably in your case you want to look at the array "to".

[to] => Array
        (
            [0] => stdClass Object
                (
                    [mailbox] => john
                    [host] => example.com
                )
            [1] => stdClass Object
                (
                    [mailbox] => mike
                    [host] => example.com
                )
        )

I'm also attaching my code I used to connect to gmail:

$server = "{imap.gmail.com:993/imap/ssl}INBOX";

$mbox = imap_open($server, $user, $pass) or die('Cannot connect: ' . imap_last_error());

$count = imap_num_msg($mbox);

for ($i = 1; $i <= $count; $i++) {
    $header = imap_headerinfo($mbox, $i);
    if ($header->subject == "Test 123") {
        print_r($header);
    }
}

imap_close($mbox);

Please try using it and try to look for the email you need with the title field. This way you are sure you won't mistake the email you send to one recipent earlier with some newer mails. Also if you have big INBOX, this function might take long to complete - to search for emails you can use function imap_search.



来源:https://stackoverflow.com/questions/18715440/php-imap-retrieve-all-recipients-in-to-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!