如何实现 Gmail OAuth API 发送电子邮件(尤其是通过 SMTP)?

2022-01-17 00:00:00 oauth api smtp gmail php

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

我正在尝试使用 此处此处宣布的新 Gmail OAuth 协议

a> 通过用户的 Gmail 帐户发送这些电子邮件(最好使用 SMTP 而不是 IMAP,但我很容易).但是,示例 PHP 代码给我带来了一些问题.

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

<块引用>

致命错误:在 path oxoauth-php-samplesendMailStorageImap.php:467 中未捕获异常 'Zend_Mail_Storage_Exception' 并带有消息 '无法更改文件夹,也许它不存在' 堆栈跟踪:#0 path oxoauth-php-samplesendMailStorageImap.php(248): Zend_Mail_Storage_Imap->selectFolder('INBOX') #1 path oxoauth-php-samples hree-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #2 {main} 下一个异常 'Zend_Mail_Storage_Exception' 带有消息'无法选择收件箱,这是一个有效的传输吗?在 path oxoauth-php-samplesendMailStorageImap.php:254 堆栈跟踪:#0 path oxoauth-php-samples hree-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #1 {main} 在第 254 行的 path oxoauth-php-samplesendMailStorageImap.php 中

我已确认我收到了良好的 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.父::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 oxoauth-php-samplesendMailStorageImap.php:467 Stack trace: #0 path oxoauth-php-samplesendMailStorageImap.php(248): Zend_Mail_Storage_Imap->selectFolder('INBOX') #1 path oxoauth-php-samples hree-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 oxoauth-php-samplesendMailStorageImap.php:254 Stack trace: #0 path oxoauth-php-samples hree-legged.php(184): Zend_Mail_Storage_Imap->__construct(Object(Zend_Mail_Protocol_Imap)) #1 {main} in path oxoauth-php-samplesendMailStorageImap.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.

相关文章