{"id":1494,"date":"2024-03-28T09:59:34","date_gmt":"2024-03-28T08:59:34","guid":{"rendered":"https:\/\/wiki.ad-libitum.info\/?page_id=1494"},"modified":"2024-03-28T10:05:07","modified_gmt":"2024-03-28T09:05:07","slug":"office365-und-oauth","status":"publish","type":"page","link":"https:\/\/wiki.ad-libitum.info\/?page_id=1494","title":{"rendered":"Office365 und OAuth"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Um eMails mit Hilfe von PHPMailer \u00fcber Office365 versenden zu k\u00f6nnen, sind einige Einstellungen vorzunehmen und es ist einiges zu beachten.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">AZURE ACTIVE DIRECTORY<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following steps register a new app in Azure Active Directory:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-34.png\" alt=\"\" class=\"wp-image-3587\"\/><figcaption class=\"wp-element-caption\">Login to&nbsp;<a href=\"https:\/\/portal.azure.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/portal.azure.com<\/a><\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-35.png\" alt=\"\" class=\"wp-image-3588\"\/><figcaption class=\"wp-element-caption\">Open the &#8222;Azure Active Directory&#8220;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-36.png\" alt=\"\" class=\"wp-image-3589\"\/><figcaption class=\"wp-element-caption\">Select &#8222;App registrations&#8220; &amp; &#8222;New registration&#8220;.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-37.png\" alt=\"\" class=\"wp-image-3590\"\/><figcaption class=\"wp-element-caption\">Copy &#8222;Application ID (Client)&#8220; (=Client ID) &amp; &#8222;Directory ID (Tenant)&#8220; (=Tenant ID).<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-38.png\" alt=\"\" class=\"wp-image-3591\"\/><figcaption class=\"wp-element-caption\">&#8222;API Permissions&#8220; &amp; &#8222;Add Permission&#8220;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-39.png\" alt=\"\" class=\"wp-image-3592\"\/><figcaption class=\"wp-element-caption\">&#8222;APIs used by my organization&#8220; &amp; &#8222;Office 365 Exchange Online&#8220;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-41.png\" alt=\"\" class=\"wp-image-3594\"\/><figcaption class=\"wp-element-caption\">&#8222;Application Permissions&#8220; &amp; &#8222;IMAP.AccessAsApp&#8220;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-42.png\" alt=\"\" class=\"wp-image-3595\"\/><figcaption class=\"wp-element-caption\">Grant admin consent<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-43.png\" alt=\"\" class=\"wp-image-3596\"\/><figcaption class=\"wp-element-caption\">&#8222;Certificates &amp; Secrets&#8220; &amp; &#8222;Client Secrets&#8220; &amp; &#8222;New Client Secret&#8220;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-44.png\" alt=\"\" class=\"wp-image-3597\"\/><figcaption class=\"wp-element-caption\">Choose description and set validity<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-45.png\" alt=\"\" class=\"wp-image-3598\"\/><figcaption class=\"wp-element-caption\">Copy &#8222;Secret ID&#8220; (Client Secret) to the clipboard<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-46.png\" alt=\"\" class=\"wp-image-3599\"\/><figcaption class=\"wp-element-caption\">Open enterprise applications<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/vielhuber.de\/wp-content\/uploads\/image-47.png\" alt=\"\" class=\"wp-image-3600\"\/><figcaption class=\"wp-element-caption\">Copy &#8222;Object ID&#8220;.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Was in dieser Anleitung fehlt, ist die Redirect URI. Diese muss zwingend eingetragen werden, sonst erfolgt eine entsprechende Fehlermeldung. Wo diese eingetragen muss, wei\u00df ich nicht. Aber hier die Stelle:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"252\" src=\"https:\/\/wiki.ad-libitum.info\/wp-content\/uploads\/URI.png\" alt=\"\" class=\"wp-image-1495\" srcset=\"https:\/\/wiki.ad-libitum.info\/wp-content\/uploads\/URI.png 921w, https:\/\/wiki.ad-libitum.info\/wp-content\/uploads\/URI-300x82.png 300w, https:\/\/wiki.ad-libitum.info\/wp-content\/uploads\/URI-768x210.png 768w\" sizes=\"auto, (max-width: 921px) 100vw, 921px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Bei der Redirect-URI ist \u00fcbrigens unerheblich, ob diese \u00f6ffentlich erreichbar ist oder nicht. Sie muss lediglich auf dem lokalen System erreichbar sein. Und es k\u00f6nnen mehrere URIs eingetragen werden. Auf Systemen ohne SSL die URI trotzdem mit https eintragen und auch so aufrufen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>composer require phpmailer\/phpmailer<br>composer require thenetworg\/oauth2-azure<br>composer require greew\/oauth2-azure-provider --with-all-dependencies<br>composer require stevenmaguire\/oauth2-microsoft<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Erster Test<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Als erster Funktionstest sollte das Skript <code>get_oauth_token.php<\/code> ausgef\u00fchrt werden. Nur wenn alles richtig eingestellt ist, wird man einen sogenannten Refresh Token zur\u00fcck bekommen. Und als Provider muss <strong>Azure<\/strong> gew\u00e4hlt werden! Dieser wird f\u00fcr die weitere Arbeit zwingend ben\u00f6tigt.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP-Code<\/h2>\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\"><code>&lt;?php<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Import PHPMailer classes into the global namespace<br>use PHPMailer\\PHPMailer\\PHPMailer;<br>use PHPMailer\\PHPMailer\\SMTP;<br>use PHPMailer\\PHPMailer\\OAuth;<br>\/\/Alias the League Google OAuth2 provider class<br>use Greew\\OAuth2\\Client\\Provider\\Azure;<\/code><\/p>\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\"><code>\/\/SMTP needs accurate times, and the PHP time zone MUST be set<br>\/\/This should be done in your php.ini, but this is how to do it if you don't have access to that<br>date_default_timezone_set('Etc\/UTC');<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Load dependencies from composer<br>\/\/If this causes an error, run 'composer install'<br>require 'vendor\/autoload.php';<br>require_once \"config.php\";<\/code><\/p>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Create a new PHPMailer instance<br>$mail = new PHPMailer();<\/code><\/p>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Tell PHPMailer to use SMTP<br>$mail-&gt;isSMTP();<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Enable SMTP debugging<br>\/\/SMTP::DEBUG_OFF = off (for production use)<br>\/\/SMTP::DEBUG_CLIENT = client messages<br>\/\/SMTP::DEBUG_SERVER = client and server messages<br>\/\/ TODO: turn off for production<br>$mail-&gt;SMTPDebug = SMTP::DEBUG_SERVER;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set the hostname of the mail server<br>$mail-&gt;Host = SMTP_SERVER;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set the SMTP port number:<br>\/\/ - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or<br>\/\/ - 587 for SMTP+STARTTLS<br>$mail-&gt;Port = SMTP_PORT;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set the encryption mechanism to use:<br>\/\/ - SMTPS (implicit TLS on port 465) or<br>\/\/ - STARTTLS (explicit TLS on port 587)<br>$mail-&gt;SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Whether to use SMTP authentication<br>$mail-&gt;SMTPAuth = true;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set AuthType to use XOAUTH2<br>$mail-&gt;AuthType = 'XOAUTH2';<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Start Option 1: Use league\/oauth2-client as OAuth2 token provider<br>\/\/Fill in authentication details here<br>\/\/Either the microsoft account owner, or the user that gave consent<br>$email = SMTP_USER;<br>$clientId = CLIENT_ID;<br>$clientSecret = CLIENT_SECRET;<br>$tenantId = TENANT_ID;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Obtained by configuring and running get_oauth_token.php<br>\/\/after setting up an app in Google Developer Console.<br>$refreshToken = REFRESH_TOKEN;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Create a new OAuth2 provider instance<br>$provider = new Azure(<br>[<br>'clientId' =&gt; $clientId,<br>'clientSecret' =&gt; $clientSecret,<br>'tenantId' =&gt; $tenantId,<br>]<br>);<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Pass the OAuth provider instance to PHPMailer<br>$mail-&gt;setOAuth(<br>new OAuth(<br>[<br>'provider' =&gt; $provider,<br>'clientId' =&gt; $clientId,<br>'clientSecret' =&gt; $clientSecret,<br>'refreshToken' =&gt; $refreshToken,<br>'userName' =&gt; $email,<br>]<br>)<br>);<br>\/\/End Option 1<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Option 2: Another OAuth library as OAuth2 token provider<br>\/\/Set up the other oauth library as per its documentation<br>\/\/Then create the wrapper class that implementations OAuthTokenProvider<br>\/\/$oauthTokenProvider = new MyOAuthTokenProvider(\/* Email, ClientId, ClientSecret, etc. *\/);<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Pass the implementation of OAuthTokenProvider to PHPMailer<br>\/\/$mail-&gt;setOAuth($oauthTokenProvider);<br>\/\/End Option 2<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set who the message is to be sent from<br>\/\/For Outlook, this generally needs to be the same as the user you logged in as<br>$mail-&gt;setFrom($email, 'EasyTec');<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set who the message is to be sent to<br>$mail-&gt;addAddress('oliver@slackertreff.de', 'Oliver Wagner');<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Set the subject line<br>$mail-&gt;Subject = 'PHPMailer Outlook XOAUTH2 SMTP test';<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Read an HTML message body from an external file, convert referenced images to embedded,<br>\/\/convert HTML into a basic plain-text alternative body<br>$mail-&gt;CharSet = PHPMailer::CHARSET_UTF8;<br>$mail-&gt;msgHTML(\"Geht es am n\u00e4chsten Tag auch noch?\");<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Replace the plain text body with one created manually<br>$mail-&gt;AltBody = 'This is a plain-text message body';<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/Attach an image file<br>\/\/$mail-&gt;addAttachment('images\/phpmailer_mini.png');<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>\/\/send the message, check for errors<br>if (!$mail-&gt;send()) {<br>echo 'Mailer Error: ' . $mail-&gt;ErrorInfo;<br>} else {<br>echo 'Message sent!';<br>}<\/code><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Funktionierender Code findet sich in der Trane Kalendersynchronisation und auf XAMPP in PHPMailer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Linksammlung<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/stackoverflow.com\/questions\/73868867\/how-to-implement-oauth-2-0-in-phpmailer-for-exchange-online\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/73868867\/how-to-implement-oauth-2-0-in-phpmailer-for-exchange-online<\/a><br>alt ohne OAUTH: <a href=\"https:\/\/stackoverflow.com\/questions\/24947434\/setting-up-phpmailer-with-office365-smtp\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/24947434\/setting-up-phpmailer-with-office365-smtp<\/a> &#8211; da kommt weiter unten eine Anleitung f\u00fcr die Einstellungen auf MS-Seite<br><a href=\"https:\/\/onelinerhub.com\/phpmailer\/how-do-i-use-phpmailer-with-oauth----\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/onelinerhub.com\/phpmailer\/how-do-i-use-phpmailer-with-oauth&#8212;-<\/a><br><a href=\"https:\/\/stackoverflow.com\/questions\/61895470\/smtp-to-o365-using-oauth2-and-phpmailer\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/61895470\/smtp-to-o365-using-oauth2-and-phpmailer<\/a><br><a href=\"https:\/\/vielhuber.de\/en\/blog\/access-with-php-to-exchange-office-365\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/vielhuber.de\/en\/blog\/access-with-php-to-exchange-office-365\/<\/a> &#8211; Anleitung f\u00fcr Einstellungen auf MS-Seite<br><a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\/discussions\/2747\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/PHPMailer\/PHPMailer\/discussions\/2747<\/a> &#8211; wegen Steven Maguire<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Refresh Token: <a href=\"https:\/\/www.devbabu.com\/how-to-use-phpmailer-with-xoauth2\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.devbabu.com\/how-to-use-phpmailer-with-xoauth2\/<\/a> Punkte 17 und 18<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Redirect URL: a URL the browser will be redirected to, once the user has granted permission<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note firstly that Steve Maguire\u2019s \u2018provider\u2019 Stevenmaguire\\OAuth2\\Client\\Provider\\Microsoft was apparently written for Windows Live Mail (see for example the WLM scopes in get_oauth_token.php) and has had little maintenance since then. Jan Hajek\u2019s thenetworg\/oauth2-azure is still under active development and works well with PHPMailer. We use it on all our websites. (<a href=\"https:\/\/github.com\/PHPMailer\/PHPMailer\/discussions\/2747\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/PHPMailer\/PHPMailer\/discussions\/2747<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Um eMails mit Hilfe von PHPMailer \u00fcber Office365 versenden zu k\u00f6nnen, sind einige Einstellungen vorzunehmen und es ist einiges zu beachten. AZURE ACTIVE DIRECTORY The following steps register a new app in Azure Active Directory: Was in dieser Anleitung fehlt, ist die Redirect URI. Diese muss zwingend eingetragen werden, sonst erfolgt eine entsprechende Fehlermeldung. Wo [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1494","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=\/wp\/v2\/pages\/1494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1494"}],"version-history":[{"count":0,"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=\/wp\/v2\/pages\/1494\/revisions"}],"wp:attachment":[{"href":"https:\/\/wiki.ad-libitum.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}