Skip to content

Latest commit

 

History

History
210 lines (143 loc) · 4.47 KB

README.md

File metadata and controls

210 lines (143 loc) · 4.47 KB

Germania KG · Mailer

Pimple Service Provider for email and SwiftMailer services.

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation

$ composer require germania-kg/mailer

Registering

<?php
use Germania\Mailer\MailerServiceProvider;

$dic = new \Pimple\Container;
$dic->register( new MailerServiceProvider );

Configuration

The configuration is an array with typical mail configration fields, each of them empty.

You can retrieve the configuration from the Pimple container like this:

$dic->register( new MailerServiceProvider );
$config = $dic['Mailer.Config'];
print_r($config);
(
    [smtp] =>
    [port] =>
    [ssl] =>
    [user] => 
    [pass] =>
    [from_name] =>
    [from_mail] =>
    [to] =>
    [subject] =>
)

Configure on Instantiation

Pass a custom configuration to the MailerServiceProvider constructor. Register it to Pimple container as usual.

$mailer_service_provider = new MailerServiceProvider([
  'from_name' => 'John Doe',
  'from_mail' => '[email protected]'
]);

$dic->register( $mailer_service_provider );
$config = $dic['Mailer.Config'];
print_r($config);
(
    [smtp] =>
    [port] =>
    [ssl] => 
    [user] => 
    [pass] =>
    [from_name] => John Doe
    [from_mail] => [email protected]
    [to] =>
    [subject] =>
)

Configure at Runtime

Two ways:

  • After registering the ServiceProvider to Pimple (i.e. extending the Mailer.Config service definition)
  • Before registering, pre-defining the configuration in a Mailer.Config service definition

After registering with Pimple

$dic->register( new MailerServiceProvider() );
$dic->extend('Mailer.Config', function($default_config, $dic) {
  $new_config = array_merge($default_config, [
    'from_name' => 'John Doe',
    'from_mail' => '[email protected]'
  ]);
  return $new_config;
});

Before registering with Pimple

$dic['Mailer.Config'] = function($dic) {
  return array(
    'from_name' => 'John Doe',
    'from_mail' => '[email protected]'
  );
};

$dic->register( new MailerServiceProvider() );

Usage

Setup MailerServiceProvider:

$dic->register( new MailerServiceProvider([
    'from_name' => 'John Doe',
    'from_mail' => '[email protected]',
    'to'        => '[email protected]',
  	'subject'   => 'Default subject'
]));

SwiftMailerCallable

<?php
use Germania\SwiftMailerCallable\SwiftMailerCallable;

// Grab the Mailer callable
$mailer = $dic[SwiftMailerCallable::class];
// DEPRECATED service name: 
$mailer = $dic['Mailer.Callable'];

// Send with subject and mail body
$result = $mailer("The subject", "<p>The mailtext</p>");

# Override receipient address
# previosuly set in Mailer.Config
$result = $mailer("The subject", "<p>The mailtext</p>", "[email protected]");

SwiftMailer

See https://swiftmailer.symfony.com/docs/introduction.html

<?php
use Germania\Mailer\SwiftMessageFactory;
use \Swift_Mailer;

// Grab Swiftmailer
$swift_mailer = $dic[Swift_Mailer::class];  

// Setup message
$message_factory = $dic[SwiftMessageFactory::class];
$message = $message_factory->createMessage();
$message->setBody("This is the mail body");

// This optional as we set it with Configuration
$message->setSubject( "Custom subject line" );

// Send message
$result = $swift_mailer->send( $message ));

Development

$ git clone https://github.com/GermaniaKG/Mailer.git
$ cd Mailer
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit