hungtrinh / zend-mail-oauth2
IMAP、POP3、SMTP使用XOAUTH2认证与Zend Mail、Zend Framework 1兼容
1.0.1
2023-02-27 10:18 UTC
Requires
- php: >=5.3
- zf1s/zend-mail: ^1.15
Requires (Dev)
- league/oauth2-google: ^4.0
- thenetworg/oauth2-azure: ^2.1
Suggests
- league/oauth2-google: Required for Google OAuth 2.0 client provider
- thenetworg/oauth2-azure: Required for Azure Active Directory OAuth 2.0 client provider
README
本包帮助项目使用zend-mail(zend Framework 1)与oauth2认证(XOAUTH2)进行工作
PHP 5.3-8.x兼容
已在Gmail、Azure AD上进行测试
目录
安装
composer require hungtrinh/zend-mail-oauth2
使用方法
与gmail(azure mail)一起工作的代码示例
- 指南:创建Google Web应用、Azure Web应用(提供client_id、secret_id)以获取您的Web应用所需的oauth2 access_token、refresh_token。
- 使用oauth2(xoauth2协议)进行认证:邮箱 + access_token
- 通过IMAP检索15封邮件
- 通过POP3检索15封邮件
- 通过SMTP发送电子邮件
git checkout https://github.com/hungtrinh/zend-mail-oauth2.git cd zend-mail-oauth2 composer install cd sample php -S 0:8080
打开浏览器访问url https://:8080。
通过IMAP检索邮件
$email = 'your_gmail@gmail.com'; $oauth2AccessToken = 'oauth2_access_token of your_gmail@gmail.com'; $imapGmailHost = 'imap.gmail.com'; $imap365Host = 'outlook.office365.com'; //use this host if working with office365 $imapProtocol = new Zend_Mail_Protocol_ImapOauth2($imapGmailHost, $port = '993', $ssl = true); if (!$imapProtocol->login($email, $oauth2AccessToken)) { throw new \DomainException('Invalid email or oauth2 access token expired'); } $index = 0; $max = 10; $mail = new Zend_Mail_Storage_Imap($imapProtocol); echo $mail->countMessages(); foreach($mail as $messageNum => $message) { echo $message->subject; if ($max === ++$index) { break; } }
通过POP3检索邮件
$email = 'your_gmail@gmail.com'; $$oauth2AccessToken = 'oauth2_access_token of your_gmail@gmail.com'; $popGmailHost = 'pop.gmail.com'; $pop365Host = 'outlook.office365.com'; //use this host if working with office365 $pop3Protocol = new Zend_Mail_Protocol_Pop3Oauth2($popGmailHost, $port = '995', $ssl = true); if (!$pop3Protocol->login($email, $oauth2AccessToken)) { throw new \DomainException('Invalid email or oauth2 access token expired'); } $index = 0; $max = 10; $mail = new Zend_Mail_Storage_Pop3($pop3Protocol); echo $mail->countMessages(); foreach($mail as $messageNum => $message) { echo $message->subject; if ($max === ++$index) { break; } }
通过SMTP发送邮件
$office365Host = 'smtp.office365.com'; //use this host if working with office365 $gmailHost = 'smtp.gmail.com'; $mailSender = 'mail_sender@gmail.com'; $oauth2AccessToken = 'access token of mail_sender@gmail.com'; $transport = new Zend_Mail_Transport_Smtp($gmailHost, [ 'ssl' => 'tls', 'port' => 587, 'auth' => 'oauth2', // Zend_Mail_Protocol_Smtp_Auth_Oauth2 'email' => $mailSender, 'accessToken' => $oauth2AccessToken, ]); Zend_Mail::setDefaultTransport($transport); Zend_Mail::setDefaultFrom($mailSender, 'sender fullname'); Zend_Mail::setDefaultReplyTo($mailSender,'sender fullname'); $mail = new Zend_Mail(); $date = date('Y-m-d H:i:s P'); $mail->addTo($mailTo) ->setSubject("Test xoauh2 - smtp $date") ->setBodyText("$date : smtp + xoauth2 send mail test"); $mail->send();
如何获取$oauth2AccessToken
您的当前Web应用需要添加功能以维护access_token
- 公开uri处理程序,从google / azure /其他Web应用接收oauth2响应 http://your_app_domainoauth2-callback.php(将access_token、refresh_token、过期时间持久化到数据库或其他持久存储)
- access_token的有效期短(1小时),因此您可以(可选)
- 设置cron-job每50分钟运行一次,使用refresh_token获取新的access_token,然后持久化access_token、refresh_token、过期时间到数据库。其他进程将使用access_token发送或检索邮件
- 或者在发送/检索邮件的过程中使用过期时间验证access_token是否已过期,然后使用refresh_token获取新的access_token
完整指南请参阅与gmail(azure mail)一起工作的代码示例部分
为zend framework 1骨架项目设置zend-mail-oauth2的自动加载
在像这样初始化Zend_Application之后添加行require_once __DIR__ . '/../vendor/autoload.php';
,public/index.php
文件内容
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), ))); /** Zend_Application */ require_once 'Zend/Application.php'; // Create application, bootstrap, and run $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' ); require_once __DIR__ . '/../vendor/autoload.php'; $application->bootstrap(); $application->run();