PHPackages                             ingenerator/mailhook - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. ingenerator/mailhook

ActiveLibrary[Testing &amp; Quality](/categories/testing)

ingenerator/mailhook
====================

Collects email from a local postfix server so you can inspect, assert and otherwise mess around with emails sent during development.

v1.3.0(10mo ago)345.5k↓39.3%2BSD-3-ClausePHPPHP ~8.2.0 || ~8.3.0 || ~8.4.0

Since Sep 1Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/ingenerator/mailhook)[ Packagist](https://packagist.org/packages/ingenerator/mailhook)[ Docs](https://github.com/ingenerator/mailhook)[ RSS](/packages/ingenerator-mailhook/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (3)Versions (10)Used By (0)

Mailhook
========

[](#mailhook)

[![License](https://camo.githubusercontent.com/f89cfeaa10d2062d4aec9765d00e3b0fc7c7095e8c7ce83efde187c1368791d7/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6d61696c686f6f6b2f6c6963656e73652e737667)](https://packagist.org/packages/ingenerator/mailhook)[![Build status](https://github.com/ingenerator/mailhook/actions/workflows/test.yaml/badge.svg)](https://github.com/ingenerator/mailhook/actions/workflows/test.yaml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/aa8c139782d8293ace6a9cfa00ecad794b43bec464e0fe500aa453cfc8b88387/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696e67656e657261746f722f6d61696c686f6f6b2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/ingenerator/mailhook/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/29da56512f352e2248ee28bd97220c12a37819456d1aad14a543b8b8f8b8ce60/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6d61696c686f6f6b2f762f737461626c652e737667)](https://packagist.org/packages/ingenerator/mailhook)[![Total Downloads](https://camo.githubusercontent.com/cf92ee7974660853b8cb49a12a776c3d3cfb48d0e7e737bd0f40edef6c56c86b/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6d61696c686f6f6b2f646f776e6c6f6164732e737667)](https://packagist.org/packages/ingenerator/mailhook)[![Latest Unstable Version](https://camo.githubusercontent.com/f82e7d509c31da7b0dd6f8d990024cfc4ff5df83de95fc704c908b3b1d158976/68747470733a2f2f706f7365722e707567782e6f72672f696e67656e657261746f722f6d61696c686f6f6b2f762f756e737461626c652e737667)](https://packagist.org/packages/ingenerator/mailhook)

A php library that collects email from a local postfix server so you can inspect, assert and otherwise mess around with emails sent during development.

Installing
----------

[](#installing)

Add mailhook to your development dependencies in composer.json:

```
{
  "require-dev": {
    "ingenerator/mailhook" : "~0.1@dev"
  }
}
```

You will also need to configure postfix to deliver all outbound mail to a local file. If you're using chef, see our [postfix-relay](https://github.com/ingenerator/chef-postfix-relay) cookbook, and configure the "postfix\_relay.allow\_live\_email" attribute to false. To install manually, `apt-get install postfix` and then append to your postfix configuration as follows.

```
# /etc/postfix/main.cf
default_transport = fs_mail

```

```
# /etc/postfix/master.cf

#
# fs_mail sends all outgoing mail to a single local file
#
fs_mail    unix  -       n       n       -       -       pipe
   flags=FB user=ubuntu argv=tee -a /tmp/outgoing_mail.dump

```

Using mailhook to inspect messages
----------------------------------

[](#using-mailhook-to-inspect-messages)

Obviously you'd usually use mailhook inside a test framework of some kind (Behat, for example). But this very simple example should give you an idea of how you can use it:

```
$mailhook = new \Ingenerator\Mailhook\Mailhook('/tmp/outgoing_mail.dump');
// You'll usually want to purge the file before your tests, to ensure you have a clean state
$mailhook->purge();

run_my_code_that_should_send_emails();

$mailhook->refresh();

$mails = $mailhook->getEmails();
assert(count($mails) === 1, 'An email was sent');
```

### Getting more detail

[](#getting-more-detail)

You probably want to know more than just that an email with some content was sent to some user. For example, you might want to assert that an email was sent to a specific user. For this, you can use the matching/assertion framework built into the package:

```
$mail   = $mailhook->assert()->firstEmailMatching(new EmailSentToMatcher('test@ingenerator.com'));
$emails = $mailhook->assert()->emailsMatching(new EmailSentToMatcher('test@ingenerator.com'));
$mailhook->assert()->noEmailMatching(new AnyEmailMatcher);
```

These assertion methods throw an exception if they fail, or return the matching email(s) if they succeed. You can add your own custom criteria by implementing the EmailMatcher interface and providing an instance of the class.

You can pass multiple matchers to assert that an email matching all the criteria was sent. For example, if you were testing the common "password reset email" feature you could do something like:

```
$mailhook = new \Ingenerator\Mailhook\Mailhook('/tmp/outgoing_mail.dump');
$mailhook->purge();

submit_my_password_reset_form();

$mail = $mailhook->assert()->firstEmailMatching(
    new EmailSentToMatcher('test@ingenerator.com'),
    new EmailWithLinkMatcher('/reset/')
);
$links  = $mail->getLinksMatching('/reset/');

visit_page_and_reset_password($links[0]->getUrl());
```

Testing and developing
----------------------

[](#testing-and-developing)

mailhook has a suite of [PhpSpec](http://phpspec.net) specifications. Run them with `bin/phpspec run`. Contributions will only be accepted if they are accompanied by well structured specs. Installing with composer should get you everything you need to work on the project.

License
-------

[](#license)

mailhook is copyright 2012-2014 inGenerator Ltd and released under the [BSD license](LICENSE).

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance55

Moderate activity, may be stable

Popularity32

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 56.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~497 days

Recently: every ~388 days

Total

9

Last Release

300d ago

Major Versions

v0.2.1 → v1.0.02019-04-03

PHP version history (7 changes)v0.1.0PHP &gt;=5.3

v1.0.0PHP ^7.2

v1.1.0PHP ^7.4 || ~8.0.0

v1.2.0PHP ~8.0.0 || ~8.1.0

v1.2.1PHP ~8.0.0 || ~8.1.0 || ~8.2.0

v1.2.2PHP ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0

v1.3.0PHP ~8.2.0 || ~8.3.0 || ~8.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/f6ebcc608a86cec9a3ce7a4932994e2b89fabbfcdde69aff43e8139f34e07a5e?d=identicon)[acoulton](/maintainers/acoulton)

---

Top Contributors

[![craig410](https://avatars.githubusercontent.com/u/1156379?v=4)](https://github.com/craig410 "craig410 (29 commits)")[![acoulton](https://avatars.githubusercontent.com/u/416566?v=4)](https://github.com/acoulton "acoulton (22 commits)")

### Embed Badge

![Health badge](/badges/ingenerator-mailhook/health.svg)

```
[![Health](https://phpackages.com/badges/ingenerator-mailhook/health.svg)](https://phpackages.com/packages/ingenerator-mailhook)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
