A testing package for intercepting mail sent from Laravel

Related tags

Email mail-intercept
Overview

Mail Intercept banner

Laravel Mail Intercept

A testing package for intercepting mail sent from Laravel

Latest Version on Packagist Total Downloads Codacy Badge Actions Status

This testing suite intercepts Laravel Mail just before they are sent out, allowing all kinds of assertions to be made on the actual emails themselves.

Mail isn't faked here. You get to inspect the actual mail ensuring you are sending exactly what you want!

Requirements

This testing package requires Laravel 5.5 or higher.

Installation

composer require kirschbaum-development/mail-intercept --dev

Usage

Next you can use the KirschbaumDevelopment\MailIntercept\WithMailInterceptor trait in your test class:

namespace Tests;

use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Testing\WithFaker;
use KirschbaumDevelopment\MailIntercept\WithMailInterceptor;

class MailTest extends TestCase
{
    use WithFaker,
        WithMailInterceptor;

    public function testMail()
    {
        $this->interceptMail();

        $email = $this->faker->email;
        
        Mail::to($email)->send(new TestMail());

        $interceptedMail = $this->interceptedMail()->first();

        $this->assertMailSentTo($email, $interceptedMail);
    }
}

That's it! Pretty simple, right?!

Testing API

$this->interceptMail()

This method MUST be called first, similar to how Mail::fake() works. But unlike the mail fake, mail is not faked, it is intercepted.

$this->interceptedMail()

This should be called after Mail has been sent, but before your assertions, otherwise you won't have any emails to work with. It returns a Collection of emails so you are free to use any of the methods available to a collection.

Assertions Parameters
$this->assertMailSentTo($to, $mail); $to string, array
$mail Swift_Message
$this->assertMailNotSentTo($to, $mail); $to string, array
$mail Swift_Message
$this->assertMailSentFrom($from, $mail); $from string, array
$mail Swift_Message
$this->assertMailNotSentFrom($from, $mail); $from string, array
$mail Swift_Message
$this->assertMailSubject($subject, $mail); $subject string
$mail Swift_Message
$this->assertMailNotSubject($subject, $mail); $subject string
$mail Swift_Message
$this->assertMailBodyContainsString($content, $mail); $content string
$mail Swift_Message
$this->assertMailBodyNotContainsString($content, $mail); $content string
$mail Swift_Message
$this->assertMailRepliesTo($reply, $mail); $reply string, array
$mail Swift_Message
$this->assertMailNotRepliesTo($reply, $mail); $reply string, array
$mail Swift_Message
$this->assertMailCc($cc, $mail); $cc string, array
$mail Swift_Message
$this->assertMailNotCc($cc, $mail); $cc string, array
$mail Swift_Message
$this->assertMailBcc($cc, $mail); $bcc string, array
$mail Swift_Message
$this->assertMailNotBcc($cc, $mail); $bcc string, array
$mail Swift_Message
$this->assertMailSender($sender, $mail); $sender string, array
$mail Swift_Message
$this->assertMailNotSender($sender, $mail); $sender string, array
$mail Swift_Message
$this->assertMailIsPlain($mail); $mail Swift_Message
$this->assertMailIsNotPlain($mail); $mail Swift_Message
$this->assertMailIsHtml($mail); $mail Swift_Message
$this->assertMailIsNotHtml($mail); $mail Swift_Message
Header Assertions Parameters
$this->assertMailHasHeader($header, $mail); $header string
$mail Swift_Message
$this->assertMailMissingHeader($header, $mail); $header string
$mail Swift_Message
$this->assertMailHeaderIs($header, $value, $mail); $header string
$value string
$mail Swift_Message
$this->assertMailHeaderIsNot($header, $value, $mail); $header string
$value string
$mail Swift_Message

You should use each item of the interceptedMail() collection as the mail object for all assertions.

If you are injecting your own headers or need access to other headers in the email, use this assertion to verify they exist and are set properly. These assertions require the header name and the compiled email.

Other assertions

Since $this->interceptedMail() returns a collection of Swift_Message objects, you are free to dissect and look into those objects using any methods available to Swift's Message API. Head over to the Swift Mail Docs for more detailed info.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] or [email protected] instead of using the issue tracker.

Credits

Sponsorship

