-
-
Notifications
You must be signed in to change notification settings - Fork 183
Open
Description
I'm unsure if it's a bug or user error but I'm trying to retrieve unseen messages, read their subject, post them to discord and mark them as seen in a laravel console command.
Everything seems to be working but when I try to retrieve the subject I get "Cannot parse an empty message".
public function handle()
{
$mailbox = new Mailbox([
'port' => 993,
'username' => '',
'password' => '',
'encryption' => 'ssl',
'host' => 'imap.gmail.com',
]);
$discordWebhook = '';
$mailbox->connect();
if ($mailbox->connected()) {
$inbox = $mailbox->folders()->find('Inbox');
$messageSubjects = [];
try {
$messages = $inbox->messages()->withoutFlags('\Seen')->get();
foreach ($messages as $message) {
try {
$subject = $message->subject() ?? 'No Subject';
$this->info("Found email: $subject");
$messageSubjects[] = $subject;
// Mark message as seen
$message->markSeen();
} catch (\Exception $e) {
$this->warn("Error processing message: " . $e->getMessage());
}
}
$this->info('Connected to mailbox successfully.');
if (count($messageSubjects) > 0) {
$this->table(['Email Subjects'], array_map(function($subject) {
return [$subject];
}, $messageSubjects));
$this->postToDiscord($discordWebhook, $messageSubjects);
} else {
$this->info('No unread emails found.');
$this->postToDiscord($discordWebhook, [], false, true);
}
} catch (\Exception $e) {
$this->error('Error retrieving messages: ' . $e->getMessage());
$this->postToDiscord($discordWebhook, [], true, false, $e->getMessage());
}
return 0;
}
$this->error('Failed to connect to mailbox.');
$this->postToDiscord($discordWebhook, [], true);
return 1;
}Metadata
Metadata
Assignees
Labels
No labels