2017 © Pedro Peláez
 

library templated-emailer

Templated emailer

image

silktide/templated-emailer

Templated emailer

  • Monday, November 6, 2017
  • by andywaite
  • Repository
  • 3 Watchers
  • 0 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Build Status Code Climate Test Coverage, (*1)

Templated emailer

This library simply ties together Symfony's powerful templating engine with the flexibility of Swiftmailer, allowing you to send templated emails easily., (*2)

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:, (*3)

$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)., (*4)

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 as the second argument if you want to use a different transport, e.g. SMTP. Here's an example of using an alternative transport:, (*5)

$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:, (*6)

/**
* @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. Here's a very basic example:, (*7)

<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)., (*8)

Usage

Before any emails can be sent, you must set a sender:, (*9)

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

Sending an email now just requires a recipient, subject, template filename and context:, (*10)

$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']:, (*11)

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

The Versions

06/11 2017

dev-master

9999999-dev

Templated emailer

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

by Andrew Waite

10/09 2015

0.1.1

0.1.1.0

Templated emailer

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

by Andrew Waite

10/09 2015

0.1.0

0.1.0.0

Templated emailer

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

by Andrew Waite