57 lines
1.9 KiB
PHP
57 lines
1.9 KiB
PHP
<?php
|
|
namespace App\Libs;
|
|
|
|
class ImapService
|
|
{
|
|
protected $connection;
|
|
|
|
public function __construct()
|
|
{
|
|
/*
|
|
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
|
|
$username = "kwesibanson@gmail.com"; // env('IMAP_USERNAME', 'your.email@gmail.com');
|
|
$password = "yutbgxtlqwwepalr"; //env('IMAP_PASSWORD', 'your-app-password');
|
|
*/
|
|
$hostname = '{imap-mail.outlook.com:993/imap/ssl/novalidate-cert}INBOX';
|
|
$username = "kwesi_banson@hotmail.com"; // env('IMAP_USERNAME', 'your.email@gmail.com');
|
|
$password = "uyligiqtreyubvvp"; //"okxixwlqppkpfepy"; //env('IMAP_PASSWORD', 'your-app-password');
|
|
|
|
$this->connection = @imap_open($hostname, $username, $password);
|
|
if (!$this->connection) {
|
|
throw new \Exception('Cannot connect: ' . imap_last_error());
|
|
}
|
|
}
|
|
public function getLatestEmails($limit = 10): array
|
|
{
|
|
$emails = imap_search($this->connection, 'ALL', SE_UID);
|
|
|
|
if (!$emails) {
|
|
return [];
|
|
}
|
|
|
|
rsort($emails); // Newest first
|
|
$result = [];
|
|
|
|
foreach (array_slice($emails, 0, $limit) as $uid) {
|
|
$header = imap_headerinfo($this->connection, imap_msgno($this->connection, $uid), FT_UID);
|
|
$body = imap_fetchbody($this->connection, imap_msgno($this->connection, $uid), 1, FT_UID);
|
|
$body = quoted_printable_decode($body);
|
|
|
|
$result[] = [
|
|
'subject' => isset($header->subject) ? imap_utf8($header->subject) : '(No Subject)',
|
|
'from' => $header->from[0]->mailbox . '@' . $header->from[0]->host,
|
|
'date' => $header->date ?? 'Unknown',
|
|
'body' => substr(strip_tags($body), 0, 200) . '...',
|
|
];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function __destruct()
|
|
{
|
|
if ($this->connection) {
|
|
imap_close($this->connection);
|
|
}
|
|
}
|
|
} |