afinogen89 / get-mail
从 POP3 获取邮件
0.0.7
2019-02-18 12:23 UTC
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-09-23 22:14:08 UTC
README
用于从 POP3 服务器获取邮件的类
部分代码从 zf2 中提取,其余为自编。
最初是为 yii2 编写的,但也可以独立使用。
安装
通过 composer 进行安装
php composer.phar require --prefer-dist afinogen89/get-mail "dev-master"
或者
"afinogen89/get-mail": "*"
在 composer.json
文件中添加。
使用
仅支持协议操作
$pop3 = new afinogen89\getmail\protocol\Pop3('example.ru'); $pop3->login('data@example.ru', '123456'); $msgList = $pop3->getList(); $pop3->logout();
处理邮件
POP3 协议
$storage = new afinogen89\getmail\storage\Pop3(['host' => 'example.ru', 'user' => 'data@example.ru', 'password' => '123456']); $msg = $storage->getMessage(1); $msg->saveToFile('/tmp/1.eml'); echo $msg->getHeaders()->getSubject(); foreach($msg->getParts() as $part) { echo $part->getContentDecode().PHP_EOL; } foreach($msg->getAttachments() as $t) { $t->saveToFile('/tmp/' . $t->filename); }
通过邮件文件夹(文件扩展名为 eml)
$storage = new afinogen89\getmail\storage\File(['path' => '../email/']); $msg = $storage->getMessage(1); $msg->saveToFile('/tmp/1.eml'); echo $msg->getHeaders()->getSubject();
连接到 gmail 的示例
$storage = new afinogen89\getmail\storage\Pop3(['host' => 'pop.gmail.com', 'user' => 'test@gmail.com', 'password' => 'pass', 'ssl' => 'SSL']);
要生成密码,需要创建一个应用程序,请访问 https://security.google.com/settings/security/apppasswords
通过配置连接到所需存储库,可以在不更改代码的情况下快速切换。
$storage = afinogen89\getmail\storage\Storage::init( [ 'storage' => \afinogen89\getmail\storage\Storage::POP3, 'host' => 'pop.gmail.com', 'user' => 'test@gmail.com', 'password' => '123456', 'ssl' => 'SSL' ] );
连接到 yandex
为了脚本正常工作,需要在设置中(邮件 - 所有设置 - 邮件程序)勾选“通过 pop.yandex.ru 的 POP3 协议”和“在 POP3 获取邮件时将雅虎邮箱中的邮件标记为已读”。
$storage = new \afinogen89\getmail\storage\Pop3( [ 'host' => 'pop.yandex.ru', 'user' => 'test@yandex.ru', 'password' => '123456', 'ssl' => 'SSL' ] );
英文版本
安装
通过 composer 安装此扩展是首选方法。
运行
php composer.phar require --prefer-dist afinogen89/get-mail "dev-master"
或者
"afinogen89/get-mail": "*"
添加到 composer.json
文件的依赖项部分。
使用
扩展安装完成后,只需在代码中使用 POP3 协议即可
$pop3 = new afinogen89\getmail\protocol\Pop3('example.ru'); $pop3->login('data@example.ru', '123456'); $msgList = $pop3->getList(); $pop3->logout();
从邮件中获取消息
$storage = new afinogen89\getmail\storage\Pop3(['host' => 'example.ru', 'user' => 'data@example.ru', 'password' => '123456']); $msg = $storage->getMessage(1); $msg->saveToFile('/tmp/1.eml'); echo $msg->getHeaders()->getSubject(); foreach($msg->getParts() as $part) { echo $part->getContentDecode().PHP_EOL; } foreach($msg->getAttachments() as $t) { $t->saveToFile('/tmp/' . $t->filename); }