summaryrefslogtreecommitdiff
path: root/src/Mail/MailerServiceProvider.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-05 13:40:03 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-05 13:44:43 +0200
commit01e9c22695a3e495f07ab445750221af72e09fe4 (patch)
tree63c01a396d289f5f1e4d11259654e772300ff98c /src/Mail/MailerServiceProvider.php
parent9d34f371cb9c5ab0d60bd3158678b9cc9da6cc80 (diff)
Implemented mailing abstraction
Closes #434
Diffstat (limited to 'src/Mail/MailerServiceProvider.php')
-rw-r--r--src/Mail/MailerServiceProvider.php86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/Mail/MailerServiceProvider.php b/src/Mail/MailerServiceProvider.php
new file mode 100644
index 00000000..70cb0d30
--- /dev/null
+++ b/src/Mail/MailerServiceProvider.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Engelsystem\Mail;
+
+use Engelsystem\Config\Config;
+use Engelsystem\Container\ServiceProvider;
+use Engelsystem\Mail\Transport\LogTransport;
+use InvalidArgumentException;
+use Swift_Mailer as SwiftMailer;
+use Swift_SendmailTransport as SendmailTransport;
+use Swift_SmtpTransport as SmtpTransport;
+use Swift_Transport as Transport;
+
+class MailerServiceProvider extends ServiceProvider
+{
+ public function register()
+ {
+ /** @var Config $config */
+ $config = $this->app->get('config');
+ $mailConfig = $config->get('email');
+
+ $transport = $this->getTransport($mailConfig['driver'], $mailConfig);
+ $this->app->instance(Transport::class, $transport);
+ $this->app->instance('mailer.transport', $transport);
+
+ /** @var SwiftMailer $swiftMailer */
+ $swiftMailer = $this->app->make(SwiftMailer::class);
+ $this->app->instance(SwiftMailer::class, $swiftMailer);
+ $this->app->instance('mailer.swift', $swiftMailer);
+
+ /** @var Mailer $mailer */
+ $mailer = $this->app->make(EngelsystemMailer::class);
+ $mailer->setFromAddress($mailConfig['from']['address']);
+ if (!empty($mailConfig['from']['name'])) {
+ $mailer->setFromName($mailConfig['from']['name']);
+ }
+
+ $this->app->instance(EngelsystemMailer::class, $mailer);
+ $this->app->instance(Mailer::class, $mailer);
+ $this->app->instance('mailer', $mailer);
+ }
+
+ /**
+ * @param string $transport
+ * @param array $config
+ * @return Transport
+ */
+ protected function getTransport($transport, $config)
+ {
+ switch ($transport) {
+ case 'log':
+ return $this->app->make(LogTransport::class);
+ case 'mail':
+ case 'sendmail':
+ return $this->app->make(SendmailTransport::class, ['command' => $config['sendmail']]);
+ case 'smtp':
+ return $this->getSmtpTransport($config);
+ }
+
+ throw new InvalidArgumentException(sprintf('Mail driver "%s" not found', $transport));
+ }
+
+ /**
+ * @param array $config
+ * @return SmtpTransport
+ */
+ protected function getSmtpTransport(array $config)
+ {
+ /** @var SmtpTransport $transport */
+ $transport = $this->app->make(SmtpTransport::class, [
+ 'host' => $config['host'],
+ 'port' => $config['port'],
+ 'security' => $config['encryption'],
+ ]);
+
+ if ($config['username']) {
+ $transport->setUsername($config['username']);
+ }
+
+ if ($config['password']) {
+ $transport->setPassword($config['password']);
+ }
+
+ return $transport;
+ }
+}