如何获取从gmail API发送的电子邮件

时间:2019-02-18 19:40:37

标签: php api google-app-engine gmail-api

我有一个脚本,可以将收到的电子邮件发送到我的gmail,但现在我也想获取通过电子邮件发送的电子邮件。

这是我要收到传入电子邮件的代码

请让我知道我也需要外发电子邮件

此代码目前通过console.google.com上的pub sub运行

 $ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=Label_56&maxResults=10');

    curl_setopt_array($ch, array(
        CURLOPT_POST => false,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_HTTPHEADER => array(
            'Authorization: Bearer '. $tokenval,
             'Content-Type: application/json', 
        ),
    ));

    // execute request and get response
   $result = curl_exec($ch);


   $allmessages = json_decode($result);
   $allmessage = $allmessages->messages; 


   for($i=0;$i<count( $allmessage);$i++)
   {
       //echo $allmessage[$i]->id;
     $checkoldmsgid =  $this->Customer_m->getCount('messages',array('massageid'=>$allmessage[$i]->id ));

     if( $checkoldmsgid ==0)
     {

         $ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/messages/'.$allmessage[$i]->id);

                curl_setopt_array($ch, array(
                    CURLOPT_POST => false,
                    CURLOPT_RETURNTRANSFER => TRUE,
                    CURLOPT_HTTPHEADER => array(
                        'Authorization: Bearer '. $tokenval,
                         'Content-Type: application/json'
                    ),
                ));

             // execute request and get response
            $resultm = curl_exec($ch);
            $msgval = json_decode($resultm);





            $sendernum =explode('@',$msgval->payload->headers[19]->value);
            $recivernum =explode('@',$msgval->payload->headers[0]->value);
            $createdat =   date('Y-m-d H:i:s',strtotime($msgval->payload->headers[18]->value));


             $massage = $msgval->snippet;


              $single_message = $service->users_messages->get("me", $allmessage[$i]->id);
              $payload = $single_message->getPayload();


            $body = $payload->getBody();
            $FOUND_BODY = $this->decodeBody($body['data']);

1 个答案:

答案 0 :(得分:0)

您可能会在SENT通话中使用Label_56 system label(而不是curl_init()

  

已发送

     

自动应用于以drafts.send发送的邮件   或messages.send,并插入messages.insert和   用户在发件人标头中发送的电子邮件,或由用户通过   Web界面。