使用PHP在Gmail中插入Gmail API邮件

时间:2018-01-02 10:09:40

标签: php email gmail-api

我试图在Gmail中插入电子邮件使用google gmail API但不知何故邮件会被弹回。任何人都可以帮助我。这是我正在使用的代码。有什么我想念的吗

$client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setClientId('CLIENT_ID');
    $client->setClientSecret('CLIENT_SECRET_ID');
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');
    $client->setRedirectUri('http://localhost/demo/google/test/');



    $accessToken = "ACCESS_TOKEN";
    $client->refreshToken($accessToken );

    / set mesaage /
   // $raw_message = "To:test@gmail.com <test905@gmail.com>\r\n";
    $raw_message = "To:test@gmail.com <testfirstlastname009@gmail.com>\r\n";
    $raw_message .= "From:dev2test.@gmail.com <dev2test@gmail.com>\r\n";
    $raw_message .= 'Subject: =?utf-8?B?' .base64_encode('Repoting from the crm task') . "?=\r\n";

// Set the right MIME & Content type
    $raw_message .= "MIME-Version: 1.0\r\n";
    $raw_message .= "Content-Type: text/html; charset=iso-8859-1\r\n";
    //$raw_message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";

   /* $mime = rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
    $message = new Google_Service_Gmail_Message();
    $message->setRaw($mime);*/

    $template = "Hello this is <br> testing email for the localsystem sending by gmail or not";
    $raw_message .= $template;
    $mime = rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
    $message = new Google_Service_Gmail_Message();
    $message->setRaw($mime);
    /* end of set message*/

    $gmailService = new Google_Service_Gmail($client);

    try {
        $messages = $gmailService->users_messages->insert('me',$message);
        print 'Message with ID: ' . $messages->getId() . ' sent.';
        exit;     
    } catch (Exception $e) {
        print 'An error occurred: ' . $e->getMessage();
    }

0 个答案:

没有答案