https://github.com/theseer/imapstore
Minimalistic Imap Client to Store Messages on an IMAP Server
https://github.com/theseer/imapstore
Last synced: 11 months ago
JSON representation
Minimalistic Imap Client to Store Messages on an IMAP Server
- Host: GitHub
- URL: https://github.com/theseer/imapstore
- Owner: theseer
- License: bsd-3-clause
- Created: 2025-06-16T08:07:34.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-27T11:48:16.000Z (about 1 year ago)
- Last Synced: 2025-07-28T02:52:42.866Z (12 months ago)
- Language: PHP
- Size: 43 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ImapStore
A minimalistic PHP IMAP client library specifically designed for storing email messages on IMAP servers. This library provides a clean, object-oriented interface for connecting to IMAP servers and storing messages with appropriate flags.
## Features
- **Lightweight**: Focused solely on storing messages, not retrieving them
- **Secure**: Supports TLS/SSL connections
- **Type-safe**: Built with strict PHP type declarations
- **Simple API**: Easy-to-use object-oriented interface
- **Flexible Authentication**: Currently supports LOGIN authentication with extensible architecture for custom authentication methods
- **Message Flags**: Set message flags (like SEEN) when storing
## Installation
Install via Composer:
```bash
composer require theseer/imapstore
```
## Requirements
- PHP 8.4 or higher
- No additional PHP extensions required
## Usage
### Basic Usage
```php
store(
Message::fromString($emailData),
Foldername::fromString('INBOX'),
MessageFlag::SEEN
);
```
### Advanced Usage
#### Different Folder Destinations
```php
// Store in different folders
$store->store(
Message::fromString($emailData),
Foldername::fromString('Sent'),
MessageFlag::SEEN
);
$store->store(
Message::fromString($emailData),
Foldername::fromString('Drafts'),
MessageFlag::DRAFT
);
```
#### Non-TLS Connections
```php
// For non-TLS connections (not recommended for production)
// Uses default port 143
$store = new ImapStore(
TCPConnection::createPlain('imap.example.com'),
new LoginAuthenticator('username@example.com', 'password')
);
```
#### Custom Ports and Connections
```php
// Using custom ports
$store = new ImapStore(
TCPConnection::createTLS('imap.example.com', 1993),
new LoginAuthenticator('username@example.com', 'password')
);
// Custom connection implementations can be created if needed
// by implementing the Connection interface
```
#### Custom Authentication
```php
// 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
);
```
#### Multiple Message Flags
```php
// Store message with multiple flags using variadics
$store->store(
Message::fromString($emailData),
Foldername::fromString('INBOX'),
MessageFlag::SEEN,
MessageFlag::FLAGGED
);
```
#### Using PHPMailer to Generate Messages
```php
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
// ...
$mail->setFrom('sender@example.com', 'John Doe');
$mail->addAddress('recipient@example.com', '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
);
```
## Message Flags
The library supports the following message flags that can be set when storing messages:
- `MessageFlag::SEEN` - Mark message as read
- `MessageFlag::ANSWERED` - Mark message as answered
- `MessageFlag::FLAGGED` - Mark message as flagged/important
- `MessageFlag::DELETED` - Mark message as deleted
- `MessageFlag::DRAFT` - Mark message as draft
## Error Handling
The library may throw exceptions for various error conditions. It's recommended to wrap your code in try-catch blocks:
```php
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";
}
```
## Security Considerations
- Always use TLS/SSL connections when possible (`TCPConnection::createTLS()`)
- Store credentials securely (consider using environment variables)
- Use strong authentication methods
## Alternatives and Complementary Libraries
- **[DirectoryTree/ImapEngine](https://packagist.org/packages/directorytree/imapengine)** - Full-featured IMAP client when you need comprehensive email operations (reading, searching, managing)
- **[ddeboer/imap](https://github.com/ddeboer/imap)** - Alternative full IMAP client library for reading and managing emails
- **[PHPMailer](https://github.com/PHPMailer/PHPMailer)** - Ideal for generating properly formatted email messages that can then be stored using ImapStore