Álvaro N. Franz
Álvaro N. Franz

Reputation: 1228

PHP Gmail API - Send draft

How may I send a draft with GMAIL API and OAuth2.0 via PHP?

In the official docs there is no reference on how to achieve this with PHP.

Based on the Java example, I tried:

$drafts = array();

try {
    $draftsResponse = $service->users_drafts->listUsersDrafts('me');
    if ($draftsResponse->getDrafts()) {
        $drafts = array_merge($drafts, $draftsResponse->getDrafts());
    }
} 

catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}

var_dump($drafts);

foreach ($drafts as $draft) {
    echo 'Draft with ID: ' . $draft->getId() . '<br/>';
    $abc = $service->users_drafts->send('me',$draft->getId());
    var_dump($abc);
}

But of course I am doing something wrong, because it is not working. The first var_dump() returns all drafts. But nothing else happens after that.

Can you please help me?

Upvotes: 3

Views: 377

Answers (1)

Tholle
Tholle

Reputation: 112787

You have to create a new Google_Service_Gmail_Draft instance and use that, not just supply the id:

foreach ($drafts as $draft) {
    $d = new Google_Service_Gmail_Draft();
    $d->setId($draft->getId());
    $service->users_drafts->send('me', $d);
}

Upvotes: 1

Related Questions