php - Bounce <nobody@gmail.com> Gmail API sending failure -
i'm trying send emails google api. i'm able read emails, authenticate oath using client_secret.json
file per quickstart instructions. i've got sending emails working, unable send successfully.
my email bouncing , i'm unable specify email i'm sending (hence nobody@gmail.com
address).
code here bellow works part. i've commented out parts work: (reading emails).
code:
<?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 authorized api client. * @return google_client authorized client object */ function getclient() { $client = new google_client(); $client->setapplicationname(application_name); $client->setscopes(scopes); $client->setauthconfigfile(client_secret_path); $client->setaccesstype('offline'); // load authorized credentials file. $credentialspath = expandhomedirectory(credentials_path); if (file_exists($credentialspath)) { $accesstoken = file_get_contents($credentialspath); } else { // request authorization user. $authurl = $client->createauthurl(); printf("open following link in browser:\n%s\n", $authurl); print 'enter verification code: '; $authcode = trim(fgets(stdin)); // exchange authorization code access token. $accesstoken = $client->authenticate($authcode); // store credentials disk. if(!file_exists(dirname($credentialspath))) { mkdir(dirname($credentialspath), 0700, true); } file_put_contents($credentialspath, $accesstoken); printf("credentials saved %s\n", $credentialspath); } $client->setaccesstoken($accesstoken); // refresh token if it's expired. if ($client->isaccesstokenexpired()) { $client->refreshtoken($client->getrefreshtoken()); file_put_contents($credentialspath, $client->getaccesstoken()); } return $client; } /** * expands home directory alias '~' full path. * @param string $path path expand. * @return string expanded path. */ function expandhomedirectory($path) { $homedirectory = getenv('home'); if (empty($homedirectory)) { $homedirectory = getenv("homedrive") . getenv("homepath"); } return str_replace('~', realpath($homedirectory), $path); } // api client , construct 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->setraw($mime); // print labels in user's account. $userid = 'me'; function sendmessage($service, $userid, $message) { try { $message = $service->users_messages->send($userid, $message); print 'message id: ' . $message->getid() . ' sent.'; // <----------- get's here , works return $message; } catch (exception $e) { print 'an error occurred: ' . $e->getmessage(); } } try { sendmessage($service, $userid, $msg); } catch (exception $ex ) { echo $ex->getmessage(); }
email response (in gmail inbox):
an error occurred. message not sent. test message or date: tue, 21 jul 2015 14:51:12 -0700 message-id: <
i'm not sure how specify destination address, personimsendingto@gmail.com
. couldn't find in way of documentation. if me out or point me in right direction, i'd appreciative.
you need put entire rfc822 email message in raw field. have "test message or something" should have string like:
to: someguy@gmail.com from: myaddress@gmail.com subject: cool subject here's text/plain email, neato?
remember have \r\n
between lines , 2 between headers , body. seems php pear::mail_mime library constructing such rfc822 mime email message strings.
for more details, see sending email.
Comments
Post a Comment