2016-08-10 1 views
1

예정된 시간에 초안 이메일 (Gmail)을 보내야합니다. 아래 코드를 시도했습니다. Gmail API PHP 클라이언트 라이브러리 또는 API - Gmail 초안을 어떻게 보내나요?

$access_token = 'ya29.DxswidlwadllsidVeg5B67CdpLN6QR0d1nCuQmg9GaMT9iq8-zIA8O29yR9rEMM'; 
    //the access token is subject to change every hour 
    $header = array('Content-Type: message/rfc822', "Authorization: Bearer $access_token"); 
    $to = '125699645855239833'; // this is a draft id. 
    $line = "\r\n"; 
    $raw=''; 
    $url = 'https://www.googleapis.com/upload/gmail/v1/users/me/drafts?fields=id&key=XsqwwosqldwwdssaOLwotWD6seRloXZM'; 
    $raw .= "id: $to ".$line.$line; 
    $method_type = 1; // set as post method 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 

    if($header !== 0){ 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
    } 

    if($method_type == 1 or $method_type == 0){ 
    curl_setopt($curl, CURLOPT_POST, $method_type); 
    }else{ 
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    } 

    if($data !== 0){ 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $raw); 
    } 

    $response = curl_exec($curl); 
    $json = json_decode($response, true); 
    print_r($json); 
    curl_close($curl); 

내가 설정되지 않은

배열 ([ID] => R-5083530165761240787)하지만 초안 (이메일) 아래로 컬 게시 한 후 결과를 얻었다. 변경 (1)을 다음과 같은

답변

1

당신은 두 가지 사항을 변경해야합니다 라인 3 번에, 당신은

Content-Type: message/rfc822 to Content-Type: application/json 

변경 (2)를 교체해야합니다 라인 no.38에, 당신은

$raw .= "id: $to ".$line.$line; to $raw = '{ "id": "'.$t.'" }'.$line.$line; 

설명 교체 필요 : 데이터는 콘텐츠 형식이 json 인 원시 형식으로 전달되어야합니다.

초안을 보낼 때 메시지를있는 그대로 또는 업데이트 된 메시지와 함께 보내도록 선택할 수 있습니다. 초안 내용을 새 메시지로 업데이트하는 경우 drafts.send 요청의 본문에 초안 자원을 제공하십시오. 보내질 초안의 draft.id를 설정하십시오. draft.message.raw 필드를 base64url 인코딩 된 문자열로 인코딩 된 새 MIME 메시지로 설정합니다. 자세한 내용은 drafts.send를 참조하십시오.

참조 : https://developers.google.com/gmail/api/guides/drafts

+0

나를 위해 작동합니다. :) –