Skip to content

Commit fa1361d

Browse files
committed
Create Transport factory
1 parent 3a14bf3 commit fa1361d

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,25 @@ $mailer->personal($message2); // without spool
202202
$mailer->personal($message2, 1); // with spool
203203
$mailer->flush();
204204
205+
```
206+
207+
If you using native library transport, you can use `Ddrv\Mailer\TransportFactory`.
208+
209+
```php
210+
<?php
211+
212+
use Ddrv\Mailer\TransportFactory;
213+
214+
// smtp
215+
$transport = TransportFactory::make('smtp://user:[email protected]:465/?encryption=tls&domain=example.com&sender=user%40exapmle.com');
216+
217+
// sendmail
218+
$transport = TransportFactory::make('sendmail://localhost/?options=-i+-r+user%40example.com');
219+
220+
// file
221+
$transport = TransportFactory::make('file:////path/to/mail/files');
222+
223+
// fake
224+
$transport = TransportFactory::make('fake://localhost');
225+
226+
```

src/TransportFactory.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Ddrv\Mailer;
4+
5+
use Ddrv\Mailer\Transport\FakeTransport;
6+
use Ddrv\Mailer\Transport\FileTransport;
7+
use Ddrv\Mailer\Transport\SendmailTransport;
8+
use Ddrv\Mailer\Transport\SmtpTransport;
9+
use InvalidArgumentException;
10+
11+
final class TransportFactory
12+
{
13+
14+
/**
15+
* @param string $transportUrl
16+
* @return TransportInterface
17+
* @throws InvalidArgumentException
18+
*/
19+
public static function make($transportUrl)
20+
{
21+
$transportUrl = preg_replace('#^(file):///#', '$1://localhost/', $transportUrl);
22+
$url = array_fill_keys(array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query'), '');
23+
$url = array_replace($url, parse_url($transportUrl));
24+
$path = $url['path'];
25+
parse_str(array_key_exists('query', $url) ? $url['query'] : '', $query);
26+
$sender = array_key_exists('sender', $query) ? $query['sender'] : $url['user'];
27+
switch ($url['scheme']) {
28+
case 'smtp':
29+
$host = (string)$url['host'];
30+
$port = (int)$url['port'];
31+
$user = urldecode($url['user']);
32+
$password = urldecode($url['pass']);
33+
$defaultEncryption = SmtpTransport::ENCRYPTION_TLS;
34+
$encryption = array_key_exists('encryption', $query) ? $query['encryption'] : $defaultEncryption;
35+
$domain = array_key_exists('domain', $query) ? $query['domain'] : '';
36+
$transport = new SmtpTransport($host, $port, $user, $password, $sender, $encryption, $domain);
37+
break;
38+
case 'sendmail':
39+
$options = array_key_exists('options', $query) ? $query['options'] : null;
40+
$transport = new SendmailTransport($options);
41+
break;
42+
case 'file':
43+
if ($path) {
44+
$path = str_replace('/', DIRECTORY_SEPARATOR, $path);
45+
$path = mb_substr($path, 1);
46+
}
47+
$transport = new FileTransport($path);
48+
break;
49+
case 'fake':
50+
$transport = new FakeTransport();
51+
break;
52+
default:
53+
$transport = new FakeTransport();
54+
}
55+
return $transport;
56+
}
57+
}

0 commit comments

Comments
 (0)