gmail_xoauth.phps 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * This example shows how to send via Google's Gmail servers using XOAUTH2 authentication.
  4. */
  5. //Import PHPMailer classes into the global namespace
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\OAuth;
  8. // Alias the League Google OAuth2 provider class
  9. use League\OAuth2\Client\Provider\Google;
  10. //SMTP needs accurate times, and the PHP time zone MUST be set
  11. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  12. date_default_timezone_set('Etc/UTC');
  13. //Load dependencies from composer
  14. //If this causes an error, run 'composer install'
  15. require '../vendor/autoload.php';
  16. //Create a new PHPMailer instance
  17. $mail = new PHPMailer;
  18. //Tell PHPMailer to use SMTP
  19. $mail->isSMTP();
  20. //Enable SMTP debugging
  21. // 0 = off (for production use)
  22. // 1 = client messages
  23. // 2 = client and server messages
  24. $mail->SMTPDebug = 2;
  25. //Set the hostname of the mail server
  26. $mail->Host = 'smtp.gmail.com';
  27. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  28. $mail->Port = 587;
  29. //Set the encryption system to use - ssl (deprecated) or tls
  30. $mail->SMTPSecure = 'tls';
  31. //Whether to use SMTP authentication
  32. $mail->SMTPAuth = true;
  33. //Set AuthType to use XOAUTH2
  34. $mail->AuthType = 'XOAUTH2';
  35. //Fill in authentication details here
  36. $email = 'someone@gmail.com';
  37. $clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com';
  38. $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP';
  39. //Obtained by configuring and running get_oauth_token.php
  40. //after setting up an app in Google Developer Console.
  41. $refreshToken = 'RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0';
  42. //Create a new OAuth2 provider instance
  43. $provider = new Google([
  44. 'clientId' => $clientId,
  45. 'clientSecret' => $clientSecret
  46. ]);
  47. //Pass the OAuth provider instance to PHPMailer
  48. $mail->setOAuth(
  49. new OAuth([
  50. 'provider' => $provider,
  51. 'clientId' => $clientId,
  52. 'clientSecret' => $clientSecret,
  53. 'refreshToken' => $refreshToken,
  54. 'userName' => $email
  55. ])
  56. );
  57. //Set who the message is to be sent from
  58. //For gmail, this generally needs to be the same as the user you logged in as
  59. $mail->setFrom($email, 'First Last');
  60. //Set who the message is to be sent to
  61. $mail->addAddress('someone@gmail.com', 'John Doe');
  62. //Set the subject line
  63. $mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test';
  64. //Read an HTML message body from an external file, convert referenced images to embedded,
  65. //convert HTML into a basic plain-text alternative body
  66. $mail->CharSet = 'utf-8';
  67. $mail->msgHTML(file_get_contents('contentsutf8.html'), dirname(__FILE__));
  68. //Replace the plain text body with one created manually
  69. $mail->AltBody = 'This is a plain-text message body';
  70. //Attach an image file
  71. $mail->addAttachment('images/phpmailer_mini.png');
  72. //send the message, check for errors
  73. if (!$mail->send()) {
  74. echo "Mailer Error: " . $mail->ErrorInfo;
  75. } else {
  76. echo "Message sent!";
  77. }