class Gmail extends ServiceWrapper

Gmail helper

Provides helper functions to Gmail API

Constants

GMAIL_INBOX

GMAIL_DRAFT

GMAIL_UNREAD

GMAIL_IMPORTANT

GMAIL_STARRED

GMAIL_SPAM

GMAIL_TRASH

GMAIL_SENT

Properties

protected Google_Service $_service from ServiceWrapper

Methods

mixed
__call(string $name, mixed[] $arguments)

Magic method to transfer method calls to underlying Google service (except for method defined here)

mixed
__get(string $k)

Magic method to get a property from underlying Google service (except for properties defined here)

__construct(Google_Service $service)

Constructor of service helper

Google_Service_Gmail_Message[]
listAllUsersMessages(string $userid, string[] $optparams = array())

Get a list of messages IDs, with optional parameters (query)

string
getMessageAttachmentDecoded(string $userid, string $mid, string $aid, string[] $optparams = array())

Get a message attachment and decode it (as Gmail encodes it with base64)

string
getMessageAttachment(string $userid, string $mid, string $aid, string[] $optparams = array())

Get a message attachment (encoded by Gmail with base64)

string
getLabel($userid, $label)

Search a label and get its ID.

string
base64RFC4648Decode(string $data)

Helper method to decode quoted printable rfc4648.

string|string[]|false
getMessageHeader(Google_Service_Gmail_Message $email, string $header)

Look for a particular header in the message headers list

string|string[]|false
getMessagePartHeader(Google_Service_Gmail_MessagePart $part, string $header)

Look for a particular header in a message part headers list

string|string[]|false
getHeader(array $headers, string $header)

Look for a particular header in a headers list

Google_Service_Gmail_MessagePart
getMessagePart(Google_Service_Gmail_Message $email, string $searchFor)

Get a specific message part, identified by its Mime-type

getMessageBody(Google_Service_Gmail_Message $email, string[] $contentTypes = array('text/html', 'text/plain'))

Get a message body (usaually text/html or text/plain) and decode it (as Google always encodes it to base64)

string
getMessageSubject(Google_Service_Gmail_Message $email)

Get message subject ; shortcut to Gmail::getMessageHeader($email, 'Subject')

int|string|null
getMessageDate(Google_Service_Gmail_Message $email)

Get message send date/time

getMessageAttachments(Google_Service_Gmail_Message $email)

Get a list of attachments for a message

getMessageInlineAttachments(Google_Service_Gmail_Message $email)

Get a list of inline attachments (usually embedded images such as logos or signatures) for a message

getMessageBodyWithInlineAttachments(Google_Service_Gmail_Message $email, string $userid, string[] $contentTypes = array('text/html', 'text/plain'))

