PHEM (PHP Email Management) is a versatile library designed to simplify email operations in your PHP projects. It provides a unified interface for interacting with SMTP, IMAP, and POP3 protocols, enabling you to send and receive emails with ease. PHEM supports secure connections, authentication, and provides logging for debugging and monitoring.
- Unified Interface: Interact with SMTP, IMAP, and POP3 using a consistent API.
- Secure Connections: Supports TLS/SSL encryption for secure email communication.
- Authentication: Securely authenticate with email servers using usernames and passwords.
- Logging: Detailed logging of email transactions for debugging and analysis.
- Send Emails (SMTP): Easily send emails with support for CC and BCC.
- Receive Emails (IMAP/POP3): Retrieve emails with various filtering options.
Simply include the PHEM.php
file in your project:
require_once 'PHEM.php';
Before using PHEM, you need to configure the connection settings for the desired protocol (SMTP, IMAP, or POP3).
SMTP Configuration:
PHEM::smtp('smtp.example.com', 587, 'tls'); // Host, Port, Security
PHEM::smtpLogin('your_smtp_username', 'your_smtp_password');
IMAP Configuration:
PHEM::imap('imap.example.com', 993, '/ssl'); // Host, Port, Security
PHEM::imapLogin('your_imap_username', 'your_imap_password');
POP3 Configuration:
PHEM::pop('pop.example.com', 995, '/ssl'); // Host, Port, Security
PHEM::popLogin('your_pop_username', 'your_pop_password');
$from = 'sender@example.com';
$name = 'Sender Name';
$to = 'recipient@example.com';
$cc = 'cc@example.com'; // Optional
$bcc = 'bcc@example.com'; // Optional
$subject = 'Email Subject';
$message = 'Email body text';
if (PHEM::smtpSend($from, $name, $to, $cc, $bcc, $subject, $message)['status']) {
echo "Email sent successfully!";
PHEM::showLog(); // Optional: display the SMTP transaction log.
} else {
echo "Email sending failed.";
}
$filter = 'unread'; // See filtering options below.
$limit = 10; // Number of emails to retrieve
$emails = PHEM::imapGet($filter, $limit);
if (!empty($emails)) {
foreach ($emails as $email) {
echo "Subject: " . $email['subject'] . "<br>";
echo "From: " . $email['from'] . "<br>";
echo "Date: " . $email['date'] . "<br>";
echo "Message: " . $email['message'] . "<br><hr>";
}
} else {
echo "No emails found.";
}
Though the library code has a popGet
function, it's declared as private
. If you need POP3 functionality, you would need to modify the code to make popGet
public. Its usage would then be similar to imapGet
:
// Assuming popGet is made public
$filter = 'unread'; // See filtering options below.
$limit = 10; // Number of emails to retrieve
$emails = PHEM::popGet($filter, $limit);
// ... process emails (similar to IMAP example)
The $filter
parameter in the imapGet
and popGet
methods accepts the following filter criteria:
unread
/unseen
: Unread emails.read
/seen
: Read emails.all
: All emails.latest
: Most recent emails.important
/starred
: Flagged emails.spam
: Emails marked as spam.deleted
: Deleted emails.draft
: Draft emailsfrom:{email}
: Emails from a specific address.to:{email}
: Emails sent to a specific address.subject:{text}
: Emails with a specific subject.body:{text}
: Emails containing specific text in the body.before:{date}
: Emails sent before a specific date (format: "DD-Mon-YYYY").since:{date}
: Emails sent since a specific date (format: "DD-Mon-YYYY").on:{date}
: Emails sent on a specific date (format: "DD-Mon-YYYY").- And more (see
getSearchCriteria
function inPHEM.php
for a full list).
To view the SMTP transaction log, call the showLog()
method after sending an email:
PHEM::showLog();
This will output the SMTP communication details in a <pre>
block, which can be helpful for debugging.
Contributions are welcome! If you find any bugs or have suggestions for improvement, please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License.