PHP code example of wingear / imap-socket-connection
1. Go to this page and download the library: Download wingear/imap-socket-connection library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wingear / imap-socket-connection example snippets
wingear\IMAPSocketConnection;
// Create socket connection. EXAMPLE! Use Google documentation for more correct connection to gmail
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, 'imap.gmail.com', 993);
// Creating IMAP connection
$imap = new IMAPSocketConnection($socket);
// Folder selecting
$imap->selectFolder('INBOX');
// Searching all emails
$messages = $imap->searchMails();
// Get email's headers
$headers = $imap->fetchHeaders($messages[0]);
// Get email's body
$body = $imap->fetchBody($messages[0]);
// Closing connection
$imap->close();
$imap = new IMAPConnection($socket);
// Select folder
$imap->selectFolder('INBOX');
// Get folder list
$folders = $imap->listFolders();
// Get folder info
$info = $imap->getFolderInfo('INBOX');
// Search messages
$messages = $imap->searchMails('UNSEEN'); // Unread messages
$messages = $imap->searchMails('ALL'); // All messages
// Get headers
$headers = $imap->fetchHeaders($messageId);
$headers = $imap->fetchHeaders($messageId, ['From', 'Subject', 'Date']);
// Get message's body
$body = $imap->fetchBody($messageId);
$body = $imap->fetchBody($messageId, false); // Without PEEK (will mark as read)
// Get full message
$fullMessage = $imap->fetchMessage($messageId);
// Get text part
$text = $imap->getMessageText($messageId);
// Returns: ['plain' => '...', 'html' => '...']
// Get exact header fields
$from = $imap->getFrom($messageId);
$to = $imap->getTo($messageId);
$date = $imap->getDate($messageId);
$fields = $imap->getHeaderFields($messageId, ['From', 'Subject']);
// Mark as read
$imap->markAsRead([1, 2, 3]);
// Mark as unread
$imap->markAsUnread([1, 2, 3]);
// Delete message(s)
$imap->deleteMessages([1, 2, 3]);
// Set directory to save attachments
$imap->setAttachmentsDirectory('/path/to/attachments/');
// Get attachments list
$attachments = $imap->getAttachments($messageId);
// Returns: [['filename' => '...', 'data' => '...', 'content_type' => '...'], ...]
// Save attachment
$imap->saveAttachment($messageId, 'document.pdf');