silverstripe-framework/docs/en/02_Developer_Guides/06_Testing/How_Tos/03_Testing_Email.md
Michael Pritchard fdbd899766 DOC Update SilverStripe to Silverstripe CMS
- Remaining Developer Guides and Upgrading
- SilverStripe in a namespace or api has not been change
- To keep PRs easier no formatting was changed

Update merge conflics with two files

Update Silverstripe Ltd, Silverstripe Cloud and Silverstripe CMS

Silverstripe CMS Ltd > Silverstripe Ltd
Silverstripe CMS Platform > Silverstripe Cloud
Silverstripe CMS Framework > Silverstripe CMS

Resolve merge conflict

Remove Framework from Silverstripe CMS Framework

- 3 files

Change SilverStripe CMS to Silverstripe CMS
2021-07-30 13:54:15 +01:00

1.4 KiB

title summary icon
How to test emails within unit tests Test email functionality without ever hitting an inbox envelope

Testing Email within Unit Tests

Silverstripe CMS's test system has built-in support for testing emails sent using the Email class. If you are running a SapphireTest test, then it holds off actually sending the email, and instead lets you assert that an email was sent using this method.

use SilverStripe\Control\Email\Email;

public function MyMethod() 
{
    $e = new Email();
    $e->To = "someone@example.com";
    $e->Subject = "Hi there";
    $e->Body = "I just really wanted to email you and say hi.";
    $e->send();
}

To test that MyMethod sends the correct email, use the SapphireTest::assertEmailSent() method.

$this->assertEmailSent($to, $from, $subject, $body);

// to assert that the email is sent to the correct person
$this->assertEmailSent("someone@example.com", null, "/th.*e$/");

Each of the arguments ($to, $from, $subject and $body) can be either one of the following.

  • A string: match exactly that string
  • null/false: match anything
  • A PERL regular expression (starting with '/')

API Documentation