rpurinton / gmail
A Gmail Library for PHP
1.0.35
2025-03-05 21:33 UTC
Requires
- rpurinton/config: ^1.0.25
- rpurinton/https: ^1.0.19
This package is auto-updated.
Last update: 2025-03-05 21:34:17 UTC
README
Introduction
The RPurinton Gmail Library is a PHP library designed to facilitate interaction with Gmail using Google's API. It provides functionality for sending, receiving, and managing Gmail messages efficiently.
Installation
Composer Installation
To install the library, use Composer:
composer require rpurinton/gmail
Configuration Setup
- Create a
config
directory at the same level as thevendor
directory. - Place the
Gmail.json
file with your credentials in theconfig
directory.- Note: The
access_token
,refresh_token
, andexpires_at
will be added later by the library.
- Note: The
Google Cloud Project Setup
- Create a Google Cloud project.
- Enable the Gmail API.
- Create OAuth 2.0 credentials.
- Download the credentials JSON file and place it in the
config
directory asGmail.json
.
Usage
Initialization
- Create a new instance of the
Gmail
class. - Run the
->init()
method to start the OAuth2 authentication flow. - This method will redirect to Google for authentication and back to your script with the authorization code.
- The
init()
method is crucial for the first-time setup to obtain theaccess_token
andrefresh_token
. - These tokens are stored in the
Gmail.json
configuration file. - Once the tokens are obtained, the
init()
method is not required again unless the user deauthorizes the app from their Gmail account.
API Methods
send()
: Send an email with optional attachments, CC, and BCC.list()
: List messages based on a query and maximum results.read()
: Retrieve a specific message by ID.delete()
: Delete messages in batch.getAttachmentIds()
: Retrieve attachment IDs from a message.
Examples
<?php require 'vendor/autoload.php'; use RPurinton\Gmail\Gmail; $gmail = new Gmail(); $gmail->init(); // Only needed for the first-time setup // Example to send an email $gmail->send('from@example.com', ['to@example.com'], 'Subject', 'Email body');
Troubleshooting
- Ensure that the
Gmail.json
file is correctly configured and accessible. - Verify that the Google Cloud project is set up with the correct credentials and permissions.
- If you encounter issues with token expiration, ensure the
refresh_token
is valid and theinit()
method was successfully executed initially.
License
This library is licensed under the MIT License. See the LICENSE file for more information.