Pimple Service Provider for email and SwiftMailer services.
$ composer require germania-kg/mailer
<?php
use Germania\Mailer\MailerServiceProvider;
$dic = new \Pimple\Container;
$dic->register( new MailerServiceProvider );
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] =>
)
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] =>
)
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
$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;
});
$dic['Mailer.Config'] = function($dic) {
return array(
'from_name' => 'John Doe',
'from_mail' => '[email protected]'
);
};
$dic->register( new MailerServiceProvider() );
Setup MailerServiceProvider:
$dic->register( new MailerServiceProvider([
'from_name' => 'John Doe',
'from_mail' => '[email protected]',
'to' => '[email protected]',
'subject' => 'Default subject'
]));
<?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]");
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 ));
$ git clone https://github.com/GermaniaKG/Mailer.git
$ cd Mailer
$ composer install
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