Skip to content

Commit 842a9fd

Browse files
www-dataralflang
authored andcommitted
feat(mail): add XOAUTH2 support to SMTP factory
1 parent 2a3e57c commit 842a9fd

1 file changed

Lines changed: 36 additions & 8 deletions

File tree

lib/Horde/Core/Factory/Mail.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2013-2026 Horde LLC (http://www.horde.org/)
54
*
@@ -101,13 +100,42 @@ public function getConfig()
101100
* running from CLI with 'user_admin' registry flag, which sets
102101
* the authentication name but not the credentials. */
103102
if (strcasecmp($transport, 'smtp') === 0) {
104-
if ($registry->isAuthenticated()
105-
&& strlen((string) ($auth = $registry->getAuth()))) {
106-
if (!empty($params['username_auth'])) {
107-
$params['username'] = $auth;
108-
}
109-
if (!empty($params['password_auth'])) {
110-
$params['password'] = $registry->getAuthCredential('password');
103+
if ($registry->isAuthenticated() &&
104+
strlen((string) ($auth = $registry->getAuth()))) {
105+
/* Try to get SMTP credentials via hook (e.g. for XOAUTH2 support). */
106+
try {
107+
$hooks = $this->_injector->getInstance('Horde_Core_Hooks');
108+
$smtp_creds = $hooks->callHook('smtp_credentials', 'horde', [$auth]);
109+
110+
// Hook returned XOAUTH2 credentials
111+
if (isset($smtp_creds['xoauth2_token'])) {
112+
$params['xoauth2_token'] = $smtp_creds['xoauth2_token'];
113+
if (isset($smtp_creds['username'])) {
114+
$params['username'] = $smtp_creds['username'];
115+
}
116+
// Don't set password when using XOAUTH2
117+
} else {
118+
// Hook returned regular credentials
119+
if (isset($smtp_creds['username'])) {
120+
$params['username'] = $smtp_creds['username'];
121+
} elseif (!empty($params['username_auth'])) {
122+
$params['username'] = $auth;
123+
}
124+
125+
if (isset($smtp_creds['password'])) {
126+
$params['password'] = $smtp_creds['password'];
127+
} elseif (!empty($params['password_auth'])) {
128+
$params['password'] = $registry->getAuthCredential('password');
129+
}
130+
}
131+
} catch (Horde_Exception_HookNotSet $e) {
132+
// No hook defined, use default username/password
133+
if (!empty($params['username_auth'])) {
134+
$params['username'] = $auth;
135+
}
136+
if (!empty($params['password_auth'])) {
137+
$params['password'] = $registry->getAuthCredential('password');
138+
}
111139
}
112140
}
113141

0 commit comments

Comments
 (0)