Get a message body with its inline attachments converted to inline images (HTML tags with IMG SRC attribute set to data:image/jpeg;base64,.

Details

in ServiceWrapper at line 32
mixed __call(string $name, mixed[] $arguments)

Magic method to transfer method calls to underlying Google service (except for method defined here)

Parameters

string $name Method name
mixed[] $arguments Array of parameters (indexed array, not associative)

Return Value

mixed

in ServiceWrapper at line 47
mixed __get(string $k)

Magic method to get a property from underlying Google service (except for properties defined here)

Parameters

string $k Property name

Return Value

mixed

in ServiceWrapper at line 61
__construct(Google_Service $service)

Constructor of service helper

Parameters

Google_Service $service Google service to send requests to

at line 47
Google_Service_Gmail_Message[] listAllUsersMessages(string $userid, string[] $optparams = array())

Get a list of messages IDs, with optional parameters (query)

Don't make a mistake by thinking 'listAllUsersMessages' means listing all email with no filtering options. 'All' means we want to fetch the entire list in one call, and not bother with page tokens. To select emails to include, use any filter available through the $optparams parameter (see API reference for a list of filters and syntax).

Moreover, this method only returns messages IDs, not actual content. You have to fetch each the email content by calling $service->users_messages->get($userid, $messageid).

Parameters

string $userid User ID concerned (email) or 'me' to indicate the authenticated user
string[] $optparams Array of parameters for request, as defined in the API protocol reference

Return Value

Google_Service_Gmail_Message[] An array of emails entries with messages IDs only

at line 88
string getMessageAttachmentDecoded(string $userid, string $mid, string $aid, string[] $optparams = array())

Get a message attachment and decode it (as Gmail encodes it with base64)

Parameters

string $userid User ID concerned (email) or 'me' to indicate the authenticated user
string $mid Message ID
string $aid Attachment ID
string[] $optparams Array of parameters for request, as defined in the API protocol reference

Return Value

string The decoded attachment as a string

at line 103
string getMessageAttachment(string $userid, string $mid, string $aid, string[] $optparams = array())

Get a message attachment (encoded by Gmail with base64)

Parameters

string $userid User ID concerned (email) or 'me' to indicate the authenticated user
string $mid Message ID
string $aid Attachment ID
string[] $optparams Array of parameters for request, as defined in the API protocol reference

Return Value

string The attachment as a base64-encoded string

at line 119
string getLabel($userid, $label)

Search a label and get its ID.

No cache mechanism is used, please implement something more efficient in your own code if many calls are to be done.

Parameters

$userid
$label

Return Value

string Return label ID with $label as title

Exceptions

Exception If no label found with $label as title, an exception is thrown

at line 144
string base64RFC4648Decode(string $data)

Helper method to decode quoted printable rfc4648.

Before decoding from base64 we have to replace _ by / and - by +, as Google API does those suspicious replacements after encoding to base64

Parameters

string $data Base64 encoded string

Return Value

string Decoded string

at line 157
string|string[]|false getMessageHeader(Google_Service_Gmail_Message $email, string $header)

Look for a particular header in the message headers list

Parameters

Google_Service_Gmail_Message $email Message object
string $header Header name to look for

Return Value

string|string[]|false Return the header value or an array of header values if multiple headers with name $header are found ; if not found, returns FALSE

at line 170
string|string[]|false getMessagePartHeader(Google_Service_Gmail_MessagePart $part, string $header)

Look for a particular header in a message part headers list

Parameters

Google_Service_Gmail_MessagePart $part Message part object
string $header Header name to look for

Return Value

string|string[]|false Return the header value or an array of header values if multiple headers with name $header are found ; if not found, returns FALSE

at line 184
string|string[]|false getHeader(array $headers, string $header)

Look for a particular header in a headers list

Parameters

array $headers List of headers (as array of objects with name & value properties)
string $header Header name to look for

Return Value

string|string[]|false Return the header value or an array of header values if multiple headers with name $header are found ; if not found, returns FALSE

Exceptions

Exception Thrown if $headers is not an array of Google_Service_Gmail_MessagePartHeader objects

at line 214
Google_Service_Gmail_MessagePart getMessagePart(Google_Service_Gmail_Message $email, string $searchFor)

Get a specific message part, identified by its Mime-type

The message part is returned as provided by the API, that is to say it is still base64 encoded. To get a specific message part and decode it, call getMessageBody() method instead with the appropriate suitable content-types.

Parameters

Google_Service_Gmail_Message $email Message object
string $searchFor Part Content-type to look for

Return Value

Google_Service_Gmail_MessagePart Returns the part object if found, FALSE otherwise

at line 247
MessageBody|null getMessageBody(Google_Service_Gmail_Message $email, string[] $contentTypes = array('text/html', 'text/plain'))

Get a message body (usaually text/html or text/plain) and decode it (as Google always encodes it to base64)

Parameters

Google_Service_Gmail_Message $email Message object
string[] $contentTypes Array of suitable content-types. The first content-type found in the message is used. So you may set it to ['text/html', 'text/plain'] and not ['text/plain', 'text/html'].

Return Value

MessageBody|null Returns the part content as a Gmail\MessageBody object ; returns NULL if no suitable part is found

at line 270
string getMessageSubject(Google_Service_Gmail_Message $email)

Get message subject ; shortcut to Gmail::getMessageHeader($email, 'Subject')

Parameters

Google_Service_Gmail_Message $email

Return Value

string The email subject as a string

at line 282
int|string|null getMessageDate(Google_Service_Gmail_Message $email)

Get message send date/time

Parameters

Google_Service_Gmail_Message $email

Return Value

int|string|null Returns a UNIX timestamp with local timezone ; returns a string with Date header if the date format cannot be parsed ; returns NULL if no Date header

at line 318
MessageAttachment[]|null getMessageAttachments(Google_Service_Gmail_Message $email)

Get a list of attachments for a message

Parameters

Google_Service_Gmail_Message $email

Return Value

MessageAttachment[]|null Returns an array of attachements objects or NULL if email has no attachments

at line 360
MessageAttachment[]|null getMessageInlineAttachments(Google_Service_Gmail_Message $email)

Get a list of inline attachments (usually embedded images such as logos or signatures) for a message

Parameters

Google_Service_Gmail_Message $email

Return Value

MessageAttachment[]|null Returns an array of inline attachements objects or NULL if email has no attachments

at line 403
MessageBody getMessageBodyWithInlineAttachments(Google_Service_Gmail_Message $email, string $userid, string[] $contentTypes = array('text/html', 'text/plain'))

Get a message body with its inline attachments converted to inline images (HTML tags with IMG SRC attribute set to data:image/jpeg;base64,.

...)

Parameters

Google_Service_Gmail_Message $email Message object
string $userid User id or special value 'me' (required to fetch attachments : we have to send a request to the API)
string[] $contentTypes Array of suitable content-types. The first content-type found in the message is used. So you may set it to ['text/html', 'text/plain'] and not ['text/plain', 'text/html'].

Return Value

MessageBody Returns the body content with inline embeddings converted to IMG tags with base64 content in their SRC attribute