Skip to content

Instantly share code, notes, and snippets.

@seeschloss
Created November 22, 2024 15:50
Show Gist options
  • Save seeschloss/2cfe1cdd3322ba459988e689fb1c38d2 to your computer and use it in GitHub Desktop.
Save seeschloss/2cfe1cdd3322ba459988e689fb1c38d2 to your computer and use it in GitHub Desktop.
<?php
$config = [
'mailbox' => '{ssl0.ovh.net:993/imap/ssl}INBOX',
'user' => 'user@domain',
'password' => 'plop',
];
$filters = [
[ 'condition' => fn($headers) => isset($headers->subject) and strpos($headers->subject, "[sysadmin]") === 0 and strpos($headers->subject, "CRITICAL") === false,
'destination' => 'sysadmin'
],
[ 'condition' => fn($headers) => isset($headers->subject) and strpos($headers->subject, "[dev]") === 0,
'destination' => 'dev',
],
];
<?php
require __DIR__.'/cfg/config.inc.php';
$state_file = __DIR__.'/var/state';
if (!file_exists(__DIR__.'/var')) {
mkdir(__DIR__.'/var');
}
if (!file_exists($state_file)) {
file_put_contents($state_file, '0');
}
$latest_id = file_get_contents($state_file);
$box = imap_open($config['mailbox'], $config['user'], $config['password']);
$list = imap_sort($box, SORTARRIVAL, 1);
file_put_contents($state_file, $list[0]);
foreach ($list as $id) {
if ($id <= $latest_id) {
break;
}
filter_message($box, $id);
}
function filter_message($box, $id) {
global $filters;
$info = imap_headerinfo($box, $id);
$info->subject = imap_utf8($info->subject);
foreach ($filters as $filter) {
if ($filter['condition']($info)) {
if (!empty($filter['destination'])) {
echo "Moving message '{$info->subject}' to '{$filter['destination']}'\n";
imap_mail_move($box, $id, $filter['destination']);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment