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');

// Execute any IMAP command
$response = $imap->executeCommand('CAPABILITY');
$response = $imap->executeCommand('LOGIN "user" "password"');


wingear\IMAPSocketConnection;

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, 'imap.example.com', 143);

$imap = new IMAPSocketConnection($socket);
$imap->executeCommand('LOGIN "[email protected]" "password"');
$imap->selectFolder('INBOX');

$unreadMessages = $imap->searchMails('UNSEEN');

foreach ($unreadMessages as $messageId) {
    $from = $imap->getFrom($messageId);
    $subject = $imap->getHeaderFields($messageId, ['Subject']);
    $text = $imap->getMessageText($messageId);
    
    echo "From: $from\n";
    echo "Subject: " . $subject['Subject'] . "\n";
    echo "Text: " . substr($text['plain'], 0, 100) . "...\n\n";
}

$imap->close();


$imap->setAttachmentsDirectory('./downloads/');
$attachments = $imap->getAttachments($messageId);

foreach ($attachments as $attachment) {
    echo "Found attachment: " . $attachment['filename'] . "\n";
    $imap->saveAttachment($messageId, $attachment['filename']);
}

// Good
$password = $_ENV['EMAIL_PASSWORD'];

// Bad
$password = 'my-secret-password';

$context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ]
]);

$socket = stream_socket_client('ssl://imap.gmail.com:993', $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);