For some reason, IMAP using office365 is not working
Here error log:
2023/04/27 21:27:16 [error] 77464#77464: *6 FastCGI sent in stderr: "PHP message: PHP Warning: imap_open(): Couldn't open stream {outlook.office365.com:993/imap/readonly/ssl}INBOX in /var/www/html/post.php on line 623" while reading response header from upstream, client: ip, server: domain.tld, request: "POST /post.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.1-fpm.sock:", host: "domain.tld", referrer: "domain.tld/settings_mail.php"
2023/04/27 21:27:16 [error] 77464#77464: *6 FastCGI sent in stderr: "PHP message: PHP Notice: Unknown: Can not authenticate to IMAP server: AUTHENTICATE failed. (errflg=2) in Unknown on line 0" while reading upstream, client: ip, server: domian.tld, request:
tried below based on the error
telent outllok.office365.com 993 - works fine
checked the imap extension - enabled
firewall disabled
using app password or account password - same error - credential works on other appss
changed the imap config to: $imap = imap_open("{{$imap_mailbox}}INBOX", $config_smtp_username, $config_smtp_password, OP_READONLY, 0, ['AUTHENTICATOR' => 'LOGIN']);
and
// Connect with LOGIN authentication method
$imap = imap_open("{{$imap_mailbox}}INBOX", $config_smtp_username, $config_smtp_password, OP_READONLY, 0, ['DISABLE_AUTHENTICATOR' => 'PLAIN']);
- people on google reporting same thing with php imap, so i`m about to try bellow acording to my ChatGPT friend recommendation:
// Validate admin role
validateAdminRole();
// Prepare connection string with encryption (TLS/SSL/<blank>)
$imap_mailbox = "$config_imap_host:$config_imap_port/imap/readonly/$config_imap_encryption";
// Connect to IMAP server
$imap = imap_open("{{$imap_mailbox}}INBOX", $config_imap_username, $config_imap_password);
// Check for errors
if (!$imap) {
die('Could not connect to IMAP server: ' . imap_last_error());
}
// Search for new messages
$message_numbers = imap_search($imap, 'UNSEEN');
// Loop through each message
foreach ($message_numbers as $message_number) {
// Fetch the message headers and body
$headers = imap_headerinfo($imap, $message_number);
$body = imap_body($imap, $message_number);
// Parse the message into a ticket object
$ticket = parseMessageIntoTicket($headers, $body);
// Add the ticket to IT Flow
$result = addTicketToITFlow($ticket);
// Mark the message as read
imap_setflag_full($imap, $message_number, "\\Seen");
}
// Close the connection to the IMAP server
imap_close($imap);
It uses the Microsoft Graph API to access the user's mailbox and retrieve the emails. The retrieved emails can then be parsed and processed to create tickets in ITflow.