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

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

问题描述

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

我试图用新的Gmail OAuth协议在这里宣布描述通过用户的Gmail帐户发送这些电子邮件(preferably使用SMTP而非IMAP,但我很容易)。但是,示例PHP code给了我几个问题。


  1. 所有code样品是基于IMAP,SMTP没有。为什么支持SMTP协议,如果你不打算向人们展示如何使用它?

  2. 示例code给我从一个未捕获的Zend异常的致命错误 - 它不能找到收件箱文件夹


  

致命错误:未捕获的异常'Zend_Mail_Storage_Exception有消息无法改变文件夹,也许它不存在'路径\\为\\ XOAUTH的PHP样本\\ Zend的\\邮件\\存储\\ Imap.php:467堆栈跟踪:# 0路径\\为\\ XOAUTH的PHP样本\\ Zend的\\邮件\\存储\\ Imap.php(248):Zend_Mail_Storage_Imap-> selectFolder(收件箱)#1路径\\为\\ XOAUTH的PHP样本\\三legged.php (184):Zend_Mail_Storage_Imap - > __构造(对象(Zend_Mail_Protocol_Imap))#2主{}接下来异常'Zend_Mail_Storage_Exception有消息?无法选择收件箱,这是一个有效的交通在路径\\为\\ XOAUTH的PHP样本\\ Zend的\\邮件\\存储\\ Imap.php:254堆栈跟踪:#0路径\\为\\ XOAUTH的PHP样本\\三legged.php(184):Zend_Mail_Storage_Imap - > __构造(对象(Zend_Mail_Protocol_Imap))#1 {}主要路径\\为\\ XOAUTH的PHP样本\\ Zend的\\邮件\\存储\\ Imap.php线254


我已经验证了我得到很好的OAuth令牌回来,我只是不知道如何让实际的电子邮件的交易发生。该协议仍然是相当新的,所以有没有关于它的很多非官方的社区文档在那里,和官方的文档有关SMTP RFC帮倒忙枯燥。所以,如果有人可以帮助得到这个下去,我会非常AP preciate它。

注意:我已经能够通过SSL连接到Gmail的SMTP服务器,并成功地发送电子邮件,前提是用户已经给我的申请他/她的Gmail用户名和密码。我想避免这种方法,因为它鼓励网络钓鱼和安全意识的用户不会接受它。这个问题是不是这个。


解决方案

我使用谷歌Apps帐户,并试图创建一个应用程序,可以让我的用户通过新的OAuth授权通过SMTP发送邮件。我能得到它使用一些对这个讨论的信息工作。不过,我想我应该澄清的是,我绊了一对夫妇的事情...

1)SMTP Zend框架显然会自动查找在Zend /邮件/协议/ SMTP /验证在这种情况下,Xoauth.php文件名的.php,它并不在Zend的,除非你创建它通常存在的文件夹。我能够通过复制现有的login.php文件Xoauth.php以及与此网页上提出的建议,稍微修改它成功地做到这一点。

这是非常有益的,但​​它只是Xoauth.php文件的一部分(编辑的login.php中的副本,你会看到一个等效面积):

  //确保AUTH尚未启动。
父:: AUTH();
$这个 - > _send('AUTH XOAUTH'$这个 - 方式> _xoauth_request);
$这个 - > _expect(235);
$这个 - > _auth = TRUE;

2)请注意,你一定要对PHP的include_path Zend的,即使你直接在PHP中引用它,因为它可能会尝试与它Loader.php文件引用自身,而无需使用显式路径。

3)除了去除明显的IMAP功能,用等价SMTP功能代替他们,我并不需要从谷歌的OAuth样本PHP改变code。我也必须包括了Zend / Mail.php文件,以发送电子邮件,并添加必要的code实际的测试工作发送电子邮件。

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天全站免登陆