title: Bounce <nobody@gmail.com> Gmail API sending failure
body: <p>I'm trying to send emails with the google API. I'm able to read emails, authenticate with oath using a <code>client_secret.json</code> file per the <a href="https://developers.google.com/gmail/api/quickstart/php" rel="nofollow">quickstart</a> instructions. I've almost got sending emails working, but am unable to send successfully. </p>
  
  <p>My email is bouncing and I'm unable to specify the email I'm sending to (hence the <code>nobody@gmail.com</code> address). </p>
  
  <p>Code here bellow works for the most part. I've commented out the parts that work: (reading emails). </p>
  
  <p><strong>Code:</strong> </p>
  
  <pre><code>&lt;?php
  require 'google-api-php-client/src/Google/autoload.php';
  
  define('APPLICATION_NAME', 'Gmail API Quickstart');
  define('CREDENTIALS_PATH', '~/.credentials/gmail-api-quickstart.json');
  define('CLIENT_SECRET_PATH', 'client_secret.json');
  define('SCOPES', implode(' ', array(
    Google_Service_Gmail::MAIL_GOOGLE_COM,
    Google_Service_Gmail::GMAIL_COMPOSE)
  ));
  
  /**
   * Returns an authorized API client.
   * @return Google_Client the authorized client object
   */
  function getClient() {
    $client = new Google_Client();
    $client-&gt;setApplicationName(APPLICATION_NAME);
    $client-&gt;setScopes(SCOPES);
    $client-&gt;setAuthConfigFile(CLIENT_SECRET_PATH);
    $client-&gt;setAccessType('offline');
  
    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
      $accessToken = file_get_contents($credentialsPath);
    } else {
      // Request authorization from the user.
      $authUrl = $client-&gt;createAuthUrl();
      printf("Open the following link in your browser:\n%s\n", $authUrl);
      print 'Enter verification code: ';
      $authCode = trim(fgets(STDIN));
  
      // Exchange authorization code for an access token.
      $accessToken = $client-&gt;authenticate($authCode);
  
      // Store the credentials to disk.
      if(!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
      }
      file_put_contents($credentialsPath, $accessToken);
      printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client-&gt;setAccessToken($accessToken);
  
    // Refresh the token if it's expired.
    if ($client-&gt;isAccessTokenExpired()) {
      $client-&gt;refreshToken($client-&gt;getRefreshToken());
      file_put_contents($credentialsPath, $client-&gt;getAccessToken());
    }
    return $client;
  }
  
  /**
   * Expands the home directory alias '~' to the full path.
   * @param string $path the path to expand.
   * @return string the expanded path.
   */
  function expandHomeDirectory($path) {
    $homeDirectory = getenv('HOME');
    if (empty($homeDirectory)) {
      $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
    }
    return str_replace('~', realpath($homeDirectory), $path);
  }
  
  // Get the API client and construct the service object.
  $client = getClient();
  $service = new Google_Service_Gmail($client);
  
  
  $msg = new Google_Service_Gmail_Message(); 
  $mime = rtrim(strtr(base64_encode("TEST MESSAGE OR SOMETHING"), '+/', '-_'), '=');
  $msg-&gt;setRaw($mime); 
  
  // Print the labels in the user's account.
  $userId = 'me';
  
  function sendMessage($service, $userId, $message) {
    try {
      $message = $service-&gt;users_messages-&gt;send($userId, $message);
      print 'Message with ID: ' . $message-&gt;getId() . ' sent.';
      // &lt;----------- GET'S HERE AND THIS WORKS
      return $message;
    } catch (Exception $e) {
      print 'An error occurred: ' . $e-&gt;getMessage();
    }
  }
  
  try {
      sendMessage($service, $userId, $msg); 
  } catch (Exception $Ex ) {
      echo $Ex-&gt;getMessage(); 
  }
  </code></pre>
  
  <p><strong>Email Response</strong> (In my Gmail inbox)<strong>:</strong> </p>
  
  <pre><code>An error occurred. Your message was not sent.
  
  TEST MESSAGE OR SOMETHING Date: Tue, 21 Jul 2015 14:51:12 -0700 Message-Id: &lt;
  </code></pre>
  
  <p>I'm not sure how to even specify the destination address, like <code>personimsendingto@gmail.com</code>. I couldn't really find much in the way of documentation. If someone could help me out or point me in the right direction, I'd be greatly appreciative. </p>