-
Notifications
You must be signed in to change notification settings - Fork 21
/
MailerService.php
82 lines (68 loc) · 2.31 KB
/
MailerService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace TypeRocket\Services
{
use TypeRocket\Core\Config;
use TypeRocket\Core\Container;
class MailerService extends Service
{
protected $driver;
public const ALIAS = 'mail';
/**
* @return $this|Service
* @throws \Exception
*/
public function register() : Service
{
$default = Config::get('mail.default');
if(!$default) {
throw new \Exception('mail.php config is missing.');
}
$driver = Config::get("mail.drivers.{$default}.driver");
$this->driver( apply_filters('typerocket_mailer_service_driver', new $driver ) );
return $this;
}
/**
* @param \TypeRocket\Pro\Utility\Mailers\MailDriver|null $driver
*
* @return \TypeRocket\Pro\Utility\Mailers\MailDriver|null
*/
public function driver(\TypeRocket\Pro\Utility\Mailers\MailDriver $driver = null)
{
if(func_num_args() == 0) {
return $this->driver;
}
return $this->driver = $driver;
}
/**
* @param string|string[] $to Array or comma-separated list of email addresses to send message.
* @param string $subject Email subject.
* @param string $message Message contents.
* @param string|string[] $headers Additional headers.
* @param string|string[] $attachments Paths to files to attach.
* @return bool
*/
public function send($to, $subject, $message, $headers = '', $attachments = []) : bool
{
return $this->driver()->send(...func_get_args());
}
/**
* @return static
*/
public static function getFromContainer()
{
return Container::resolve(static::ALIAS);
}
}
}
namespace
{
use TypeRocket\Services\MailerService;
function typerocket_mail_service_override_wp_mail($return, $args): bool
{
$mailer = MailerService::getFromContainer();
return $mailer->send($args['to'], $args['subject'], $args['message'], $args['headers'], $args['attachments']);
}
if(\TypeRocket\Core\Config::get('mail.default')) {
add_filter('pre_wp_mail', 'typerocket_mail_service_override_wp_mail', 0, 2);
}
}