Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Factory for creating templatedEmailer and README.
Browse files Browse the repository at this point in the history
  • Loading branch information
andywaite committed Sep 10, 2015
1 parent 753af28 commit 143c48e
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: php
php:
- 5.5

install: composer install
script: phpunit --coverage-clover build/logs/clover.xml
after_script:
- CODECLIMATE_REPO_TOKEN="994d49d26b49bc06c85c32c84ece534c44d0bad8ae4b86b54ff87172de256788" vendor/bin/test-reporter --stdout > codeclimate.json
80 changes: 78 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,78 @@
# templated-emailer
Templated emailer
# Templated emailer
This library simply ties together Symfony's powerful templating engine with the flexibility of Swiftmailer, allowing you to send templated emails easily.

## Setup

### Without DI

If you're not using DI (or don't know what DI is) there is a helpful factory to create the mailer:

$emailer = \Silktide\TemplatedEmailer\TemplatedEmailerFactory::create('/path/to/templates');

The only required argument is the path to the folder containing your email templates (see Creating a template below).

By default, the factory will set up the class to use PHP's mail() function to send your mail and Symfony's PhpEngine for templating.
You can optionally provide a [Swiftmailer transport class](http://swiftmailer.org/docs/sending.html) as the second argument
if you want to use a different transport, e.g. SMTP. Here's an example of using an alternative transport:

$transport = \Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password');
$emailer = \Silktide\TemplatedEmailer\TemplatedEmailerFactory::create('/example', $transport);


### Dependency injection / manual instantiation

The TemplatedEmailer class just requires two arguments, a Symfony template engine and an instance of Swift_Mailer:

/**
* @var \Symfony\Component\Templating\EngineInterface
*/
$templateEngine;

/**
* @var \Swift_Mailer
*/
$emailClient;

$emailer = new \Silktide\TemplatedEmailer\TemplatedEmailer($templateEngine, $emailClient);

## Creating a template

Templates are using [Symfony's well documented template library](http://symfony.com/doc/current/components/templating/introduction.html). Here's a very basic example:

<h1>An email from myApp</h1>
<p>Dear <?php echo $recipientName; ?></p>
<p><?php echo $message; ?>

The variables in the message are passed through as an array to the send() method (see usage below).

## Usage

Before any emails can be sent, you must set a sender:

$emailer->setSender('pat@postman.com', 'Postman Pat');

Sending an email now just requires a recipient, subject, template filename and context:

$this->mailer->send(
'mrsgoggins@greendalepo.com',
'Jess the black and white cat',
'anEmail.php',
[
'recipientName' => 'Mrs Goggins',
'message' => 'Hello!'
]
);

The recipient can also have a name set by using an array in the format ['email@host.tld' => 'Friendly Name']:

$this->mailer->send(
['mrsgoggins@greendalepo.com' => 'Mrs Goggins'],
'Jess the black and white cat',
'anEmail.php',
[
'recipientName' => 'Mrs Goggins',
'message' => 'Hello!'
]
);
42 changes: 42 additions & 0 deletions src/TemplatedEmailerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright 2013-2015 Silktide Ltd. All Rights Reserved.
*/

namespace Silktide\TemplatedEmailer;


use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\TemplateNameParser;
use Swift_MailTransport;
use Swift_Transport;
use Swift_Mailer;

abstract class TemplatedEmailerFactory
{
/**
* For non DI use, create a TemplatedEmailer
*
* @param string $basePath
* @param Swift_Transport|null $transport
* @return TemplatedEmailer
*/
public static function create($basePath, Swift_Transport $transport = null)
{
// Use PHP's default SendMail if no transport is provided
if ($transport == null) {
$transport = Swift_MailTransport::newInstance();
}

// Create a mailer with our transport
$mailer = Swift_Mailer::newInstance($transport);

// Create templating system
$loader = new FilesystemLoader($basePath);
$engine = new PhpEngine(new TemplateNameParser(), $loader);

// Create our instance
return new TemplatedEmailer($engine, $mailer);
}
}
40 changes: 40 additions & 0 deletions test/TemplatedEmailerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright 2013-2015 Silktide Ltd. All Rights Reserved.
*/

namespace Silktide\TemplatedEmailer\Test;

use Silktide\TemplatedEmailer\TemplatedEmailer;
use Silktide\TemplatedEmailer\TemplatedEmailerFactory;
use Symfony\Component\Templating\EngineInterface;
use Swift_Mime_SimpleMessage;
use Swift_Mailer;
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_MockObject_MockObject;
use Swift_SmtpTransport;

class TemplatedEmailerFactoryTest extends PHPUnit_Framework_TestCase
{
/**
* Should return an instance of TemplatedEmailer
*/
public function testCanCreateInstanceUsingFactory()
{
$emailer = TemplatedEmailerFactory::create('/example');
$this->assertInstanceOf('Silktide\TemplatedEmailer\TemplatedEmailer', $emailer);
}

/**
* Should allow use of your own transport
*/
public function testCanCreateInstanceWithCustomTransport()
{
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
$emailer = TemplatedEmailerFactory::create('/example', $transport);
$this->assertInstanceOf('Silktide\TemplatedEmailer\TemplatedEmailer', $emailer);
}
}
2 changes: 1 addition & 1 deletion test/TemplatedMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TemplatedEmailerTest extends PHPUnit_Framework_TestCase
/**
* @var string
*/
protected $senderEmail = 'mrsgoggins@greendalepostoffice.com';
protected $senderEmail = 'mrsgoggins@greendalepo.com';

/**
* @var string
Expand Down

0 comments on commit 143c48e

Please sign in to comment.