11<?php
22
3- /**
4- * Example: Get and parse all emails which match the subject "part of the subject" with saving their attachments.
5- *
6- * @author Sebastian Krätzig <info@ts3-tools.info>
7- */
8-
9- require_once __DIR__ . '/../vendor/autoload.php ' ;
10- use PhpImap \Mailbox ;
3+ /**
4+ * Example: Get and parse all emails which match the subject "part of the subject" with saving their attachments.
5+ *
6+ * @author Sebastian Krätzig <info@ts3-tools.info>
7+ */
8+ require_once __DIR__ .'/../vendor/autoload.php ' ;
119 use PhpImap \Exceptions \ConnectionException ;
10+ use PhpImap \Mailbox ;
1211
1312 $ mailbox = new Mailbox (
1413 '{imap.gmail.com:993/imap/ssl}INBOX ' , // IMAP server and mailbox folder
1514 'some@gmail.com ' , // Username for the before configured mailbox
1615 '********* ' , // Password for the before configured username
1716 __DIR__ , // Directory, where attachments will be saved (optional)
18- 'US-ASCII ' // Server encoding (optional)
17+ 'US-ASCII ' // Server encoding (optional)
1918 );
2019
2120 try {
2221 $ mail_ids = $ mailbox ->searchMailbox ('SUBJECT "part of the subject" ' );
23- } catch (ConnectionException $ ex ) {
24- die (" IMAP connection failed: " . $ ex ->getMessage ());
22+ } catch (ConnectionException $ ex ) {
23+ die (' IMAP connection failed: ' . $ ex ->getMessage ());
2524 } catch (Exception $ ex ) {
26- die (" An error occured: " . $ ex ->getMessage ());
25+ die (' An error occured: ' . $ ex ->getMessage ());
2726 }
2827
2928 foreach ($ mail_ids as $ mail_id ) {
3433 false // Do NOT mark emails as seen (optional)
3534 );
3635
37- echo " from-name: " . (isset ($ email ->fromName )) ? $ email ->fromName : $ email ->fromAddress . "\n" ;
38- echo " from-email: " . $ email ->fromAddress . "\n" ;
39- echo " to: " . $ email ->to . "\n" ;
40- echo " subject: " . $ email ->subject . "\n" ;
41- echo " message_id: " . $ email ->messageId . "\n" ;
42-
43- echo " mail has attachments? " ;
36+ echo ' from-name: ' .( string ) (isset ($ email ->fromName ) ? $ email ->fromName : $ email ->fromAddress ). "\n" ;
37+ echo ' from-email: ' .( string ) $ email ->fromAddress . "\n" ;
38+ echo ' to: ' .( string ) $ email ->toString . "\n" ;
39+ echo ' subject: ' .( string ) $ email ->subject . "\n" ;
40+ echo ' message_id: ' .( string ) $ email ->messageId . "\n" ;
41+
42+ echo ' mail has attachments? ' ;
4443 if ($ email ->hasAttachments ()) {
4544 echo "Yes \n" ;
4645 } else {
4746 echo "No \n" ;
4847 }
49-
48+
5049 if (!empty ($ email ->getAttachments ())) {
51- echo count ($ email ->getAttachments ()) . " attachements \n" ;
50+ echo \ count ($ email ->getAttachments ()). " attachements \n" ;
5251 }
5352 if ($ email ->textHtml ) {
54- echo "Message HTML: \n" . $ email ->textHtml ;
53+ echo "Message HTML: \n" . $ email ->textHtml ;
5554 } else {
56- echo "Message Plain: \n" . $ email ->textPlain ;
55+ echo "Message Plain: \n" . $ email ->textPlain ;
5756 }
58-
57+
5958 if (!empty ($ email ->autoSubmitted )) {
6059 // Mark email as "read" / "seen"
6160 $ mailbox ->markMailAsRead ($ mail_id );
62- echo "+------ IGNORING: Auto-Reply ------+ \n" ;
61+ echo "+------ IGNORING: Auto-Reply ------+ \n" ;
6362 }
64-
63+
6564 if (!empty ($ email_content ->precedence )) {
6665 // Mark email as "read" / "seen"
6766 $ mailbox ->markMailAsRead ($ mail_id );
6867 echo "+------ IGNORING: Non-Delivery Report/Receipt ------+ \n" ;
69- }
68+ }
7069 }
7170
72- $ mailbox ->disconnect ();
71+ $ mailbox ->disconnect ();
0 commit comments