Gmail api Sending email 400 bad request

2019-09-18 01:37发布

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

0条回答
登录 后发表回答