Gmail oauth received on date using PHP - php

How do I go about getting a count of emails received on a particular date across all gmail folders. Managed to get email inbox counting working ok but now need to figure out how to get number received for previous day using PHP and oauth. Can anyone help?

The messages#list method allows you to pass a q parameter which accepts the same query format from the Gmail search field.
Thus it's possible to do this:
// $gmail is an instance of Google_Service_Gmail
$messages = $gmail->users_messages->listUsersMessages('me', array(
'q' => "after:2014/10/20 before:2014/10/21"
));
// $count is the number of messages from 2014/10/20
$count = $messages->getResultSizeEstimate();
Beware that the result will be different from what you see in Gmail if you have the 'Conversation view' turned on.

Related

GMail API - Getting certain message headers or fields

I have successfully connected GMail API via G Suite account and service account. I can get a message list and I can retrieve messages by IDs. I'm working with PHP.
What I'm having problems with is to get for example the FROM or TO headers, SUBJECT or the snippet field.
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from'], 'fields'=>['snippet','labelIds']);
$fullMessage = $service->users_messages->get($user, $id, $optParam);
This will return the snippet, but not the subject or from or the labelIds.
If I use the GMail "Try this API" and use the id of the message and use "snippet" in the "fields" entry, I just get the snippet back as:
{
"snippet": "Short snippet of the message"
}
If I use:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject','from','to']);
I do get the 3 headers, but I also get a lot more information, including the labels and snippet - about 3K for each message.
I just can't seem to be able to specify a small subset of the data. All I need is to show messages as a list with the subject, date/time, from/to.
I don't care so much about the amount of data, but it takes on average about 3.5 seconds to retrieve the data for just 14 message!
Is there a way to restrict this so I don't get all the "extra" data or speed the retrieval up somehow?
Sending the request would involve specifying the metadata keys as well as the parameter names of the fields you want to obtain. You can use an HTTP GET request with the URI to get the ‘to’, ‘from’, ‘subject’ and ‘snippet’ with https://www.googleapis.com/gmail/v1/users/me/messages/<MESSAGE_ID>?format=metadata&metadataHeaders=to&metadataHeaders=from&metadataHeaders=subject&fields=snippet%2C+payload%2Fheaders, which will also limit the headers you obtain.
In PHP you can use this:
$optParam = array('format' => 'metadata', 'metadataHeaders'=>['subject', 'from', 'to'], 'fields'=>'payload/headers,snippet');
Note that the fields parameter needs to be sent as a string and not an array.
Also be aware there is a known issue with the Gmail API where using the https://www.googleapis.com/auth/gmail.metadata scope will not return the snippet.
You’ll need to use https://www.googleapis.com/auth/gmail.readonly instead.
You can also make a batch of requests in one network call which will help speed up overall execution time as documented here.

How to flag only one message as "\flagged" with Horde Imap Client library

I'm trying to flag message with the imap flag "\flagged". The problem is that not only the message I intended to mark gets flagged, but also all other messages which were sent from the same email address are also flagged.
For example, in my mailbox I have 10 messages from example#host.com. I want to mark only one of them as "flagged", so I get it's uid and send a request. But it marks all 10 messages. I want only that one with the specific uid.
I'm working with gmail account. When flagging messages in gmail's webmail itself, it marks only one message as intended, so it's not a weird feature from gmail itself.
The code I'm using is below. What's the problem?
$uids = new Horde_Imap_Client_Ids([1521]);//uid of message I want to mark as flagged
$options = [
'uids' => $uids,
'add' => ['\flagged'],
];
$Imap_Client_Socket->store('INBOX', $options);
Change your options to this
$options = [
'ids' => $uids,
'add' => [Horde_Imap_Client::FLAG_FLAGGED],
];
Notice how uids changed to ids. Also, I used the flag constant defined in Horde_Imap_Client instead.

Gmail OAuth API Count Messages PHP

I have the following code to count unread emails with Gmail OAuth API in PHP. The problem I have it will only return a max 100 due to API rate limits. Is there anyway to count unread emails using oauth (not imap) and return correct value even if more than 100 items?
$optParams['labelIds'] = 'UNREAD';
$messages = $service->users_messages->listUsersMessages('me',$optParams);
$list = $messages->getMessages();
$result = count($list);
echo "Total Inbox = ".$result ;
There is an estimatedResultCount that's always returned on all list() calls but it's an estimate clearly.
If you want an exact count of total+unread messages on a label then you can now get that from label.get().
The way we were able to solve it is by creating a condition that if the number of unread emails exceeds 100 we simply put it in the following way "+100".

SendGrid Web API bounced email header response

I am sending emails via SendGrid with this inside the x-smtpapi header
$json_string = array(
'unique_args' => array (
'email_id' => 1
)
);
It all seems to send okay, inside SendGrid I can view the "email_id" in the Email activity under Unique Args.
However when I try to use the API to look at this email, I cannot find a way to actually get these unique arguments from the API.
I am using this to try and get the headers returned with the bounced emails.
$request = 'https://api.sendgrid.com/api/bounces.get.json&api_user=username&api_key=password'
All I get is just the email addresses that have bounced, not the header information (the unique arguments)
I want to know, is it possible to get the unique arguments from the API. I have read it multiple times to no avail.
I hope this makes sense.
Thanks
At present there's no way request to lookup specific events by unique_arg with the Web API.
However, the SendGrid Event Webhook will give you granular data on each event, such as a bounce as it happens. The Event Webhook POSTs data to your server every time an action is taken upon an email (e.g. open, click, bounce).
Once you receive it, you're responsible for storing this, although it's not a typical API it gives very specific data on events which you can then compile and rehash however you like.
To get started using the webhook, you'll do something like the following, and have SendGrid POST to the following script:
<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
// Here, you now have each event and can process them how you like
process_event($event);
}
[Taken from the SendGrid Webhook Code Example]

Snapchat API auth results in a ban

I am attempting to send snapchat's from the PHP API here.
As you can see it authenticates to the server a lot
<?php
// Log in:
$snapchat = new Snapchat('username', 'password');
// Upload a snap and send it to me for 8 seconds:
$id = $snapchat->upload(
Snapchat::MEDIA_IMAGE,
file_get_contents('/home/jorgen/whatever.jpg')
);
$snapchat->send($id, array('jorgenphi'), 8);
How is it possible to only authenticate once and then send the snapchat to an ARRAY of usernames?
It appears you can send to multiple people by adding more elements to the array in 2nd parameter of the send function (check here).
Try something like:
$snapchat->send($id, array('jorgenphi', 'anotheruser', 'thirduser'), 8);

Categories