Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i read a gmail subject with php api?

in witch mode a can read the email subject with php api ?

i do this code

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);

// Print the labels in the user's account.
$user = 'me';


$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
  $optParamsGet['format'] = 'metadata'; // Display message in payload
  $message = $service->users_messages->get($user, $mail['id'],$optParamsGet);
  $objMessage = $message->getPayload();
  print_r($objMessage[18]['value']);
}

i find very difficult use this api with php because there isn't any documentation about the function. where i can find the comple documentation? (for example i found getPayload function because i see some example here)

thanks

like image 596
colapiombo Avatar asked Oct 17 '15 17:10

colapiombo


2 Answers

If you have a look at a bare bones response you get when accessing the API with regular http-requests, I think a lot of things will be more clear.

I will list my latest message, and then get the message, and have a look at the response:

Request

maxResults = 1

GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1

Response

{
 "messages": [
  {
   "id": "150791be31ee4e7b",
   "threadId": "150791be31ee4e7b"
  }
 ],
 "nextPageToken": "05858850367051193217",
 "resultSizeEstimate": 2
}

Request

GET https://www.googleapis.com/gmail/v1/users/me/messages/150791be31ee4e7b

Response

{
 "id": "150791be31ee4e7b",
 "threadId": "150791be31ee4e7b",
 "labelIds": [
  "INBOX",
  "CATEGORY_SOCIAL",
  "UNREAD"
 ],
 "snippet": "Infinite Elgintensity has uploaded Gym Idiots - The Smith Machine's Retarded Cousin I have no",
 "historyId": "623043",
 "internalDate": "1445140881000",
 "payload": {
  "mimeType": "multipart/alternative",
  "filename": "",
  "headers": [
   {
    "name": "Delivered-To",
    "value": "[email protected]"
   },
   {
    "name": "Received",
    "value": "by 10.28.188.5 with SMTP id m5csp628249wmf;        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.182.28.74 with SMTP id z10mr15179312obg.80.1445140882358;        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "Return-Path",
    "value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com>"
   },
   {
    "name": "Received",
    "value": "from mail-ob0-x245.google.com (mail-ob0-x245.google.com. [2607:f8b0:4003:c01::245])        by mx.google.com with ESMTPS id l8si12292524oej.1.2015.10.17.21.01.22        for <[email protected]>        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);        Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "Received-SPF",
    "value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) client-ip=2607:f8b0:4003:c01::245;"
   },
   {
    "name": "Authentication-Results",
    "value": "mx.google.com;       spf=pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 2607:f8b0:4003:c01::245 as permitted sender) smtp.mailfrom=3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com;       dkim=pass [email protected];       dmarc=pass (p=QUARANTINE dis=NONE) header.from=youtube.com"
   },
   {
    "name": "Received",
    "value": "by mail-ob0-x245.google.com with SMTP id da8so130023858obb.1        for <[email protected]>; Sat, 17 Oct 2015 21:01:22 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.107.148.204 with SMTP id w195mr21311032iod.15.1445140881929;        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "X-Received",
    "value": "by 10.140.238.83 with SMTP id j80mr20061780qhc.2.1445140881732;        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "Return-Path",
    "value": "<3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com>"
   },
   {
    "name": "Received",
    "value": "from mail-pa0-f72.google.com (mail-pa0-f72.google.com. [209.85.220.72])        by gmr-mx.google.com with ESMTPS id wl2si2608966pab.1.2015.10.17.21.01.21        for <[email protected]>        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);        Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "Received-SPF",
    "value": "pass (google.com: domain of 3kRkjVgcLDAgvwzmxt66w212jm.kwu1p2ttmB-898Fxiom0.xt20owwotm.kwu@youtube-subscriptions.bounces.google.com designates 209.85.220.72 as permitted sender) client-ip=209.85.220.72;"
   },
   {
    "name": "Received",
    "value": "by mail-pa0-f72.google.com with SMTP id da3so25pad.2        for <[email protected]>; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "DKIM-Signature",
    "value": "v=1; a=rsa-sha256; c=relaxed/relaxed;        d=youtube.com; s=20120806;        h=mime-version:message-id:date:subject:from:to:content-type;        bh=dxXkvZ79qLn59RVfXz4QAs5AW2+MyxrsrN55S+jKtK0=;        b=mrrriloHJkEy4TT3wpzhEO1nvinpewLWHHExGGprhWlUU+5ME9M8w3xlaTeDcm0V0y         QYDbH6TTG2rc0vwiLuZ46Dgm3rDC0brbg/Bdh3TQRUBneOTdAW4oMMs48DSIBbbFa58j         U7GHaaXGNtsY3ah4D2x8Bv9XxPbtN1YoMEV+DFlYAStrIyyUiGXk7yotcBWqbB89/MFt         ga9f+5Rhf38/TggWLRD88fBp8u2EIDB5VwqHsX5IstX2+Y0yb+yf4sk9iiToAnIXZhiC         Ngqp3YdgLrI6+UlAvixbdB42nlxsGbBJ9WoF4dejLEIeQps47loxNuA2hBsiO1wqSE6n         3XYA=="
   },
   {
    "name": "MIME-Version",
    "value": "1.0"
   },
   {
    "name": "X-Received",
    "value": "by 10.66.182.162 with SMTP id ef2mr20318738pac.35.1445140881616; Sat, 17 Oct 2015 21:01:21 -0700 (PDT)"
   },
   {
    "name": "X-No-Auto-Attachment",
    "value": "1"
   },
   {
    "name": "Message-ID",
    "value": "<[email protected]>"
   },
   {
    "name": "Date",
    "value": "Sun, 18 Oct 2015 04:01:21 +0000"
   },
   {
    "name": "Subject",
    "value": "Infinite Elgintensity just uploaded a video"
   },
   {
    "name": "From",
    "value": "YouTube <[email protected]>"
   },
   {
    "name": "To",
    "value": "[email protected]"
   },
   {
    "name": "Content-Type",
    "value": "multipart/alternative; boundary=047d7bd6aa64e62f000522591497"
   }
  ],
  "body": {
   "size": 0
  },
  "parts": [
   {
    "partId": "0",
    "mimeType": "text/plain",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/plain; charset=UTF-8; format=flowed; delsp=yes"
     }
    ],
    "body": {
     "size": 301,
     "data": "SW5maW5pdGUgRWx..."
    }
   },
   {
    "partId": "1",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "text/html; charset=UTF-8"
     },
     {
      "name": "Content-Transfer-Encoding",
      "value": "quoted-printable"
     }
    ],
    "body": {
     "size": 16884,
     "data": "DQo8aHRtbCBsYW5nPSJl..."
    }
   }
  ]
 },
 "sizeEstimate": 22314
}

This is how a typical message will look, and all that the various libraries does is to make it easier to work with. The headers will reside inside of the payload, and it's the Subject-header you are looking for.

There is no function for getting a specific header, but it's not that hard to implement:

function getHeader($headers, $name) {
  foreach($headers as $header) {
    if($header['name'] == $name) {
      return $header['value'];
    }
  }
}

$results = $service->users_messages->listUsersMessages($user);
foreach($results as $mail){
  $message = $service->users_messages->get($user, $mail['id']);
  $headers = $message->getPayload()->getHeaders();
  $subject = getHeader($headers, 'Subject');
}
like image 102
Tholle Avatar answered Sep 23 '22 17:09

Tholle


this works for me

 $message = $service->users_messages->get($userId, $messageId,['format' => 'metadata', 'metadataHeaders' => ['From']]);

 $headers = $message->getPayload()->getHeaders();

$from = $headers[0]['value']);

if($form == "My From")

....

I was working an mixed 2 or 3 examples and this is the result, in this case i need de Form metadata, change it for Subject and done.

like image 28
Lázaro Javier Pérez Moleiro Avatar answered Sep 20 '22 17:09

Lázaro Javier Pérez Moleiro