mirror of
https://github.com/itflow-org/itflow
synced 2026-11-26 06:17:27 +00:00
Mail: Microsoft 365 sends email and reads the support mailbox through Microsoft Graph when the app on the OAuth tab has Mail.Send or Mail.ReadWrite, so the mailbox needs no SMTP AUTH, and SMTP or IMAP stay in use for whatever it lacks; new functions/mail_graph.php with mailGraphToken caching the Graph token and its scopes and keeping the rotated refresh token, mailGraphSend sending the finished MIME or a draft with uploaded attachments past 3 MB, and functions/mail_graph_mailbox.php standing in for ImapEngine in the email parser; the OAuth tab says which way email goes, Test IMAP and Test OAuth Token Refresh cover Graph, the Exchange token renewals now name their scope, database 3.9.2 adds three settings columns, 26.11.9 changelog entries
This commit is contained in:
34
admin/database_updates/3.9.2.php
Normal file
34
admin/database_updates/3.9.2.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 3.9.2 (from 3.9.1)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*
|
||||
* Microsoft 365 mail through Microsoft Graph (functions/mail_graph.php) - what
|
||||
* Graph last said about the mailbox connected on Admin > Settings > Mail > OAuth:
|
||||
* settings.config_mail_graph_token - the Graph token, NULL when there is none.
|
||||
* settings.config_mail_graph_token_expires_at - when it runs out, or when an
|
||||
* app that couldn't use Graph is asked again; NULL - not asked yet.
|
||||
* settings.config_mail_graph_scopes - the Graph permissions it carries:
|
||||
* Mail.Send sends through Graph, Mail.ReadWrite reads the mailbox through it.
|
||||
*
|
||||
* Re-runnable: each column is added only where it isn't there yet.
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
$itflow_column = function ($table, $column) use ($mysqli) {
|
||||
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' LIMIT 1");
|
||||
return $result && mysqli_num_rows($result) > 0;
|
||||
};
|
||||
|
||||
if (!$itflow_column('settings', 'config_mail_graph_token')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_mail_graph_token` text DEFAULT NULL AFTER `config_mail_oauth_access_token_expires_at`");
|
||||
}
|
||||
if (!$itflow_column('settings', 'config_mail_graph_token_expires_at')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_mail_graph_token_expires_at` datetime DEFAULT NULL AFTER `config_mail_graph_token`");
|
||||
}
|
||||
if (!$itflow_column('settings', 'config_mail_graph_scopes')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_mail_graph_scopes` text DEFAULT NULL AFTER `config_mail_graph_token_expires_at`");
|
||||
}
|
||||
@@ -97,6 +97,9 @@ mysqli_query($mysqli, "UPDATE settings SET
|
||||
WHERE company_id = 1
|
||||
");
|
||||
|
||||
// What Microsoft Graph can do is asked afresh with the new sign-in (functions/mail_graph.php)
|
||||
mailGraphForget();
|
||||
|
||||
logAudit("Settings", "Edit", "$session_name connected Microsoft 365 for mail");
|
||||
flashAlert("Microsoft 365 connected - ITFlow can now use the mailbox");
|
||||
redirect($settings_mail_path);
|
||||
|
||||
@@ -98,6 +98,10 @@ if (isset($_POST['edit_mail_oauth_settings']) || isset($_POST['oauth_connect_mic
|
||||
}
|
||||
|
||||
$changed = settingsWrite('settings', $before, $after);
|
||||
// Graph is asked again too - the app may now have its permissions, or be another app (functions/mail_graph.php)
|
||||
if (array_key_exists('config_mail_oauth_access_token', $after)) {
|
||||
mailGraphForget();
|
||||
}
|
||||
// What it signed in with is only ever fetched - not something anyone changed
|
||||
unset($changed['config_mail_oauth_access_token'], $changed['config_mail_oauth_access_token_expires_at']);
|
||||
$lines = settingsChangeLines($before, $changed);
|
||||
@@ -263,6 +267,12 @@ if (isset($_POST['test_email_imap'])) {
|
||||
|
||||
$provider = escapeSql($config_imap_provider ?? '');
|
||||
|
||||
// Read through Microsoft Graph, the mailbox is tested that way - as the email parser reads it (functions/mail_graph.php)
|
||||
if ($provider === 'microsoft_oauth' && ($graph_test = mailGraphMailboxTest($config_imap_username ?? '')) !== null) {
|
||||
flashAlert($graph_test['ok'] ? escapeHtml($graph_test['message']) : '<strong>Mailbox test failed:</strong> ' . escapeHtml($graph_test['message']), $graph_test['ok'] ? 'success' : 'error');
|
||||
redirect($mail_tab_redirect);
|
||||
}
|
||||
|
||||
$host = $config_imap_host;
|
||||
$port = (int) $config_imap_port;
|
||||
$encryption = strtolower(trim($config_imap_encryption)); // e.g. "ssl", "tls", "none"
|
||||
@@ -360,6 +370,7 @@ if (isset($_POST['test_email_imap'])) {
|
||||
'client_secret' => $config_mail_oauth_client_secret,
|
||||
'refresh_token' => $config_mail_oauth_refresh_token,
|
||||
'grant_type' => 'refresh_token',
|
||||
'scope' => MAIL_MICROSOFT_EXCHANGE_SCOPE,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -463,7 +474,7 @@ if (isset($_POST['test_oauth_token_refresh'])) {
|
||||
'client_secret' => $oauth_client_secret,
|
||||
'refresh_token' => $oauth_refresh_token,
|
||||
'grant_type' => 'refresh_token',
|
||||
]);
|
||||
] + ($provider === 'microsoft_oauth' ? ['scope' => MAIL_MICROSOFT_EXCHANGE_SCOPE] : []));
|
||||
|
||||
$ch = curl_init($token_url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
@@ -507,6 +518,18 @@ if (isset($_POST['test_oauth_token_refresh'])) {
|
||||
$provider_label = $provider === 'microsoft_oauth' ? 'Microsoft 365' : 'Google Workspace';
|
||||
logAudit("Settings", "Edit", "$session_name tested OAuth token refresh for $provider_label mail settings");
|
||||
|
||||
flashAlert("OAuth token refresh successful for $provider_label. Access token expires at $new_expires_at.");
|
||||
// And what Microsoft Graph can do for the mailbox now - asked afresh (functions/mail_graph.php)
|
||||
$graph_said = '';
|
||||
if ($provider === 'microsoft_oauth') {
|
||||
try {
|
||||
$graph_send = mailGraphToken('Mail.Send', true) !== null;
|
||||
$graph_read = mailGraphToken('Mail.ReadWrite') !== null;
|
||||
$graph_said = ' ' . mailGraphSays($config_smtp_provider === 'microsoft_oauth', $config_imap_provider === 'microsoft_oauth', $graph_send, $graph_read);
|
||||
} catch (\Throwable $e) {
|
||||
$graph_said = ' ' . escapeHtml($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
flashAlert("OAuth token refresh successful for $provider_label. Access token expires at $new_expires_at." . $graph_said);
|
||||
redirect($mail_tab_redirect);
|
||||
}
|
||||
|
||||
@@ -290,6 +290,26 @@ if (!$config_module_enable_ticketing) {
|
||||
</span>
|
||||
</div>
|
||||
<div class="itflow-settings-field" id="ms_connect_group" style="display:none;">
|
||||
<?php
|
||||
// Through Microsoft Graph, or by SMTP and IMAP - as Graph last said (functions/mail_graph.php)
|
||||
$graph_state = mailGraphState();
|
||||
$graph_send_ms = $config_smtp_provider === 'microsoft_oauth';
|
||||
$graph_read_ms = $config_imap_provider === 'microsoft_oauth';
|
||||
if ($graph_state && $oauth_connected && ($graph_send_ms || $graph_read_ms)) {
|
||||
$graph_all = (!$graph_send_ms || $graph_state['send']) && (!$graph_read_ms || $graph_state['read']); ?>
|
||||
<div class="itflow-settings-note mb-3" data-mail-graph="<?= !$graph_state['checked'] ? 'unchecked' : ($graph_all ? 'graph' : ($graph_state['send'] || $graph_state['read'] ? 'some' : 'none')) ?>">
|
||||
<?php if (!$graph_state['checked']) { ?>
|
||||
<i class="fas fa-fw fa-clock text-body-secondary me-1"></i>Whether the app can use Microsoft Graph is checked when ITFlow next sends or reads mail - or test the sign-in on Tests.
|
||||
<?php } else { ?>
|
||||
<i class="fas fa-fw <?= $graph_all ? 'fa-check-circle text-success' : 'fa-info-circle text-info' ?> me-1"></i><?= escapeHtml(mailGraphSays($graph_send_ms, $graph_read_ms, $graph_state['send'], $graph_state['read'])) ?>
|
||||
<?php if (!$graph_all) {
|
||||
$graph_missing = array_filter(['Mail.Send' => $graph_send_ms && !$graph_state['send'], 'Mail.ReadWrite' => $graph_read_ms && !$graph_state['read']]);
|
||||
$graph_spared = array_filter(['SMTP AUTH' => isset($graph_missing['Mail.Send']), 'IMAP' => isset($graph_missing['Mail.ReadWrite'])]); ?>
|
||||
Give the app Microsoft Graph's <?= implode(' and ', array_keys($graph_missing)) ?> permission<?= count($graph_missing) > 1 ? 's' : '' ?> to go through Graph instead, and the mailbox needs no <?= implode(' or ', array_keys($graph_spared)) ?> - then test the sign-in on Tests.
|
||||
<?php } ?>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?php } ?>
|
||||
<label class="form-label" for="mailOauthRedirect">Redirect address</label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fa fa-fw fa-link"></i></span>
|
||||
@@ -727,7 +747,7 @@ if (!$config_module_enable_ticketing) {
|
||||
setHtml(oauthHint, '<i class="fas fa-fw fa-info-circle me-1"></i>' + (anyMs && anyGoogle
|
||||
? 'Sending and receiving each pick a different one - both have to use the same, as they share this app.'
|
||||
: anyMs
|
||||
? 'Microsoft 365: register an app in Microsoft Entra (App registrations) with the redirect address below, the IMAP.AccessAsUser.All and SMTP.Send permissions, and a client secret. Its ID, secret and tenant go here.'
|
||||
? 'Microsoft 365: register an app in Microsoft Entra (App registrations) with the redirect address below and a client secret, and give it Microsoft Graph\'s Mail.Send and Mail.ReadWrite permissions (with Mail.Send.Shared and Mail.ReadWrite.Shared for a shared mailbox) - or Exchange\'s SMTP.Send and IMAP.AccessAsUser.All. Its ID, secret and tenant go here.'
|
||||
: anyGoogle
|
||||
? 'Google Workspace: make an OAuth client ID (a Web application) in Google Cloud. Its ID and secret go here; the refresh token needs the https://mail.google.com/ scope.'
|
||||
: 'Used when Sending or Receiving is set to Google Workspace or Microsoft 365.'));
|
||||
|
||||
Reference in New Issue
Block a user