0
votes

I have a script that get incoming emails to my gmail but now i also want to get emails that i send via email.

Here is the code that i have to get incoming emails

Please let me know what i need for outgoing emails as well

At the moment this code runs via pub sub on console.google.com

 $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

1 Answers

0
votes

You'll probably want to use the SENT system label (instead of the Label_56 one) in your curl_init() call:

SENT

Applied automatically to messages that are sent with drafts.send or messages.send, inserted with messages.insert and the user's email in the From header, or sent by the user through the web interface.