Skip to content

yasasn86/mailcatcher

 
 

Repository files navigation

MailCatcher for PHP

Build status

STILL IN DEVELOPMENT

Integrates MailCatcher in your PHP application. MailCatcher is a simple SMTP server with an HTTP API, and this library aims to integrate it to make it easy to use it with PHP.

Behat extension

This library provides a Behat extension to help you test mails in your application.

To use it, you first need to be sure MailCatcher is properly installed and running.

First, configure in your behat.yml:

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

Then, add the MailCatcherContext context in your FeatureContext file:

<?php

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

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

Available steps

This extension provides you mail context in your tests. To use assertions, you must first open a mail using criterias.

Once it's opened, you can make assertions on it and click in it.

Server manipulation

Deletes all messages on the server

When I purge mails

Mail opening

When I open mail from "[email protected]"

When I open mail containing "a message"

When I open mail to "[email protected]"

When I open mail with subject "Welcome, mister Bond!"

Assertion

Verify number of messages sent to the server:

Then 1 mail should be sent

Then 13 mails should be sent

Verify text presence in message:

Then I should see "something" in mail

Then I should see "something else" in mail

Client API

Browse easily your API with the integrated SDK:

$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'        => '[email protected]',
    'to'          => '[email protected]',
    'subject'     => 'Bla',
    'contains'    => 'Hello',
    'attachments' => true,
    'format'      => 'html',
), $limit = 3);

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

Message 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();

Attachment API

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

About

A library to manipulate Mailcatcher API

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%