germania-kg / mailer
Pimple Service Provider for email and SwiftMailer services.
1.3.2
2022-05-30 13:47 UTC
Requires
- php: ^5.6|^7.0|^8.0
- germania-kg/swiftmailer-callable: ^1.0
- pimple/pimple: ^3.0
- psr/log: ^1.0
- swiftmailer/swiftmailer: ^5.4|^6.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-12-16 12:37:37 UTC
README
Pimple Service Provider for email and SwiftMailer services.
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' => 'me@test.com' ]); $dic->register( $mailer_service_provider ); $config = $dic['Mailer.Config']; print_r($config);
(
[smtp] =>
[port] =>
[ssl] =>
[user] =>
[pass] =>
[from_name] => John Doe
[from_mail] => me@test.com
[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' => 'me@test.com' ]); return $new_config; });
Before registering with Pimple
$dic['Mailer.Config'] = function($dic) { return array( 'from_name' => 'John Doe', 'from_mail' => 'me@test.com' ); }; $dic->register( new MailerServiceProvider() );
Usage
Setup MailerServiceProvider:
$dic->register( new MailerServiceProvider([ 'from_name' => 'John Doe', 'from_mail' => 'me@test.com', 'to' => 'admin@test.com', '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>", "admin@test.com");
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