Skip to content

Commit

Permalink
add tests and support eml files
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Mar 16, 2016
1 parent c9b20c2 commit 0734316
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 15 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/themsaid/laravel-mail-preview.svg?style=flat-square)](https://packagist.org/packages/themsaid/laravel-mail-preview)

This package introduces a new `preview` mail driver for laravel, when selected it will produce the output of the
sent emails and save it as HTML documents for you to check in a web browser.
This package introduces a new `preview` mail driver for laravel, when selected it will produce the content of the
sent mail and save it as .html & .eml documents.

## Installation

Expand Down Expand Up @@ -35,22 +35,28 @@ MAIL_DRIVER=preview

## How it works

On every email sent a HTML document will be generated in `storage/email-previews` with a name that includes the first receiver and the subject:
On every email sent a `.html` & a `.eml` documents will be generated in `storage/email-previews` with a name that includes the first receiver and the subject:

```
1457904864_jack_at_gmail_com_invoice_000234.html
1457904864_jack_at_gmail_com_invoice_000234.eml
```

When opened in a web browser you'll be able to see how your email will look like, however there might be some differences that varies from
an email client to another.
You can open the `.html` file in a web browser, or open the `.eml` file in your default email client to have a realistic look
at the final output.

### Preview in a web browser

When you open the `.html` file in a web browser you'll be able to see how your email will look like, however there might be
some differences that varies from an email client to another.

At the beginning of the generated file you'll find a HTML comment with all the message info:

```html
<!--
From:{"info@acme.com":"Acme HQ"},
to:{"jack@gmail.com":"Jack Black"},
reply-to:"info@acme.com",
reply-to:{"info@acme.com"},
cc:[{"finance@acme.com":"Acme Finance"}, {"management@acme.com":"Acme Management"}],
bcc:null,
subject:Invoice #000234
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
"php": ">=5.5.9",
"illuminate/support": "5.2.*",
"illuminate/mail": "5.2.*",
"illuminate/filesystem": "5.2.*"
"illuminate/filesystem": "5.2.*",
"mockery/mockery": "~0.9.2"
},
"require-dev": {
"phpunit/phpunit" : "4.*"
"phpunit/phpunit" : "4.*",
"orchestra/testbench": "~3.0"
},
"autoload": {
"psr-4": {
"Themsaid\\MailPreview\\": "src"
}
},
"classmap": [
"tests"
]
}
}
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
</php>
</phpunit>
29 changes: 23 additions & 6 deletions src/PreviewTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
$this->cleanOldPreviews();

$this->files->put(
$this->getEmailPreviewPath($message),
$this->getEmailPreviewContent($message)
$this->getPreviewFilePath($message).'.html',
$this->getHTMLPreviewContent($message)
);

$this->files->put(
$this->getPreviewFilePath($message).'.eml',
$this->getEMLPreviewContent($message)
);
}

Expand All @@ -68,29 +73,41 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null)
*
* @return string
*/
protected function getEmailPreviewPath(Swift_Mime_Message $message)
protected function getPreviewFilePath(Swift_Mime_Message $message)
{
$to = str_replace(['@', '.'], ['_at_', '_'], array_keys($message->getTo())[0]);

$subject = $message->getSubject();

return $this->previewPath.'/'.str_slug($message->getDate().'_'.$to.'_'.$subject, '_').'.html';
return $this->previewPath.'/'.str_slug($message->getDate().'_'.$to.'_'.$subject, '_');
}

/**
* Get the content of the email preview file.
* Get the HTML content for the preview file.
*
* @param \Swift_Mime_Message $message
*
* @return string
*/
protected function getEmailPreviewContent(Swift_Mime_Message $message)
protected function getHTMLPreviewContent(Swift_Mime_Message $message)
{
$messageInfo = $this->getMessageInfo($message);

return $messageInfo.$message->getBody();
}

/**
* Get the EML content for the preview file.
*
* @param \Swift_Mime_Message $message
*
* @return string
*/
protected function getEMLPreviewContent(Swift_Mime_Message $message)
{
return $message->toString();
}

/**
* Generate a human readable HTML comment with message info.
*
Expand Down
91 changes: 91 additions & 0 deletions tests/MailPreviewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

use Mockery as m;
use Themsaid\MailPreview\PreviewTransport;

class MailPreviewTest extends TestCase
{
public function testCreatesPreviewDirectory()
{
$message = new Swift_Message('Foo subject', '<html>Body</html>');
$message->setFrom('myself@example.com');
$message->setTo('me@example.com');
$files = m::mock('Illuminate\Filesystem\Filesystem');
$transport = new PreviewTransport(
$files,
'framework/emails'
);

$files->shouldReceive('exists')->once()->with('framework/emails')->andReturn(false);
$files->shouldReceive('makeDirectory')->once()->with('framework/emails');
$files->shouldReceive('put')->once()->with(
'framework/emails/.gitignore',
"*\n!.gitignore"
);

self::getMethod('createEmailPreviewDirectory')->invokeArgs($transport, []);
}

public function testCleansOldPreviews()
{
$message = new Swift_Message('Foo subject', '<html>Body</html>');
$message->setFrom('myself@example.com');
$message->setTo('me@example.com');
$files = m::mock('Illuminate\Filesystem\Filesystem');
$transport = new PreviewTransport(
$files,
'framework/emails',
60
);

$files->shouldReceive('files')->once()->with('framework/emails')->andReturn(['path/to/old/file', 'path/to/new/file']);
$files->shouldReceive('lastModified')->with('path/to/old/file')->andReturn(time() - 70);
$files->shouldReceive('lastModified')->with('path/to/new/file')->andReturn(time());
$files->shouldReceive('delete')->once()->with(['path/to/old/file']);

self::getMethod('cleanOldPreviews')->invokeArgs($transport, [$message]);
}

public function testCreatesPreviewFiles()
{
$message = new Swift_Message('Foo subject', '<html>Body</html>', 'text/html');
$message->setFrom('myself@example.com', 'Jack Black');
$message->setTo('me@example.com');
$files = m::mock('Illuminate\Filesystem\Filesystem');
$transport = new PreviewTransport(
$files,
'framework/emails'
);

$files->shouldReceive('exists')->once()->with('framework/emails')->andReturn(true);
$files->shouldReceive('files')->once()->with('framework/emails')->andReturn([]);

$files->shouldReceive('put')->with(
'framework/emails/'.$message->getDate().'_me_at_example_com_foo_subject.html',
m::any()
);

$files->shouldReceive('put')->with(
'framework/emails/'.$message->getDate().'_me_at_example_com_foo_subject.eml',
$message->toString()
);

$transport->send($message);
}

/**
* Gets a private method
*
* @param $name
*
* @return ReflectionMethod
*/
protected static function getMethod($name)
{
$class = new ReflectionClass(PreviewTransport::class);
$method = $class->getMethod($name);
$method->setAccessible(true);

return $method;
}
}
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

abstract class TestCase extends Orchestra\Testbench\TestCase
{
protected function getPackageProviders($app)
{
return ['Themsaid\MailPreview\MailPreviewServiceProvider'];
}
}

0 comments on commit 0734316

Please sign in to comment.