douping5226
douping5226
2019-02-18 19:40

如何从gmail API发送电子邮件

  • php
  • api

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条回答