taq/delayedmail

将电子邮件消息排队到文件中以稍后发送

1.0.5 2016-04-05 22:25 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:46:13 UTC


README

这是一个简单的应用程序,用于通过PHP发送电子邮件而不会阻塞发送并等待SMTP服务器的响应。它提供了一些类,如

  • Message 用于编写消息
  • Server 用于连接到SMTP服务器
  • Sender 用于运行和发送队列中的消息
  • Runner 用于激活Sender对象

工作原理

首先,我们需要服务器配置。在 test 目录中有一个名为 delayedmail.ini 的示例文件

host = smtp.gmail.com
port = 587
user = taq
password = secret
path = /tmp/delayedmailtest

唯一的区别参数是 path 参数。这是邮件文件将被存储的位置。

非常重要

如果您在Gmail账户上使用双因素认证,则使用此库发送电子邮件将不会工作。您需要一个不太安全的账户才能使其工作。

存储稍后发送的消息

使用的数据存储只是普通的纯文本文件。它们存储在上述配置的 path 上。在该目录中,将有两个子目录

  • delivery 其中包含队列中的消息。
  • sent,消息在 Sender 发送后将移动到这里。
  • error,如果消息存在错误,则将消息移动到这里。

如何使用它

排队消息

只需在您的应用程序中包含 delayedmail.php,创建一个新的 Server 对象,进行配置,编写并排队一条新消息

<?php
   include_once "delayedmail.php";

   $server = new DelayedMail\Server("myconfigs.ini");
   $msg    = new DelayedMail\Message();
   $msg->from("taq <eustaquiorangel@gmail.com>")->
           to("Eustaquio Rangel <taq@bluefish.com.br>")->
           cc("Eustaquio Rangel <taq@anotherhost.com>")->
      subject("DelayedMail test!")->
         text("This is just\na test!")->
       attach("taq.jpg");
   $server->push($msg);
?>

现在检查 delivery 目录,那里将有一个包含消息内容的文件。

您可以在 attachcc 上使用数组。

运行运行器

只需编辑 runner.php 文件,设置所需间隔和配置文件(通常是与服务器相同的配置文件)并从命令行运行它

<?php
$dir = dirname(__FILE__);
echo "- loading classes from $dir\n";
include_once "$dir/delayedmail.php";

$sender = new DelayedMail\Sender(5,"delayedmail.ini");
$sender->run();
?>
$ php runner.php
- initializing ...
- checking for files in /tmp/delayedmailtest/delivery ...
- no files found.

测试

发送电子邮件

转到 test 目录,将 delayedmail.initest.php 文件配置到您想要的配置,然后运行

$ php test.php

然后检查您配置的地址上的电子邮件应用程序。

代码

运行 composer update,转到 test 目录并运行

$ phpunit .