Development of this package is sponsored by Kirschbaum Development Group, a developer driven company focused on problem solving, team building, and community. Learn more about us or join us!

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Add AssertableMessage to method signatures

    Add AssertableMessage to method signatures

    The assertMailBodyContainsString and assertMailBodyNotContainsString assertions don't currently work with the documented use, because interceptedMail() returns an array of AssertableMessage objects. This just adds the AssertableMessage type to the method signatures.

    opened by amsoell 2
  • AssertMailSubject, AssertMailBodyContainsString fail with latest release

    AssertMailSubject, AssertMailBodyContainsString fail with latest release

    The functions for assertMailSentSubject, and assertMailBodyContainsStr need to have union types similar to assertMailSentTo

    Works: public function assertMailSentTo(array|string $expected, AssertableMessage|Email $mail)

    Fails: public function assertMailSubject(string $expected, Email $mail) Fails: public function assertMailBodyContainsString(string $needle, Email $mail)

    Note: I think most of the assertions need the union type.

    Thanks for looking into this.

    Error message: Tests\TestCase::assertMailSubject(): Argument #2 ($mail) must be of type Symfony\Component\Mime\Email, KirschbaumDevelopment\MailIntercept\AssertableMessage given

    opened by ronfuller 2
  • Use HTML body when email is HTML for body assertions

    Use HTML body when email is HTML for body assertions

    $mail->getBody()->bodyToString() includes line break and carriage return characters injected at unpredictable areas within the email message, making the assertMailBodyContainsString assertion unreliable and in the case of finding strings over a certain length, return false every time.

    Screen Shot 2022-06-14 at 7 17 22 AM

    This PR uses the $mail->getHtmlBody() method if it is an HTML email and falls back to $mail->getBody()->bodyToString() in case it is a plain text email. This method does not output line breaks, making assertions against body text far more reliable.

    Screen Shot 2022-06-14 at 7 16 47 AM

    bug 
    opened by therobfonz 0
  • Assertable message updates

    Assertable message updates

    This adds some better coverage in the assertions for type-hinting both Email and AssertableMessage classes. Thanks to @amsoell for pointing this out and creating and initial PR (#10) for this fix.

    opened by brandonferens 0
  • Laravel 9 updates and many new assertions

    Laravel 9 updates and many new assertions

    • Upgraded for Laravel 9
    • Added new assertions methods:
      • assertMailHasPlainContent
      • assertMailDoesNotHavePlainContent
      • assertMailHasHtmlContent
      • assertMailDoesNotHaveHtmlContent
      • assertMailIsAlternative
      • assertMailIsNotAlternative
      • assertMailIsMixed
      • assertMailIsNotMixed
      • assertMailPriority
      • assertMailNotPriority
      • assertMailPriorityIsHighest
      • assertMailPriorityNotHighest
      • assertMailPriorityIsHigh
      • assertMailPriorityNotHigh
      • assertMailPriorityIsNormal
      • assertMailPriorityNotNormal
      • assertMailPriorityIsLow
      • assertMailPriorityNotLow
      • assertMailPriorityIsLowest
      • assertMailPriorityNotLowest
      • assertMailReturnPath
      • assertMailNotReturnPath
    opened by brandonferens 0
  • fixes TransportManager changes in Laravel 7

    fixes TransportManager changes in Laravel 7

    Prior to Laravel 7, app('swift.transport') actually returns a TransportManager object, (calling ->driver() on that gives you the ArrayTransport that matches the new retrieval through app('mailer')->...)

    opened by michaelfox 0
  • Fixes retrieving the swift mail transport from the app container in Laravel 7

    Fixes retrieving the swift mail transport from the app container in Laravel 7

    Laravel 7 removes the swift.transport binding from the app container. Upgrade guide:

    Laravel 7.x doesn't provide swift.mailer and swift.transport container bindings. You may now access these objects through the mailer binding:

    $swiftMailer = app('mailer')->getSwiftMailer();
    $swiftTransport = $swiftMailer->getTransport();
    
    opened by michaelfox 0
Releases(v0.3.2)
Owner
Kirschbaum Development Group, LLC
We are a team of carefully curated Laravel experts with a history of delivering practical and efficient solutions to complex problems.
Kirschbaum Development Group, LLC
A mail driver to quickly preview mail

A mail driver to quickly preview mail This package can display a small overlay whenever a mail is sent. The overlay contains a link to the mail that w

Spatie 1k Jan 4, 2023
Laravel mailer which will catch all the sent emails and show them on an application view.

Laravel Web Mailer This package contains a web mailer which will catch all the sent emails. Then, you can view it visiting the route /web-inbox. The e

Creagia 54 Dec 16, 2022
Mail Web is a Laravel package which catches emails locally for debugging

Mail Web is a Laravel package which catches emails locally for debugging Installation Use the package manager composer to install Mail Web. composer r

Appoly 64 Dec 24, 2022
Laravel Mail Credentials switcher for Budget Laravel Applications

Laravel Mail Switcher Laravel Mail Credentials Switcher is a library which helps you to: Manage your Mail Service Credentials Configure the Laravel's

(Seth) Phat Tran 34 Dec 24, 2022
Laravel Mail Catcher Driver

Laravel Mail Catcher Driver This package include a new mailbase driver which will catch all the sent emails and save it to the database. It then expos

Tauqeer Liaqat 94 Aug 30, 2022
✉️ Laravel Mail Explorer

Mailbook Mailbook is a Laravel package that lets you easily inspect your mails without having to actually trigger it in your application. View demo In

Max Hoogenbosch 88 Dec 29, 2022
Mandrill mail driver for Laravel for version 6+

Laravel Mandrill Driver This package re-enables Mandrill driver functionality using the Mail facade in Laravel 6+. To install the package in your proj

Eng Hasan Hajjar 2 Sep 30, 2022
Bounce Mail Handler for PHP | This is a "reboot" of PHPMailer-BMH from WorxWare.

PHP 7.0+ Support Composer & PSR-0 Support PHPUnit testing via Travis CI (TODO: more tests needed) PHP-Quality testing via SensioLabsInsight (TODO: mor

Lars Moelleken 43 Jan 7, 2023
:envelope: E-Mail Address Validator (syntax, dns, trash, typo)

✉️ E-Mail Address Validator for PHP Warning The best way to validate an e-mail address is still to send a duplicate opt-in-mail, when the user clicks

Lars Moelleken 41 Dec 25, 2022
Mail sending module for Mezzio and Laminas MVC with support for file attachment and template email composition

This module provides an easy and flexible way to send emails from Mezzio and Laminas MVC applications (formerly known as Zend Expressive and Zend MVC). It allows you to pre-configure emails and transports, and then send those emails at runtime.

null 82 Jan 16, 2022
PostfixAdmin - web based virtual user administration interface for Postfix mail servers

PostfixAdmin An open source, web based interface for managing domains/mailboxes/aliases etc on a Postfix based mail server.

PostfixAdmin 755 Jan 3, 2023
Provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice

WP PHPMailer provides a clean and simple way to configure the WordPress-bundled PHPMailer library, allowing you to quickly get started sending mail through a local or cloud based service of your choice.

Itineris Limited 61 Dec 6, 2022
💌 Mail app for Nextcloud

Nextcloud Mail ?? A mail app for Nextcloud Why is this so awesome? ?? Integration with other Nextcloud apps! Currently Contacts, Calendar & Files – mo

Nextcloud 684 Dec 26, 2022
Crud PHP 8 com Form E-mail

Crud com PHP 8 PDO Login - Cadastro de Usuários - Edição - Deleção - Adição | Formulário envio de e-mail Para rodar o Crud é preciso instalar um servi

Isaias Oliveira 4 Nov 16, 2021
Simple mail sending by PHPMailer and Create your local system.

Simple mail sending by PHPMailer and Create your local system. Send mail zero of cost and also send Attachment like Photo, pdf and multiple files. You should be create a login and verify two steps authentication like OTP, verifications ?? link. PHPMailer make your dreams project eassy and simple also free of cost.

SUSHIL KUMBHAR 2 Dec 8, 2021
Mail application developed with Symfony 5

Fakey-Mail Mail application developed with Symfony 5! Check out the requirements for info on how to launch the app. Check out the basic functionality

Mauro 2 Jun 21, 2022
Mail Api for fetch or send mails

flux-mail-api Mail Api for fetch or send mails Installation Native Download RUN (mkdir -p /%path%/libs/flux-mail-api && cd /%path%/libs/flux-mail-api

null 2 Dec 12, 2022
This package adds support for verifying new email addresses: when a user updates its email address, it won't replace the old one until the new one is verified.

Laravel Verify New Email Laravel supports verifying email addresses out of the box. This package adds support for verifying new email addresses. When

Protone Media 300 Dec 30, 2022
Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app. This enables your app to not only respond to new emails but also allows it to read and parse existing mails and much more.

null 530 Jan 6, 2023