【发布时间】:2014-11-11 03:40:20
【问题描述】:
有没有办法在我的 SaaS 网站中用 PHP 显示来自 Gmail 收件箱的电子邮件?
【问题讨论】:
标签: php google-api gmail gmail-api
有没有办法在我的 SaaS 网站中用 PHP 显示来自 Gmail 收件箱的电子邮件?
【问题讨论】:
标签: php google-api gmail gmail-api
更新:OP 编辑了询问 PHP 客户端库的问题。
PHP Gmail Google Client Library 可用,但仍处于测试阶段。关于如何使用 Google API 的代码 sn-ps 在他们的 Github Repository 中,但 Gmail API 示例不可用。这个Gmail API definition file 是一个很好的起点。
您可以使用Google Gmail API。它在.NET、Java 和Python 中有客户端库。引用自文档:
您的应用可以使用 API 添加 Gmail 功能,例如:
- 阅读来自 Gmail 的邮件
- 发送电子邮件
- 修改应用于消息和线程的标签
- 搜索特定消息和线程
根据他们的快速入门指南如何使用 API 的示例:
#!/usr/bin/python
import httplib2
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
# Location of the credentials storage file
STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# Retrieve a page of threads
threads = gmail_service.users().threads().list(userId='me').execute()
# Print ID for each thread
if threads['threads']:
for thread in threads['threads']:
print 'Thread ID: %s' % (thread['id'])
更多详情,请参考快速入门指南:https://developers.google.com/gmail/api/quickstart/quickstart-python
关于这件事有一个similar question。
【讨论】: