1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\HeloLaravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Mail\Mailer as MailerContract; |
6
|
|
|
use Illuminate\Mail\Mailer as LaravelMailer; |
7
|
|
|
use Illuminate\Mail\MailManager; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Facades\Mail; |
10
|
|
|
use Illuminate\Support\Facades\View; |
11
|
|
|
use Illuminate\Support\ServiceProvider; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use Swift_Mailer; |
14
|
|
|
|
15
|
|
|
class HeloLaravelServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Bootstrap the application services. |
19
|
|
|
*/ |
20
|
|
|
public function boot() |
21
|
|
|
{ |
22
|
|
|
if (!$this->app['config']['helo.is_enabled']) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$instance = app()->make(Mailer::class); |
27
|
|
|
|
28
|
|
|
Mail::swap($instance); |
29
|
|
|
|
30
|
|
|
app()->instance(MailerContract::class, $instance); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register the application services. |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
if ($this->app->runningInConsole()) { |
39
|
|
|
$this->commands([ |
40
|
|
|
TestMailCommand::class, |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
View::addNamespace('helo', __DIR__ . '/../resources/views'); |
44
|
|
|
|
45
|
|
|
$this->publishes([ |
46
|
|
|
__DIR__.'/../config/helo.php' => base_path('config/helo.php'), |
47
|
|
|
], 'config'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/helo.php', 'helo'); |
51
|
|
|
|
52
|
|
|
$this->app->singleton(Mailer::class, function ($app) { |
53
|
|
|
if (version_compare($app->version(), '7.0.0', '<')) { |
54
|
|
|
return $this->createLaravel6Mailer($app); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->createLaravel7Mailer($app); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function createLaravel6Mailer($app) |
62
|
|
|
{ |
63
|
|
|
$config = $this->getConfig(); |
64
|
|
|
|
65
|
|
|
// Once we have create the mailer instance, we will set a container instance |
66
|
|
|
// on the mailer. This allows us to resolve mailer classes via containers |
67
|
|
|
// for maximum testability on said classes instead of passing Closures. |
68
|
|
|
$mailer = new Mailer( |
69
|
|
|
$app['view'], $app['swift.mailer'], $app['events'] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
if ($app->bound('queue')) { |
73
|
|
|
$mailer->setQueue($app['queue']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Next we will set all of the global addresses on this mailer, which allows |
77
|
|
|
// for easy unification of all "from" addresses as well as easy debugging |
78
|
|
|
// of sent messages since they get be sent into a single email address. |
79
|
|
|
foreach (['from', 'reply_to', 'to'] as $type) { |
80
|
|
|
$this->setGlobalAddress($mailer, $config, $type); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $mailer; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function createLaravel7Mailer($app) |
87
|
|
|
{ |
88
|
|
|
$mailManager = $app['mail.manager']; |
89
|
|
|
|
90
|
|
|
$swiftMailer = null; |
91
|
|
|
|
92
|
|
|
// Laravel 7 no longer binds the Swift_Mailer so we try and get it from a mailer |
93
|
|
|
if ($mailManager instanceof MailManager) { |
94
|
|
|
$laravelMailer = $mailManager->mailer(); |
95
|
|
|
|
96
|
|
|
if ($laravelMailer instanceof LaravelMailer) { |
97
|
|
|
$swiftMailer = $laravelMailer->getSwiftMailer(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$config = $this->getConfig($mailManager->getDefaultDriver()); |
102
|
|
|
|
103
|
|
|
// If we could not extra a Swift_Mailer instance from the driver we do it our self |
104
|
|
|
if ($swiftMailer === null) { |
105
|
|
|
$swiftMailer = new Swift_Mailer($app['mail.manager']->createTransport($config)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Once we have create the mailer instance, we will set a container instance |
109
|
|
|
// on the mailer. This allows us to resolve mailer classes via containers |
110
|
|
|
// for maximum testability on said classes instead of passing Closures. |
111
|
|
|
$mailer = new Laravel7Mailer( |
112
|
|
|
'smtp', $app['view'], $swiftMailer, $app['events'] |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
if ($app->bound('queue')) { |
116
|
|
|
$mailer->setQueue($app['queue']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Next we will set all of the global addresses on this mailer, which allows |
120
|
|
|
// for easy unification of all "from" addresses as well as easy debugging |
121
|
|
|
// of sent messages since they get be sent into a single email address. |
122
|
|
|
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) { |
123
|
|
|
$this->setGlobalAddress($mailer, $config, $type); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $mailer; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getConfig($name = 'smtp') |
130
|
|
|
{ |
131
|
|
|
return $this->app['config']['mail.driver'] |
132
|
|
|
? $this->app['config']['mail'] |
133
|
|
|
: $this->app['config']["mail.mailers.{$name}"]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set a global address on the mailer by type. |
138
|
|
|
* |
139
|
|
|
* @param \Illuminate\Mail\Mailer $mailer |
140
|
|
|
* @param array $config |
141
|
|
|
* @param string $type |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected function setGlobalAddress($mailer, array $config, $type) |
145
|
|
|
{ |
146
|
|
|
if (version_compare(app()->version(), '7.0.0', '<')) { |
147
|
|
|
$address = Arr::get($config, $type); |
148
|
|
|
} else { |
149
|
|
|
$address = Arr::get($config, $type, $this->app['config']['mail.'.$type]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (is_array($address) && isset($address['address'])) { |
153
|
|
|
$mailer->{'always' . Str::studly($type)}($address['address'], $address['name']); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|