k3roulas/email-watcher

邮件监控:在收到新邮件时触发动作(协议pop3,imap)

0.1.1 2015-09-16 19:27 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:54:43 UTC


README

在收到新邮件时触发动作(协议pop3,imap)

##安装

通过composer

composer require k3roulas/email-watcher

请注意,此包依赖于tedivm/fetch包 https://github.com/tedious/Fetch

您需要安装php5-imap模块。

在Ubuntu上

apt-get install php5-imap
php5enmod imap

##使用方法

安装库后,您应使用内置的NewEmailWatcherDump进行测试。

一旦您验证了应用程序与您的邮件服务器通信,请实现NewEmailWatcherInterface接口,并使用自己的类代替NewEmailWatcherDump。以下是一个示例。

Gmail

使用Gmail账户,您需要启用pop3或imap。尝试一下。等待您在电子邮件中收到的安全警报。按照指示操作。您可能需要设置“不安全应用”选项: https://www.google.com/settings/security/lesssecureapps

##示例

// example with a gmail account
$port = 993;
$email = 'bob@sinclar.dance';
$password = 'IAmNotDavidGuetta';
$filename = './lastuid.json';
$server = 'imap.gmail.com';

// Nominal usage
$newEmailWatcher = new \K3roulas\EmailWatcher\NewEmailWatcherDump();

$emailWatcher = new \K3roulas\EmailWatcher\Watcher();
$emailWatcher->setServer($server)
    ->setPort($port)
    ->setEmail($email)
    ->setPassword($password)
    ->setFilename($filename)
    ->setProtocol('imap')
    ->setNewEmailWatcher($newEmailWatcher)
    ->init();

$emailWatcher->process();


// Use another persitence service : Create your own class that implement LastUidPersistInterface
$lastUidPersist = new MyPersitenceService();
$newEmailWatcher = new \K3roulas\EmailWatcher\NewEmailWatcherDump();

$emailWatcher = new \K3roulas\EmailWatcher\Watcher();
$emailWatcher->setServer($server)
    ->setPort($port)
    ->setEmail($email)
    ->setPassword($password)
    ->setProtocol('imap')
    ->setNewEmailWatcher($newEmailWatcher)
    ->setLastUidPersist($lastUidPersist)
    ->init();

$emailWatcher->process();



// You need more complicated options to access to the server @see Fetch/Server package

$fetchServer = new \Fetch\Server($server, $port);
// For example if you use GSSAPI or  NTLM
$fetchServer->setAuthentication($email, $password, false);

$newEmailWatcher = new \K3roulas\EmailWatcher\NewEmailWatcherDump();

$emailWatcher = new \K3roulas\EmailWatcher\Watcher();
$emailWatcher->setFetchServer($fetchServer)
    ->setNewEmailWatcher($newEmailWatcher)
    ->setFilename($filename)
    ->init();

$emailWatcher->process();