在SaaS网站上显示Gmail收件箱中的电子邮件

时间:2014-11-11 03:40:20

标签: php google-api gmail gmail-api

有没有办法用我的SaaS网站上的Gmail收件箱显示电子邮件?

1 个答案:

答案 0 :(得分:1)

更新:OP编辑了询问PHP客户端库的问题。

PHP Gmail Google Client Library可用,但仍处于测试阶段。关于如何使用Google API的代码段落在Github Repository中,但Gmail API示例不可用。这个Gmail API definition file是一个开始的好地方。


您可以使用Google Gmail API。它在.NETJavaPython中有客户端库。引用文档:

  

您的应用可以使用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