1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Da\Mailer; |
4
|
|
|
|
5
|
|
|
use Da\Mailer\Builder\MessageBuilder; |
6
|
|
|
use Da\Mailer\Helper\PhpViewFileHelper; |
7
|
|
|
use Da\Mailer\Model\MailMessage; |
8
|
|
|
use Da\Mailer\Transport\TransportFactory; |
9
|
|
|
use Da\Mailer\Transport\TransportInterface; |
10
|
|
|
use Symfony\Component\Mailer\SentMessage; |
11
|
|
|
|
12
|
|
|
class Mailer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var TransportInterface|null the transport used to send emails. |
16
|
|
|
*/ |
17
|
|
|
private $transport = null; |
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $logging = true; |
22
|
|
|
/** |
23
|
|
|
* Constructor. |
24
|
|
|
* |
25
|
|
|
* @param TransportInterface $transport the transport to use for sending emails. |
26
|
|
|
* @param bool $logging |
27
|
|
|
*/ |
28
|
|
|
public function __construct(TransportInterface $transport, $logging = true) |
29
|
|
|
{ |
30
|
|
|
$this->transport = $transport; |
31
|
|
|
$this->logging = $logging; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the mail transport used. |
36
|
|
|
* |
37
|
|
|
* @return null|TransportInterface |
38
|
|
|
*/ |
39
|
|
|
public function getTransport(): TransportInterface |
40
|
|
|
{ |
41
|
|
|
return $this->transport; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the Symfony Mailer Transport instance. |
46
|
|
|
* |
47
|
|
|
* @return null|\Symfony\Component\Mailer\Transport\TransportInterface |
48
|
|
|
*/ |
49
|
3 |
|
public function getTransportInstance() |
50
|
|
|
{ |
51
|
3 |
|
return $this->getTransport()->getInstance(); |
52
|
3 |
|
} |
53
|
3 |
|
|
54
|
3 |
|
/** |
55
|
|
|
* @return null|string logged messages as string |
56
|
|
|
*/ |
57
|
|
|
public function getLog() |
58
|
|
|
{ |
59
|
|
|
// TODO Add log mechanism |
60
|
|
|
return null; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
3 |
|
/** |
64
|
|
|
* Modifies the transport used. |
65
|
|
|
* |
66
|
|
|
* @param \Symfony\Component\Mailer\Transport\TransportInterface $transport |
67
|
|
|
*/ |
68
|
|
|
public function setTransport(TransportInterface $transport) |
69
|
|
|
{ |
70
|
|
|
$this->transport = $transport; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
2 |
|
/** |
74
|
2 |
|
* Sends a MailMessage instance. |
75
|
2 |
|
* |
76
|
2 |
|
* View files can be added to the `$views` array argument and if set, they will be parsed via the `PhpViewFileHelper` |
77
|
|
|
* helper that this library contains. |
78
|
2 |
|
* |
79
|
|
|
* The `$view` argument has the following syntax: |
80
|
|
|
* |
81
|
|
|
* ``` |
82
|
|
|
* $view = [ |
83
|
|
|
* 'text' => '/path/to/plain/text/email.php', |
84
|
1 |
|
* 'html' => '/path/to/html/email.php' |
85
|
|
|
* ]; |
86
|
1 |
|
* ``` |
87
|
|
|
* The `PhpViewFileHelper` will use the `$data` array argument to parse the templates. |
88
|
|
|
* |
89
|
|
|
* The template files must be of `php` type if you wish to use internal system. Otherwise, is highly recommended to |
90
|
|
|
* use your own template parser and set the `bodyHtml` and `bodyText` of the `MailMessage` class. |
91
|
|
|
* |
92
|
|
|
* @param MailMessage $message the MailMessage instance to send |
93
|
|
|
* @param array $views the view files for `text` and `html` templates |
94
|
1 |
|
* @param array $data the data to be used for parsing the templates |
95
|
|
|
* |
96
|
1 |
|
* @return SentMessage|null |
97
|
1 |
|
* |
98
|
1 |
|
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface |
99
|
|
|
* @see MailMessage::$bodyHtml |
100
|
|
|
* @see MailMessage::$bodyText |
101
|
|
|
* @see PhpViewFileHelper::render() |
102
|
|
|
*/ |
103
|
|
|
public function send(MailMessage $message, array $views = [], array $data = []): ?SentMessage |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$message = MessageBuilder::make($message); |
106
|
|
|
return $this->getTransportInstance()->send($message); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Factory method to create an instance of the mailer based on the configuration of a `MailMessage` instance. |
111
|
|
|
* |
112
|
|
|
* @param MailMessage $mailMessage the instance to create the Mailer from |
113
|
|
|
* |
114
|
2 |
|
* @return Mailer instance |
115
|
|
|
*/ |
116
|
2 |
|
public static function fromMailMessage(MailMessage $mailMessage) |
117
|
1 |
|
{ |
118
|
|
|
$options = [ |
119
|
|
|
'host' => $mailMessage->host, |
120
|
1 |
|
'port' => $mailMessage->port, |
121
|
|
|
'options' => $mailMessage->transportOptions, |
122
|
1 |
|
]; |
123
|
|
|
$factory = TransportFactory::create($options, $mailMessage->transportType); |
124
|
1 |
|
return new Mailer($factory->create()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|