如何实现 Gmail OAuth API 来发送电子邮件(尤其是通过 SMTP)? [英] How to implement Gmail OAuth API to send email (especially via SMTP)?

查看:41
本文介绍了如何实现 Gmail OAuth API 来发送电子邮件(尤其是通过 SMTP)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网络应用程序,它将代表登录用户发送电子邮件.

我正在尝试使用在此处描述的新 Gmail OAuth 协议a> 通过用户的 Gmail 帐户发送这些电子邮件(最好使用 SMTP 而不是 IMAP,但我很容易).但是,示例 PHP 代码给我带来了一些问题.

  1. 所有示例代码都基于 IMAP,而不是 SMTP.如果您不打算向人们展示如何使用它,为什么要支持"SMTP 协议?
  2. 示例代码给我一个未捕获的 Zend 异常的致命错误 - 它找不到INBOX"文件夹.

<块引用>

致命错误:路径\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php:467 中带有消息无法更改文件夹,可能不存在"的未捕获异常Zend_Mail_Storage_Exception":#0 path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php(248): Zend_Mail_Storage_Imap->selectFolder('INBOX') #1 path\to\xoauth-php-samples\three-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #2 {main} 下一个异常'Zend_Mail_Storage_Exception' 带有消息'不能选择收件箱,这是一个有效的传输吗?'在 path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php:254 堆栈跟踪:#0 path\to\xoauth-php-samples\three-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #1 {main} 在 path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php 中的第 254 行

我已经确认我获得了良好的 OAuth 令牌,但我不知道如何进行实际的电子邮件交易.这个协议还很新,所以没有太多关于它的非官方社区文档,官方文档是关于 SMTP RFC 的无用干货.因此,如果有人能帮助完成这项工作,我将不胜感激.

注意:我已经能够通过 SSL 连接到 Gmail 的 SMTP 服务器并成功发送电子邮件,前提是用户向我的应用程序提供了他/她的 Gmail 用户名和密码.我想避免使用这种方法,因为它鼓励网络钓鱼,而注重安全的用户不会接受它.这个问题不是关于那个.

解决方案

我正在使用 Google Apps 帐户并尝试创建一个应用程序,该应用程序允许我的用户通过新的 Oauth 授权通过 SMTP 发送邮件.我能够使用有关此讨论的一些信息使其正常工作.但是,我认为我应该澄清一些我偶然发现的事情......

1) SMTP 的 Zend 框架显然会自动在 Zend/Mail/Protocol/Smtp/Auth 文件夹中查找文件名 .php,在这种情况下,Xoauth.php"通常不存在于 Zend 中,除非您创建它.通过将现有的 Login.php 文件复制到 Xoauth.php 并使用此网页上提出的建议对其稍作修改,我能够成功地做到这一点.

这很有帮助,但它只是 Xoauth.php 文件的一部分(编辑 login.php 的副本,您将看到一个等效区域):

//确保 AUTH 尚未启动.父::身份验证();$this->_send('AUTH XOAUTH' . $this->_xoauth_request);$this->_expect(235);$this->_auth = true;

2) 请注意,即使您直接在 PHP 中引用它,您也绝对应该在 PHP include_path 上安装 Zend,因为它可能会尝试在不使用显式路径的情况下使用其 Loader.php 文件来引用自身.

3) 除了删除明显的 IMAP 函数并用等效的 SMTP 函数替换它们之外,我不需要更改用于 PHP 的 Google OAuth 示例中的代码.我确实必须包含 Zend/Mail.php 文件才能发送电子邮件并添加实际发送电子邮件所需的代码以使测试正常工作.

I'm developing a web application that will send emails on behalf of a logged-in user.

I'm trying to use the new Gmail OAuth protocol announced described here to send these emails through the user's Gmail account (preferably using SMTP rather than IMAP, but I'm easy). However, the sample PHP code gives me a couple of problems.

  1. All of the sample code is based on IMAP, not SMTP. Why "support" the SMTP protocol if you're not going to show people how to use it?
  2. The sample code gives me a fatal error from an uncaught Zend exception -- it can't find the "INBOX" folder.

Fatal error: Uncaught exception 'Zend_Mail_Storage_Exception' with message 'cannot change folder, maybe it does not exist' in path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php:467 Stack trace: #0 path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php(248): Zend_Mail_Storage_Imap->selectFolder('INBOX') #1 path\to\xoauth-php-samples\three-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #2 {main} Next exception 'Zend_Mail_Storage_Exception' with message 'cannot select INBOX, is this a valid transport?' in path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php:254 Stack trace: #0 path\to\xoauth-php-samples\three-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #1 {main} in path\to\xoauth-php-samples\Zend\Mail\Storage\Imap.php on line 254

I've verified that I'm getting good OAuth tokens back, I just don't know how to make the actual email transaction happen. This protocol is still rather new, so there's not much unofficial community documentation about it out there, and the official docs are unhelpfully dry stuff about the SMTP RFC. So if anyone can help get this going, I'd greatly appreciate it.

Note: I've already been able to connect to Gmail's SMTP server via SSL and successfully send an email, provided that the user has given my application his/her Gmail username and password. I'd like to avoid this method, because it encourages phishing and security-minded users won't accept it. This question is not about that.

解决方案

I'm using a Google Apps account and trying to create an application that allows my users to send mail through SMTP via the new Oauth authorization. I was able to get it to work using some of the information on this discussion. However, I think I should clarify a couple things that I stumbled over...

1) The Zend framework for SMTP apparently automatically looks in the Zend/Mail/Protocol/Smtp/Auth folder for a file name .php in this case "Xoauth.php" which does NOT normally exist in Zend unless you create it. I was able to do this successfully by copying the existing Login.php file to Xoauth.php and modifying it slightly with the suggestions made on this web page.

This was very helpful but it is only part of the Xoauth.php file (Edit a copy of login.php and you will see an equivalent area):

// Ensure AUTH has not already been initiated.
parent::auth();
$this->_send('AUTH XOAUTH ' . $this->_xoauth_request);
$this->_expect(235);
$this->_auth = true;

2) Note that you should definitely have Zend on the PHP include_path even if you reference it directly in PHP because it may try to reference itself with its Loader.php file without using an explicit path.

3) Besides removing the obvious IMAP functions and replacing them with the equivalent SMTP functions I did not need to change the code from the Google OAuth samples for PHP. I did have to include the Zend/Mail.php file in order to Send email and add the code necessary to actually send email for the test to work.

这篇关于如何实现 Gmail OAuth API 来发送电子邮件(尤其是通过 SMTP)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