Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david19's avatar

Create Collection for PHP IMAP

Hello Team. I can receive emails with attachments and inline images. That works great. But i like create a collection, before render the emails.

To get all the emails i use this:

$this->mails = imap_sort($this->connection, SORTDATE, 1,SE_UID);

This give me a array of all the emails with the UID. Then i can collect this, for create collection.

$this->mails = collect($this->mails);
Illuminate\Support\Collection {#1686 ▼
  #items: array:51 [▼
    0 => 119
    1 => 118
    2 => 117
    3 => 116
    4 => 115
    5 => 114
    6 => 113
    7 => 112
    8 => 111
    9 => 110
    10 => 109
    11 => 108
    12 => 107
    13 => 106
    14 => 105
    15 => 104
    16 => 103
    17 => 102
    18 => 101
    19 => 100
    20 => 99
    21 => 98
    22 => 97
    23 => 96
    24 => 95
    25 => 94
    26 => 93
    27 => 92
    28 => 91
    29 => 90
    30 => 89
    31 => 88
    32 => 87
    33 => 86
    34 => 85
    35 => 84
    36 => 83
    37 => 82
    38 => 81
    39 => 80
    40 => 79
    41 => 78
    42 => 77
    43 => 76
    44 => 75
    45 => 74
    46 => 73
    47 => 72
    48 => 71
    49 => 70
    50 => 69
  ]
}

Now i will add the sender and subjects, date, and more to this collection. Example: With this function, i can fetch the subject of the mail:

public function getSubject($iteration)
      {
       
        $this->subject = imap_headerinfo($this->connection, $iteration)->subject;
      
        
        
      }

How i can add the "subject" to my collection? Why collection? Because then, i can use pagination easy and all the collection functions. Many thanks :)

0 likes
0 replies

Please or to participate in this conversation.