alexandresalome/mailcatcher

访问 MailCatcher 的库

v1.4.1 2024-03-29 15:42 UTC

This package is auto-updated.

Last update: 2024-08-29 16:49:57 UTC


README

Build status Latest Stable Version Total Downloads License Monthly Downloads Daily Downloads

集成了 MailCatcher 到您的 PHP 应用程序中。

MailCatcher 是一个简单的具有 HTTP API 的 SMTP 服务器,这个库旨在将其集成,以便轻松地在 PHP 中使用。

Behat 扩展

这个库提供 Behat 扩展,以帮助您测试应用程序中的邮件。

要使用它,您首先需要确保 MailCatcher 已正确安装并运行。您可以使用 docker 来执行它

docker run -d -p 1080:1080 -p 1025:1025 --name mailcatcher schickling/mailcatcher

首先,在您的 behat.yml 中进行配置

default:
    extensions:
        Alex\MailCatcher\Behat\MailCatcherExtension\Extension:
            url: http://localhost:1080
            purge_before_scenario: true

然后,在您的 FeatureContext 文件中添加 MailCatcherContext 上下文

use Alex\MailCatcher\Behat\MailCatcherContext;
use Behat\Behat\Context\BehatContext;

class FeatureContext extends BehatContext
{
   public function __construct(array $parameters)
   {
      $this->useContext('mailcatcher', new MailCatcherContext());
   }
}

可用步骤

此扩展在测试中为您提供邮件上下文。要使用断言,您必须首先使用标准打开一个邮件。

打开后,您可以在其上执行断言并 点击

服务器操作

删除服务器上的所有消息

  • 当我清理邮件时

邮件打开

  • 当我从 "foo@example.org" 打开邮件时
  • 当我打开包含 "一条消息" 的邮件时
  • 当我打开发送给 "me@example.org" 的邮件时
  • 当我打开主题为 "欢迎,邦德先生!" 的邮件时

断言

验证发送到服务器的邮件数量

  • 然后应该发送 1 封邮件
  • 然后应该发送 13 封邮件

验证消息中存在文本

  • 然后我应该看到邮件中 "一些内容"
  • 然后我应该看到邮件中 "其他内容"

验证不打开邮件中的文本存在

  • 然后我应该看到来自 "foo@example.org" 的邮件
  • 然后我应该看到包含 "一条消息" 的邮件
  • 然后我应该看到发送给 "me@example.org" 的邮件
  • 然后我应该看到主题为 "欢迎,邦德先生!" 的邮件

自定义 MailCatcher 上下文

仅适用于 PHP 5.4 及以上版本

如果您想创建一个与 MailCatcher 相关的上下文类,您可以使用 MailCatcherTrait 在您的类中注入 MailCatcher 客户端。

use Alex\MailCatcher\Behat\MailCatcherAwareInterface;
use Alex\MailCatcher\Behat\MailCatcherTrait;
use Alex\MailCatcher\Message;
use Behat\Behat\Context\Context;

class WelcomeContext implements Context, MailCatcherAwareInterface
{
    use MailCatcherTrait;

    /**
     * @Then /^a welcome mail should be sent$/
     */
    public function testTrait()
    {
        $this->findMail(Message::SUBJECT_CRITERIA, 'Welcome!');
    }
}

此 trait 提供以下方法

  • getMailCatcherClient(): 返回 MailCatcher Client 实例。
  • findMail($criteria, $value): 搜索指定消息的功能,如果未找到则抛出异常

不要忘记 实现 MailCatcherAwareInterface 以在您的上下文类中注入 MailCatcher 客户端。

客户端 API

使用集成 SDK 轻松浏览您的 API

$client = new Alex\MailCatcher\Client('http://localhost:1080');

// Returns all messages
$messages = $client->search();

// Count messages
$client->getMessageCount();

// Filter messages
$messages = $client->search(array(
    'from'        => 'bob@example.org',
    'to'          => 'alice@example.org',
    'subject'     => 'Bla',
    'contains'    => 'Hello',
    'attachments' => true,
    'format'      => 'html',
), $limit = 3);

// Search one message
$message = $client->searchOne(array('subject' => 'Welcome'));

消息 API

// Message API, get the content of a message
$subject = $message->getSubject();
$plainTextBody = $message->getPart('text/plain')->getContent();
$htmlBody = $message->getPart('text/html')->getContent();

// Message API, return a Person object or an array of Person object
$person  = $message->getFrom();
$persons = $message->getRecipients();

// Person API
$person = $message->getFrom();

$name = $person->getName(); // null means not provided
$mail = $person->getMail();

// Attachments
$message->hasAttachments();
$message->getAttachments();

// Delete
$message->delete();

附件 API

// Attachment API
$attachment->getFilename();
$attachment->getSize();
$attachment->getType();
$attachment->getContent();