PHP code example of theseer / imapstore

1. Go to this page and download the library: Download theseer/imapstore 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/ */

    

theseer / imapstore example snippets


 declare(strict_types=1);

use theseer\imapstore\Foldername;
use theseer\imapstore\ImapStore;
use theseer\imapstore\LoginAuthenticator;
use theseer\imapstore\Message;
use theseer\imapstore\MessageFlag;
use theseer\imapstore\TCPConnection;

 text/plain; charset=UTF-8\r\n\r\n";
$emailData .= "This is the message body.\r\n";

// Create the IMAP store connection
$store = new ImapStore(
    TCPConnection::createTLS('imap.example.com'),
    new LoginAuthenticator('[email protected]', 'password')
);

// Store the message in the INBOX folder with SEEN flag
$store->store(
    Message::fromString($emailData),
    Foldername::fromString('INBOX'),
    MessageFlag::SEEN
);

// Store in different folders
$store->store(
    Message::fromString($emailData),
    Foldername::fromString('Sent'),
    MessageFlag::SEEN
);

$store->store(
    Message::fromString($emailData),
    Foldername::fromString('Drafts'),
    MessageFlag::DRAFT
);

// For non-TLS connections (not recommended for production)
// Uses default port 143
$store = new ImapStore(
    TCPConnection::createPlain('imap.example.com'),
    new LoginAuthenticator('[email protected]', 'password')
);

// Using custom ports
$store = new ImapStore(
    TCPConnection::createTLS('imap.example.com', 1993),
    new LoginAuthenticator('[email protected]', 'password')
);

// Custom connection implementations can be created if needed
// by implementing the Connection interface

// The library currently supports LOGIN authentication
// Custom authentication methods can be implemented
// by implementing the Authenticator interface

$customAuth = new CustomAuthenticator($credentials);
$store = new ImapStore(
    TCPConnection::createTLS('imap.example.com'),
    $customAuth
);

// Store message with multiple flags using variadics
$store->store(
    Message::fromString($emailData),
    Foldername::fromString('INBOX'),
    MessageFlag::SEEN,
    MessageFlag::FLAGGED
);

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer();

// ...

$mail->setFrom('[email protected]', 'John Doe');
$mail->addAddress('[email protected]', 'Jane Smith');
$mail->Subject = 'Generated Email';
$mail->Body = 'This email was generated using PHPMailer and stored via ImapStore.';

// Potentially sent your mail using PHPMailer here 

// Store it using ImapStore
$store->store(
    Message::fromString($mail->getSentMIMEMessage()),
    Foldername::fromString('Sent'),
    MessageFlag::SEEN
);

try {
    $store->store(
        Message::fromString($emailData),
        Foldername::fromString('INBOX'),
        MessageFlag::SEEN
    );
    echo "Message stored successfully\n";
} catch (Exception $e) {
    echo "Error storing message: " . $e->getMessage() . "\n";
}