|
| 1 | +<?php |
| 2 | +require '../src/Curl/Curl.php'; |
| 3 | + |
| 4 | +use \Curl\Curl; |
| 5 | + |
| 6 | +define('CLIENT_ID', 'XXXXXXXXXXXX.apps.googleusercontent.com'); |
| 7 | +define('CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXX'); |
| 8 | + |
| 9 | +session_start(); |
| 10 | + |
| 11 | +if (isset($_GET['code'])) { |
| 12 | + $code = $_GET['code']; |
| 13 | + |
| 14 | + // Exchange the authorization code for an access token. |
| 15 | + $curl = new Curl(); |
| 16 | + $curl->post('https://accounts.google.com/o/oauth2/token', array( |
| 17 | + 'code' => $code, |
| 18 | + 'client_id' => CLIENT_ID, |
| 19 | + 'client_secret' => CLIENT_SECRET, |
| 20 | + 'redirect_uri' => implode('', array( |
| 21 | + isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', |
| 22 | + '://', |
| 23 | + $_SERVER['SERVER_NAME'], |
| 24 | + $_SERVER['SCRIPT_NAME'], |
| 25 | + )), |
| 26 | + 'grant_type' => 'authorization_code', |
| 27 | + )); |
| 28 | + |
| 29 | + if ($curl->error) { |
| 30 | + echo $curl->response->error . ': ' . $curl->response->error_description; |
| 31 | + exit; |
| 32 | + } |
| 33 | + |
| 34 | + $_SESSION['access_token'] = $curl->response->access_token; |
| 35 | + header('Location: ?'); |
| 36 | +} elseif (!empty($_SESSION['access_token'])) { |
| 37 | + // Use the access token to send an email. |
| 38 | + $curl = new Curl(); |
| 39 | + $curl->setHeader('Content-Type', 'message/rfc822'); |
| 40 | + $curl->setHeader('Authorization', 'OAuth ' . $_SESSION['access_token']); |
| 41 | + |
| 42 | + $boundary = md5(time()); |
| 43 | + $raw = |
| 44 | + 'MIME-Version: 1.0' . "\r\n" . |
| 45 | + 'Subject: hi' . "\r\n" . |
| 46 | + 'To: John Doe <jdoe@example.com>' . "\r\n" . |
| 47 | + 'Content-Type: multipart/alternative; boundary=' . $boundary . "\r\n" . |
| 48 | + "\r\n" . |
| 49 | + '--' . $boundary . "\r\n" . |
| 50 | + 'Content-Type: text/plain; charset=UTF-8' . "\r\n" . |
| 51 | + "\r\n" . |
| 52 | + 'hello, world' . "\r\n" . |
| 53 | + "\r\n" . |
| 54 | + '--' . $boundary . "\r\n" . |
| 55 | + 'Content-Type: text/html; charset=UTF-8' . "\r\n" . |
| 56 | + "\r\n" . |
| 57 | + '<em>hello, world</em>' . "\r\n" . |
| 58 | + ''; |
| 59 | + |
| 60 | + $curl->post('https://www.googleapis.com/upload/gmail/v1/users/me/messages/send', $raw); |
| 61 | + |
| 62 | + echo 'Email ' . $curl->response->id . ' was sent.'; |
| 63 | +} else { |
| 64 | + $curl = new Curl(); |
| 65 | + $curl->get('https://accounts.google.com/o/oauth2/auth', array( |
| 66 | + 'scope' => 'https://www.googleapis.com/auth/gmail.compose', |
| 67 | + 'redirect_uri' => implode('', array( |
| 68 | + isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http', |
| 69 | + '://', |
| 70 | + $_SERVER['SERVER_NAME'], |
| 71 | + $_SERVER['SCRIPT_NAME'], |
| 72 | + )), |
| 73 | + 'response_type' => 'code', |
| 74 | + 'client_id' => CLIENT_ID, |
| 75 | + 'approval_prompt' => 'force', |
| 76 | + )); |
| 77 | + |
| 78 | + $url = $curl->response_headers['Location']; |
| 79 | + echo '<a href="' . $url . '">Continue</a>'; |
| 80 | +} |
0 commit comments