@@ -134,6 +134,32 @@ public function markAsRead() {
134134 return $ this ;
135135 }
136136
137+ /**
138+ * Perform an imap search request
139+ *
140+ * @return \Illuminate\Support\Collection
141+ * @throws \Webklex\IMAP\Exceptions\ConnectionFailedException
142+ */
143+ protected function search (){
144+ $ this ->generate_query ();
145+
146+ /**
147+ * Don't set the charset if it isn't used - prevent strange outlook mail server errors
148+ * @see https://github.com/Webklex/laravel-imap/issues/100
149+ */
150+ if ($ this ->getCharset () === null ){
151+ $ available_messages = imap_search ($ this ->getClient ()->getConnection (), $ this ->getRawQuery (), IMAP ::SE_UID );
152+ }else {
153+ $ available_messages = imap_search ($ this ->getClient ()->getConnection (), $ this ->getRawQuery (), IMAP ::SE_UID , $ this ->getCharset ());
154+ }
155+
156+ if ($ available_messages !== false ) {
157+ return collect ($ available_messages );
158+ }
159+
160+ return collect ();
161+ }
162+
137163 /**
138164 * Fetch the current query and return all found messages
139165 *
@@ -144,21 +170,13 @@ public function get() {
144170 $ messages = MessageCollection::make ([]);
145171
146172 try {
147- $ this ->generate_query ();
148-
149- /**
150- * Don't set the charset if it isn't used - prevent strange outlook mail server errors
151- * @see https://github.com/Webklex/laravel-imap/issues/100
152- */
153- if ($ this ->getCharset () === null ){
154- $ available_messages = imap_search ($ this ->getClient ()->getConnection (), $ this ->getRawQuery (), IMAP ::SE_UID );
155- }else {
156- $ available_messages = imap_search ($ this ->getClient ()->getConnection (), $ this ->getRawQuery (), IMAP ::SE_UID , $ this ->getCharset ());
157- }
173+ $ available_messages = $ this ->search ();
174+ $ available_messages_count = $ available_messages ->count ();
175+
176+ if ($ available_messages_count > 0 ) {
158177
159- if ( $ available_messages !== false ) {
178+ $ messages -> total ( $ available_messages_count );
160179
161- $ available_messages = collect ($ available_messages );
162180 $ options = config ('imap.options ' );
163181
164182 if (strtolower ($ options ['fetch_order ' ]) === 'desc ' ){
0 commit comments