Have you looked in PHP imap_open? It's pretty easy to setup a conncetion and read all emails. It supports also email search, filter by date, download attachments if any, etc. Simple example I've used in a projec of mine:
Note: My goal was only to get th attachment file, but it is easy to get the title, content and other mail data and store i to database. You can try just the first part and then dump $emails to see what you get as data.
$hostname = {mailServerName :993/imap/ssl}INBOX';
$username = emailAddress';
$password = emailPassword';
// Connect
$inbox = imap_open($hostname, $username, $password);
//Set date
$date = date('d F Y', strtotime('- 1 day'));
// Filter by date
$emails = imap_search($inbox, 'SINCE "' . $date . '"');
if ($emails) {
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach ($emails as $email_number) {
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email_number);
$header = imap_headerinfo($inbox, $email_number);
$attachments = [];
/* if any attachments found... */
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = [
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => '',
];
if ($structure->parts[$i]->ifparameters) {
foreach ($structure->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) === 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
$attachments = $this->etoGetAttachments($attachments, $i, $inbox, $email_number, $structure);
}
}
$j = 0;
/* iterate through each attachment and save it */
foreach ($attachments as $attachment) {
if ($attachment['is_attachment'] == 1) {
$timestamp = strtotime($header->date);
$filename = $this->etoFileName($attachment, $timestamp . '.' . $j);
// External class to store the attachment file
$file->file_name = $filename;
$file->create($user);
}
$j++;
}
}
}
}
public
function etoGetAttachments(array $attachments, int $i, $inbox, $email_number, $structure): array
{
if ($attachments[$i]['is_attachment']) {
$j = $i + 1;
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, '' . $j . '');
/* 3 = BASE64 encoding */
if ($structure->parts[$i]->encoding == 3) {
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
} /* 4 = QUOTED-PRINTABLE encoding */
elseif ($structure->parts[$i]->encoding == 4) {
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
return $attachments;
}
/**
* @param $attachment
* @param $timestamp
*
* @return string
*/
public
function etoFileName($attachment, $timestamp)
{
$filename1 = $attachment['name'];
$filename2 = iconv_mime_decode($filename1, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
//$filename = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $filename);
$filename3 = transliterator_transliterate('Any-Latin;Latin-ASCII;', $filename2);
$filename = $timestamp . '.' . $filename3;
$folder = 'tmp';
if (!is_dir($folder) && !mkdir($folder) && !is_dir($folder)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $folder));
}
$this->etoFopen($folder, $filename, $attachment['attachment']);
return $filename;
}
/**
* @param string $folder
* @param string $filename
* @param string $attachment1
*/
public
function etoFopen(string $folder, string $filename, string $attachment1): void
{
$fp = fopen(DOL_DOCUMENT_ROOT . '/custom/emailtoorder/' . $folder . '/' . $filename, 'wb+');
fwrite($fp, $attachment1);
fclose($fp);
}
/**
* @param resource $inbox
*
* @return string
*/
public
function etoCloseConn($inbox): string
{
imap_close($inbox);
array_map('unlink', glob('tmp/*.mobileconfig'));
return 'all attachment Downloaded';
}
When you have your classes ready you just need a cron job to call the method fetching the emails.