gmail_xoauth.phps 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * This example shows settings to use when sending via Google's Gmail servers.
  4. */
  5. //SMTP needs accurate times, and the PHP time zone MUST be set
  6. //This should be done in your php.ini, but this is how to do it if you don't have access to that
  7. date_default_timezone_set('Etc/UTC');
  8. require '../PHPMailerAutoload.php';
  9. //Load dependencies from composer
  10. //If this causes an error, run 'composer install'
  11. require '../vendor/autoload.php';
  12. //Create a new PHPMailer instance
  13. $mail = new PHPMailerOAuth;
  14. //Tell PHPMailer to use SMTP
  15. $mail->isSMTP();
  16. //Enable SMTP debugging
  17. // 0 = off (for production use)
  18. // 1 = client messages
  19. // 2 = client and server messages
  20. $mail->SMTPDebug = 0;
  21. //Ask for HTML-friendly debug output
  22. $mail->Debugoutput = 'html';
  23. //Set the hostname of the mail server
  24. $mail->Host = 'smtp.gmail.com';
  25. //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  26. $mail->Port = 587;
  27. //Set the encryption system to use - ssl (deprecated) or tls
  28. $mail->SMTPSecure = 'tls';
  29. //Whether to use SMTP authentication
  30. $mail->SMTPAuth = true;
  31. //Set AuthType
  32. $mail->AuthType = 'XOAUTH2';
  33. //User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
  34. $mail->oauthUserEmail = "someone@gmail.com";
  35. //Obtained From Google Developer Console
  36. $mail->oauthClientId = "RANDOMCHARS-----duv1n2.apps.googleusercontent.com";
  37. //Obtained From Google Developer Console
  38. $mail->oauthClientSecret = "RANDOMCHARS-----lGyjPcRtvP";
  39. //Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
  40. //Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
  41. // eg: http://localhost/phpmail/get_oauth_token.php
  42. $mail->oauthRefreshToken = "RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0";
  43. //Set who the message is to be sent from
  44. //For gmail, this generally needs to be the same as the user you logged in as
  45. $mail->setFrom('from@example.com', 'First Last');
  46. //Set who the message is to be sent to
  47. $mail->addAddress('whoto@example.com', 'John Doe');
  48. //Set the subject line
  49. $mail->Subject = 'PHPMailer GMail SMTP test';
  50. //Read an HTML message body from an external file, convert referenced images to embedded,
  51. //convert HTML into a basic plain-text alternative body
  52. $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
  53. //Replace the plain text body with one created manually
  54. $mail->AltBody = 'This is a plain-text message body';
  55. //Attach an image file
  56. $mail->addAttachment('images/phpmailer_mini.png');
  57. //send the message, check for errors
  58. if (!$mail->send()) {
  59. echo "Mailer Error: " . $mail->ErrorInfo;
  60. } else {
  61. echo "Message sent!";
  62. }