1. Go to this page and download the library: Download yaijs/php-ymap 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/ */
yaijs / php-ymap example snippets
use Yai\Ymap\ConnectionConfig;
use Yai\Ymap\ImapClient;
$config = new ConnectionConfig(
'{imap.gmail.com:993/imap/ssl}INBOX',
'[email protected]',
'app-password'
);
$client = new ImapClient($config);
$client->connect();
foreach ($client->getUnreadUids() as $uid) {
$message = $client->fetchMessage($uid);
echo $message->getSubject();
$client->markAsRead($uid);
}
use Yai\Ymap\FetchOptions;
// Lightweight: Only metadata for inbox listings
$options = new FetchOptions(
Critical for large attachments!
);
$messages = $service->getMessages($options);
// In your service configuration (e.g., services.xml, services.yaml)
<service id="YourApp\Service\EmailProcessorService">
<argument type="service" id="Yai\Ymap\ImapService"/>
</service>
// In your scheduled task or background job
class EmailProcessorTask
{
public function run(): void
{
$messages = $this->imapService
->fields(['uid', 'subject', 'from', 'preview'])
->limit(20) // Process in batches
->unreadOnly()
->getMessages();
foreach ($messages as $msg) {
// Process message...
$this->imapService->markAsRead($msg['uid']);
}
}
}
use Yai\Ymap\ImapClient;
use Yai\Ymap\ConnectionConfig;
$config = new ConnectionConfig(
'{imap.gmail.com:993/imap/ssl}INBOX',
'[email protected]',
'app-password'
);
$client = new ImapClient($config);
$client->connect();
$message = $client->fetchMessage(12345);
foreach ($message->getAttachments() as $attachment) {
// Stream directly to the filesystem (no giant strings in memory)
$client->saveAttachmentTo(
$message->getUid(),
$attachment,
'/tmp/' . $attachment->getFilename()
);
// Or access the content lazily
if ($attachment->getMimeType() === 'application/pdf') {
processPdf($attachment->getContent());
}
if ($attachment->isInline()) {
$contentId = $attachment->getContentId(); // For referencing in HTML
}
}
use Yai\Ymap\ImapClient;
use Yai\Ymap\Connection\ImapConnectionInterface;
// Create a mock connection (PHPUnit example)
$mockConnection = $this->createMock(ImapConnectionInterface::class);
$mockConnection->method('search')
->willReturn([1, 2, 3]);
// Inject into ImapClient
$client = new ImapClient($config, connection: $mockConnection);
// Now $client->searchUIDs() returns mocked data
use Yai\Ymap\ImapService;
use Yai\Ymap\ImapClientInterface;
$service = ImapService::create()
->connect('{imap.host:993/imap/ssl}INBOX', '[email protected]', 'secret')
->useClient($container->get(ImapClientInterface::class));
use Yai\Ymap\Connection\ExtImapConnection; // Optional: wraps ext-imap
use Yai\Ymap\Connection\SocketsImapConnection; // Default: pure PHP socket connector
// Default in v1.0.3+: socket connector
$client = new ImapClient($config); // Uses SocketsImapConnection
// Optional override to native extension connector
$client = new ImapClient($config, connection: new ExtImapConnection());
use Yai\Ymap\Exceptions\ConnectionException;
use Yai\Ymap\Exceptions\MessageFetchException;
try {
$messages = $imap->getMessages();
} catch (ConnectionException $e) {
// Invalid credentials, TLS failure, server unreachable, etc.
} catch (MessageFetchException $e) {
// Individual message could not be parsed/fetched
}
// Optional: capture per-message failures instead of silently skipping
$imap->onError(static function (int $uid, \Throwable $e): void {
error_log(sprintf('Failed to fetch UID %d: %s', $uid, $e->getMessage()));
});