=============================================================================== Reference ------------------------------------------------------------------------------- https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail_xoauth.phps =============================================================================== Installing and testing phpmailer ------------------------------------------------------------------------------- 1. Create an empty folder. 2. Put "composer.phar" in the folder 3. Run the followings: php composer.phar require phpmailer/phpmailer php composer.phar require league/oauth2-client php composer.phar require league/oauth2-google 4. Download an example php for xoauth from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail_xoauth.phps and put it in the "examples" folder 5. Set up clientId, clientSecret, refreshToken in the php file =============================================================================== =============================================================================== How to obtain clientId, clientSecret, refreshToken ------------------------------------------------------------------------------- 1. https://console.cloud.google.com/apis/ 2. Enable "Gmail API" from Libaray 3. Create a project "PHPMailer" for phpkobo@gmailcom "GmXOA" for pg999333@gmailcom 4. Configre "OAuth Consent Screen" User Type: External ----------------------------------------- For phpkobo@gmailcom ----------------------------------------- AppName: RToken Email: phpkobo@gmail.com Developer contact information: phpkobo@gmail.com Scopes: No scopes necessary. Test User: phpkobo@gmail.com ----------------------------------------- For pg999333@gmailcom ----------------------------------------- AppName: RToken Email: pg999333@gmailcom Developer contact information: pg999333@gmailcom Scopes: No scopes necessary. Test User: pg999333@gmailcom 5. Create Credentials Select "OAuth Client ID" Application Type=web application ----------------------------------------- For phpkobo@gmailcom ----------------------------------------- Application Name: Get oauth token x Authorized Redirect URI:https://www.phpkobo.com/mod/tools/phpmailer/get_oauth_token.php Client ID: 828165177377-gmfu8kj0lkr3fvv4d59cungl6nqfbbrt.apps.googleusercontent.com 828165177377-gmfu8kj0lkr3fvv4d59cungl6nqfbbrt.apps.googleusercontent.com 828165177377-gmfu8kj0lkr3fvv4d59cungl6nqfbbrt.apps.googleusercontent.com Client Secret: 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 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 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 Refresh Token: 1//0fa8mcL5Eqt7oCgYIARAAGA8SNwF-L9IrPk4-INGsTf5h7wokZSX9y8GH_2WAEl0Qsq7RVBORbBLhAhRfwXirqBkVc4aMljnbHCQ 1//0fa8mcL5Eqt7oCgYIARAAGA8SNwF-L9IrPk4-INGsTf5h7wokZSX9y8GH_2WAEl0Qsq7RVBORbBLhAhRfwXirqBkVc4aMljnbHCQ 1//0fa8mcL5Eqt7oCgYIARAAGA8SNwF-L9IrPk4-INGsTf5h7wokZSX9y8GH_2WAEl0Qsq7RVBORbBLhAhRfwXirqBkVc4aMljnbHCQ ----------------------------------------- For pg999333@gmailcom ----------------------------------------- Application Name: Web client 1 Authorized Redirect URI:http://php80ub20.solar.com/phpkobo/research/php-downloaded-apps/phpmailer/get_oauth_token.php Client ID: 792305721379-ek00v4qk100i3c83bhi6a1ieapovehqb.apps.googleusercontent.com 792305721379-ek00v4qk100i3c83bhi6a1ieapovehqb.apps.googleusercontent.com 792305721379-ek00v4qk100i3c83bhi6a1ieapovehqb.apps.googleusercontent.com Client Secret: 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 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 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 Refresh Token: 1//06n110UZlSD1eCgYIARAAGAYSNwF-L9IrEpiVmLbUNo6YFpJOaD7DoE4vMd0Zj_upgbQ1nN1IeziTCaWQr3OU4JTWGP-hoteZC1M 1//06n110UZlSD1eCgYIARAAGAYSNwF-L9IrEpiVmLbUNo6YFpJOaD7DoE4vMd0Zj_upgbQ1nN1IeziTCaWQr3OU4JTWGP-hoteZC1M 1//06n110UZlSD1eCgYIARAAGAYSNwF-L9IrEpiVmLbUNo6YFpJOaD7DoE4vMd0Zj_upgbQ1nN1IeziTCaWQr3OU4JTWGP-hoteZC1M ----------------------------------------- 6. Set clientId and clientSecret in up get_oauth_token.php and execute it to obtain refreshToken. The URL of the php file must be registered in Client ID Settings Page. It doesn not accept anything other common extensions like "com", "org" If you are setting it up in a local vertual machine, create a fake .com domain name like "php80ub20@solar.com" for "php80ub20@solar.lan" =============================================================================== [Problem] If you use a copy of phpmailer you build in PHP8, and then copy it to PHP7.4, you'll get an error message like: ---------------------------------------------------------------- Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.2". ---------------------------------------------------------------- [Solution] Start with the following composer.json ---------------------------------------- { "config": { "platform": { "php": "7.3" } }, "require": { "phpmailer/phpmailer": "^6.6", "league/oauth2-client": "^2.6", "league/oauth2-google": "^4.0" } } ---------------------------------------- Then run: ---------------------------------------- php composer.phar update ---------------------------------------- ===============================================================================