0

i am trying to automate mail sending with the help of a google apps account

Credentials and application settings (security, scopes) seem to be ok (with the same service and the same credential, i can fetch my inbox messages without any problem.)

My apps has been authorized on the admin google console with the right creddential "a p12 key" and the scope "https://mail.google.com/"

so my php code look like

    $credentials = new \Google_Auth_AssertionCredentials(
        $serviceEmail,
        array('https://mail.google.com/'),
        file_get_contents($keyPath)
    );
    $client = new \Google_Client();

    $client->setAssertionCredentials($credentials);
    $gmailClient = new \Google_Service_Gmail($client);

    $mail = new \PHPMailer();
    $mail->CharSet = "UTF-8";
    $mail->Encoding = 'base64';
    $mail->AddAddress("my.mail@gmail.com");
    $mail->Subject = 'my subject';
    $mail->Body    = 'my body';
    $mail->preSend();
    $mime = $mail->getSentMIMEMessage();

    $m = new \Google_Service_Gmail_Message();
    $m->setRaw(\Google_Utils::urlSafeB64Encode($mime));

    try {
        $message = $gmailClient->users_messages->send('me', $m);

        return $message;
    } catch (\Exception $e) {
        return $e->getMessage();
    }

unfortunately it give me an unhelpfull message :(

{ "error": { "errors": [ { "domain": "global", "reason": "failedPrecondition", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }

what's the missing prencondition ? is there any way to know that with an advanced log on the admin pannel or something like

I also folowed this good link: Gmail REST API : 400 Bad Request + Failed Precondition

Thanks in advance

Community
  • 1
  • 1
ghaliano
  • 396
  • 6
  • 10
  • You also might wanna remove your mail id from the code – Alok Sep 07 '15 at 08:56
  • ok i also updated my answer, can someone give any clue – ghaliano Sep 09 '15 at 10:13
  • It could be related to assertionCredential. Check this other question: http://stackoverflow.com/questions/29307965/gmail-api-gives-failedprecondition-error – Gerardo Sep 16 '15 at 21:01
  • Did you get it to work? Have the same problem, but I definitely set `sub` like @Gerardo mentioned and granted domain-wide authority for https://mail.google.com as well.. Please help! – Dennis Zoma Jan 13 '16 at 00:52
  • @wottpal, Did you add your project's client ID in Admin Console? with the gmail scope? https://mail.google.com/ – Gerardo Jan 13 '16 at 01:00
  • Thanks for the fast reply. Yes I did add the Client-ID of my service-account there which is just a long number with digits only. Is this correct or should I use another ID or even the name of the service-account.. Which scopes should I add, currently I have `mail.google.com`, `gmail.send` and `gmail.compose`? – Dennis Zoma Jan 13 '16 at 01:04

0 Answers0