Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Add support for adding and inspecting custom mail headers (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic committed Sep 8, 2016
1 parent ed0301a commit 0f2dd08
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion readme.md
Expand Up @@ -26,6 +26,7 @@ Route::post('register', function () {
$m->subject('Welcome to my app!');
$m->from('noreply@example.com');
$m->bcc('notifications@example.com');
$m->getHeaders()->addTextHeader('X-MailThief-Variables', 'mailthief');
});

// <snip> Return response </snip>
Expand Down Expand Up @@ -62,9 +63,14 @@ class RegistrationTest extends TestCase
$this->seeMessageWithSubject('Welcome to my app!');

// Make sure the email was sent from the correct address
// (`from` can be a list, so we return it as a collection)
$this->seeMessageFrom('noreply@example.com');

// Make sure a given header is set on an email
$this->seeHeaders('X-MailThief-Variables');

// Make sure the header is set to a given value
$this->seeHeaders('X-MailThief-Variables', 'mailthief');

// Make sure the email contains text in the body of the message
// Default is to search the html rendered view
$this->assertTrue($this->lastMessage()->contains('Some text in the message'));
Expand Down
9 changes: 9 additions & 0 deletions src/Message.php
Expand Up @@ -19,13 +19,15 @@ class Message
public $reply_to;
public $priority;
public $attachments;
public $headers;
public $delay = 0;
/**
* Methods that are available in Laravel but not provided by MailThief
* @var array
*/
public $valid_methods = [
'addPart',
'getHeaders',
'setReadReceiptTo',
'setCharset',
'setMaxLineLength',
Expand All @@ -41,6 +43,7 @@ public function __construct($view, $data)
$this->bcc = collect();
$this->reply_to = collect();
$this->attachments = collect();
$this->headers = collect();
}

public function __call($name, $arguments)
Expand Down Expand Up @@ -172,6 +175,12 @@ public function attachData($data, $name, array $options = [])
return $this;
}

public function addTextHeader($name, $value = null)
{
$this->headers[] = ['name' => $name, 'value' => $value];
return $this;
}

public function getSwiftMessage()
{
throw new Exception("Cannot get Swift message from MailThief message.");
Expand Down
13 changes: 13 additions & 0 deletions src/Testing/InteractsWithMail.php
Expand Up @@ -80,6 +80,19 @@ public function lastMessage()
return $this->getMailer()->lastMessage();
}

public function seeHeaders($name, $value = null)
{
$this->assertTrue($this->lastMessage()->headers->contains(function ($header) use ($name, $value) {
if (is_null($value)) {
return $header['name'] === $name;
}

return $header['name'] === $name && $header['value'] === $value;
}));

return $this;
}

protected function seeMessage()
{
$this->assertNotNull(
Expand Down
14 changes: 14 additions & 0 deletions tests/InteractsWithMailTest.php
Expand Up @@ -19,6 +19,7 @@ public function render()
}
};
});

return $factory;
}

Expand Down Expand Up @@ -83,4 +84,17 @@ public function test_see_message_from()

$this->seeMessageFrom('me@example.com');
}

public function test_see_headers_for()
{
$mailer = $this->mailer = $this->getMailThief();

$mailer->send('example-view', [], function ($m) {
$m->to('john@example.com');
$m->getHeaders()->addTextHeader('X-MailThief-Variables', json_encode(['mailthief_id' => 2]));
});

$this->seeHeaders('X-MailThief-Variables');
$this->seeHeaders('X-MailThief-Variables', json_encode(['mailthief_id' => 2]));
}
}

0 comments on commit 0f2dd08

Please sign in to comment.