Class: GMailer
Source Location: /libgmailer.php
Class GMailer
Class Overview
|
Class GMailer is the main class/library for interacting with Gmail (Google's free webmail service) with ease.
Acknowledgement It is not completely built from scratch. It is based on: "Gmail RSS feed in PHP" by thimal, "Gmail as an online backup system" by Ilia Alshanetsky, and "Gmail Agent API" by Johnvey Hwang and Eric Larson. Special thanks to Eric Larson and all other users, testers, and forum posters for their bug reports, comments and advices.
Located in /libgmailer.php [line 156]
Author(s):
Information Tags:
|
Properties
|
Methods
|
Method Summary
| bool |
connect() |
Connect to GMail with default session management settings. |
| string |
dump() |
Dump everything to output. |
| bool |
editLabel() |
Create, edit or remove label. |
| bool |
fetch() |
Fetch contents by URL query. |
| bool |
fetchBox() |
Fetch contents from Gmail by type. |
| bool |
getAttachment() |
Save attachment with attachment ID $attid and message ID $msgid to file with name $filename. |
| string[] |
getAttachmentsOf() |
Save all attaching files of conversations to a path. |
| bool |
invite() |
Send Gmail invite to $email |
| bool |
send() |
Send Gmail. Or save a draft email. |
Properties
Status of GMailer
If something is wrong, check this class property to see what is going wrong. API Tags:
Information Tags:
| Since: | 8 July 2005 |
| Author: | Neerav |
Methods
Constructor of GMailer
During the creation of GMailer object, it will perform several tests to see if the cURL extension is available or not. However, note that the constructor will NOT return false or null even if these tests are failed. You will have to check the class property GMailer::$created to see if the object "created" is really, uh, created (i.e. working), and property GMailer::$return_status or method GMailer::lastActionStatus() to see what was going wrong. Example: - <?php
- $gmailer = new GMailer();
- if (!$gmailer->created) {
- echo "Error message: ".$gmailer->lastActionStatus("message");
- } else {
- // Do something with $gmailer
- }
- ?>
A typical usage og GMailer object would be like this: - <?php
- require_once("libgmailer.php");
-
- $gmailer = new GMailer();
- if ($gmailer->created) {
- $gmailer->setLoginInfo($gmail_acc, $gmail_pwd, $my_timezone);
- $gmailer->setProxy("proxy.company.com");
- if ($gmailer->connect()) {
- // GMailer connected to Gmail successfully.
- // Do something with it.
-
- } else {
- die("Fail to connect because: ".$gmailer->lastActionStatus());
- }
- } else {
- die("Failed to create GMailer because: ".$gmailer->lastActionStatus());
- }
- ?>
API Tags:
bool addSenderToContact(
string
$message_id
)
|
|
Add message's senders to contact list.
Parameters:
|
string |
$message_id: |
Message ID |
Information Tags:
| Since: | 14 Aug 2005 |
| Author: | Neerav |
Connect to GMail with default session management settings.
API Tags:
| Return: | Connect to Gmail successfully or not |
Connect to Gmail without setting any session/cookie
API Tags:
| Return: | Connect to Gmail successfully or not |
bool deleteContact(
string[]
$id
)
|
|
Delete contacts.
Parameters:
|
string[] |
$id: |
Contact ID to be deleted |
API Tags:
Information Tags:
| Since: | 15 Jun 2005 |
| Author: | Neerav |
bool deleteFilter(
string
$id
)
|
|
Delete a filter.
Parameters:
|
string |
$id: |
Filter ID to be deleted |
API Tags:
Information Tags:
| Since: | 25 Jun 2005 |
| Author: | Neerav |
string dump(
string
$query
)
|
|
Dump everything to output.
This is a "low-level" method. Use the method GMailer::fetchBox() to fetch standard contents from Gmail.
Parameters:
|
string |
$query: |
URL query string. |
API Tags:
| Return: | Everything received from Gmail. |
bool editContact(
string
$contact_id, string
$name, string
$email, string
$notes, [mixed[][]
$details = array()]
)
|
|
Create/edit contact.
Examples: - <?php
- // Add a new one
- $gmailer->editContact(-1,
- "John",
- "john@company.com",
- "Supervisor of project X",
- "");
-
- // Add a new one with lots of details
- $gmailer->editContact(-1, "Mike", "mike@company.com",
- "Mike the driver",
- array(array("phone" => "123-45678",
- "mobile" => "987-65432",
- "fax" => "111-11111",
- "pager" => "222-22222",
- "im" => "34343434",
- "company" => "22th Century Fox",
- "position" => "CEO",
- "other" => "Great football player!",
- "address" => "1 Fox Rd",
- "detail_name" => "Mike G. Stone"));
-
- // Modified an existing one
- $gmailer->editContact($contact_id,
- "Old Name",
- "new_mail@company.com",
- "Old notes");
- ?>
Note: You must supply the old name even if you are not going to modify it, or it will be changed to empty!
Parameters:
|
string |
$contact_id: |
Contact ID for editing an existing one, or -1 for creating a new one |
|
string |
$name: |
Name |
|
string |
$email: |
Email address |
|
string |
$notes: |
Notes |
|
mixed[][] |
$details: |
Detailed information |
API Tags:
Information Tags:
| Since: | 15 Jun 2005 |
| Author: | Neerav |
bool editFilter(
integer
$filter_id, string
$from, string
$to, string
$subject, string
$has, string
$hasnot, bool
$hasAttach, bool
$archive, bool
$star, bool
$label, string
$label_name, bool
$forward, string
$forwardto, bool
$trash
)
|
|
Create/edit a filter.
Parameters:
|
integer |
$filter_id: |
Filter ID to be edited, or "0" for creating a new one |
|
string |
$from: |
|
|
string |
$to: |
|
|
string |
$subject: |
|
|
string |
$has: |
|
|
string |
$hasnot: |
|
|
bool |
$hasAttach: |
|
|
bool |
$archive: |
|
|
bool |
$star: |
|
|
bool |
$label: |
|
|
string |
$label_name: |
|
|
bool |
$forward: |
|
|
string |
$forwardto: |
|
|
bool |
$trash: |
|
API Tags:
Information Tags:
| Since: | 25 Jun 2005 |
| Author: | Neerav |
bool editLabel(
string
$label, string
$action, string
$renamelabel
)
|
|
Create, edit or remove label.
Parameters:
|
string |
$label: |
|
|
string |
$action: |
Either "create", "delete" or "rename" |
|
string |
$renamelabel: |
New name if renaming label |
API Tags:
Information Tags:
| Since: | 7 Jun 2005 |
| Author: | Neerav |
bool fetch(
string
$query
)
|
|
Fetch contents by URL query.
This is a "low-level" method. Please use GMailer::fetchBox() for fetching standard contents.
Parameters:
|
string |
$query: |
URL query string |
API Tags:
bool fetchBox(
constant
$type, mixed
$box, int
$parameter
)
|
|
Fetch contents from Gmail by type.
Content can be one of the following categories: - GM_STANDARD: For standard mail-boxes like Inbox, Sent Mail, All, etc. In such case, $box should be the name of the mail-box: "inbox", "all", "sent", "draft", "spam", or "trash". $paramter would be used for paged results.
- GM_LABEL: For user-defined label. In such case, $box should be the name of the label.
- GM_CONVERSATION: For conversation. In such case, $box should be the conversation ID.
- GM_QUERY: For search query. In such case, $box should be the query string.
- GM_PREFERENCE: For Gmail preference. In such case, $box = "".
- GM_CONTACT: For contact list. In such case, $box can be either "all", "search" or "detail". When $box = "detail", $parameter is the Contact ID. When $box = "search", $parameter is the search query string.
Parameters:
|
constant |
$type: |
Content category |
|
mixed |
$box: |
Content type |
|
int |
$parameter: |
Extra parameter. See above. |
API Tags:
bool getAttachment(
string
$attid, string
$msgid, string
$filename, [bool
$zipped = false]
)
|
|
Save attachment with attachment ID $attid and message ID $msgid to file with name $filename.
Parameters:
|
string |
$attid: |
Attachment ID. |
|
string |
$msgid: |
Message ID. |
|
string |
$filename: |
File name. |
|
bool |
$zipped: |
Save all attachment of message ID $msgid into a zip file. |
API Tags:
string[] getAttachmentsOf(
string[]
$convs, string
$path
)
|
|
Save all attaching files of conversations to a path.
Random number will be appended to the new filename if the file already exists.
Parameters:
|
string[] |
$convs: |
Conversations. |
|
string |
$path: |
Local path. |
API Tags:
| Return: | Name of the files saved. False if failed. |
bool getCookieFromBrowser(
)
|
|
Get cookies from browser.
API Tags:
bool getSessionFromBrowser(
)
|
|
Recover session information.
API Tags:
string[] getStandardBox(
)
|
|
Get names of standard boxes.
API Tags:
bool invite(
string
$email
)
|
|
Send Gmail invite to $email
Parameters:
API Tags:
| Return: | Success or not. Note that it will still be true even if $email is an illegal address. |
See if it is connected to GMail.
string lastActionStatus(
[string
$request = "message"]
)
|
|
Last action's action, status, message, and other info
Parameters:
|
string |
$request: |
What information you would like to request. Default is "message". |
bool performAction(
constant
$act, string[]
$id, [string
$para = ""]
)
|
|
Perform action on messages.
Examples:
Parameters:
API Tags:
bool removeCookieFromBrowser(
)
|
|
Remove all related cookies stored in browser.
bool removeSessionFromBrowser(
)
|
|
Remove all session information related to Gmailer.
API Tags:
bool saveCookieToBrowser(
)
|
|
Save (send) cookies to browser.
API Tags:
bool saveSessionToBrowser(
)
|
|
Save session data.
API Tags:
bool send(
string
$to, string
$subj, string
$body, [string
$cc = ""], [string
$bcc = ""], [string
$mid = ""], [string
$tid = ""], [string[]
$files = 0], [bool
$draft = false], [string
$orig_df = ""], [bool
$is_html = 0], [string
$from = ""]
)
|
|
Send Gmail. Or save a draft email.
Examples: - <?php
- // Simplest usage: send a new mail to one person:
- $gmailer->send("who@what.com", "Hello World", "Cool!\r\nFirst mail!");
-
- // More than one recipients. And with CC:
- $gmailer->send("who@what.com, boss@company.com",
- "Hello World",
- "This is second mail.",
- "carbon-copy@company.com");
-
- // With file attachment
- $gmailer->send("who@what.com",
- "Your file",
- "Here you are!",
- "", "", "", "",
- array("path/to/file.zip", "path/to/another/file.tar.gz"));
-
- // more examples...
- ?>
Parameters:
|
string |
$to: |
Recipient's address. Separated by comma for multiple recipients. |
|
string |
$subj: |
Subject line of email. |
|
string |
$body: |
Message body of email. |
|
string |
$cc: |
Address for carbon-copy (CC). Separated by comma for multiple recipients. $cc = "" for none. |
|
string |
$bcc: |
Address for blind-carbon-copy (BCC). Separated by comma for multiple recipients. $bcc = "" for none. |
|
string |
$mid: |
Message ID of the replying email. $mid = "" if this is a newly composed email. |
|
string |
$tid: |
Conversation (thread) ID of the replying email. $tid = "" if this is a newly composed email. |
|
string[] |
$files: |
File names of files to be attached. |
|
bool |
$draft: |
Indicate this email is saved as draft, or not. |
|
string |
$orig_df: |
If this email is saved as a modified draft, then set $orig_df as the draft ID of the original draft. |
|
bool |
$is_html: |
HTML-formatted mail, or not. |
|
string |
$from: |
Send mail as this email address (personality). $from = "" to use your Gmail address (NOT the default one in your settings). Note: you will NOT send your mail successfully if you do not register this address in your Gmail settings panel. |
API Tags:
Information Tags:
bool setGeneralSetting(
bool
$use_outgoing_name, string
$outgoing_name, bool
$use_reply_email, string
$reply_to, string
$language, int
$page_length, bool
$shortcut, bool
$indicator, bool
$snippet, bool
$custom_signature, string
$signature, string
$forward_to, string
$forward_action, int
$pop_setting, int
$pop_action, [bool
$expand_label_box = 1], [bool
$expand_invite_box = 1]
)
|
|
Set general settings of Gmail account.
Parameters:
|
bool |
$use_outgoing_name: |
Use outgoing name (instead of the default)? |
|
string |
$outgoing_name: |
Outgoing name |
|
bool |
$use_reply_email: |
Use replying email address (instead of the default)? |
|
string |
$reply_to: |
Replying email address |
|
string |
$language: |
Language |
|
int |
$page_length: |
Page length. Must be either 25, 50 or 100 |
|
bool |
$shortcut: |
Enable keyboard shortcut? |
|
bool |
$indicator: |
Enable personal level indicator? |
|
bool |
$snippet: |
Enable snippet? |
|
bool |
$custom_signature: |
Enable custom signature? |
|
string |
$signature: |
Custom signature |
|
string |
$forward_to: |
Address to auto-forward to. $forward_to = "" to disable auto-forward |
|
string |
$forward_action: |
Action on Gmail's copy of auto-forwarded emails. $forward_action = "" to keep them, "archive" to archive them, or "trash" to trash them |
|
int |
$pop_setting: |
POP settings. $pop_setting = 0 to disable POP access, 1 to remain unchanged, 2 to enable POP for new mails, or 3 to enable POP for all mails |
|
int |
$pop_action: |
Action on Gmail's copy of POP-accessed emails. $pop_action = 0 to keep them, 1 to archive them, or 2 to trash them |
|
bool |
$expand_label_box: |
Expand label box? |
|
bool |
$expand_invite_box: |
Expand invite box? |
API Tags:
Information Tags:
| Since: | 29 Jun 2005 |
| Author: | Neerav |
void setLoginInfo(
string
$my_login, string
$my_pwd, float
$my_tz
)
|
|
Set Gmail's login information.
Parameters:
|
string |
$my_login: |
Gmail's login name (without @gmail.com) |
|
string |
$my_pwd: |
Password |
|
float |
$my_tz: |
Timezone with respect to GMT, but in decimal. For example, -2.5 for -02:30GMT |
void setProxy(
string
$host, string
$username, string
$pwd
)
|
|
Setting proxy server.
Example: - <?php
- // proxy server requiring login
- $gmailer->setProxy("proxy.company.com", "my_name", "my_pwd");
-
- // proxy server without login
- $gmailer->setProxy("proxy2.company.com", "", "");
- ?>
Parameters:
|
string |
$host: |
Proxy server's hostname |
|
string |
$username: |
User name if login is required |
|
string |
$pwd: |
Password if required |
void setSessionMethod(
int
$method
)
|
|
Setting session management method.
You have to select a session management method so that GMailer would "remember" your identity. Method has to be one of the following values: - GM_USE_COOKIE | !GM_USE_PHPSESSION (if your server does not have PHP Session installed)
- !GM_USE_COOKIE | GM_USE_PHPSESSION (if your server have PHP Session installed, and don't want to set browser's cookie)
- GM_USE_COOKIE | GM_USE_PHPSESSION (if your server have PHP Session installed, and would like to use cookie to store session)
Parameters:
bool starMessageQuick(
string
$message_id, string
$action
)
|
|
Star/unstar a message quickly.
Parameters:
|
string |
$message_id: |
|
|
string |
$action: |
Either "star" or "unstar". |
API Tags:
Information Tags:
|
|