|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Mail; |
|
4
|
|
|
|
|
5
|
|
|
use App\Facades\Log; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
use Swift_Mailer; |
|
9
|
|
|
use Swift_Message; |
|
10
|
|
|
use Twig\Environment; |
|
11
|
|
|
use Twig\Error\LoaderError; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Mail helper |
|
15
|
|
|
* |
|
16
|
|
|
* The mail helper provides a simple interface for sending mail via swiftmailer |
|
17
|
|
|
* |
|
18
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Helper |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $config; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Swift_Mailer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $swiftMailer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Twig_Environment |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
|
|
protected $twig; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class constructor |
|
39
|
|
|
* |
|
40
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
array $config, |
|
44
|
|
|
Swift_Mailer $swiftMailer, |
|
45
|
|
|
Environment $twig |
|
46
|
|
|
) { |
|
47
|
|
|
$this->config = $config; |
|
48
|
|
|
$this->swiftMailer = $swiftMailer; |
|
49
|
|
|
$this->twig = $twig; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Send an email |
|
54
|
|
|
* |
|
55
|
|
|
* @param App\Mail\Email $email |
|
|
|
|
|
|
56
|
|
|
* @return bool |
|
57
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
58
|
|
|
*/ |
|
59
|
|
|
public function send(Email $email) |
|
60
|
|
|
{ |
|
61
|
|
|
Log::debug('Beginning mail send', [ |
|
62
|
|
|
'email' => $email, |
|
63
|
|
|
]); |
|
64
|
|
|
try { |
|
65
|
|
|
$message = new Swift_Message( |
|
66
|
|
|
$email->getSubject() |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
// Sender |
|
70
|
|
|
if ($email->getFrom()) { |
|
71
|
|
|
$message->setFrom( |
|
72
|
|
|
$email->getFrom(), |
|
73
|
|
|
$email->getFromName() |
|
74
|
|
|
); |
|
75
|
|
|
} else { |
|
76
|
|
|
$message->setFrom( |
|
77
|
|
|
$this->config['from'], |
|
78
|
|
|
$this->config['from_name'] |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Recipients |
|
83
|
|
|
$destinations = [ |
|
84
|
|
|
'addTo' => $email->getTo(), |
|
85
|
|
|
'addCc' => $email->getCc(), |
|
86
|
|
|
'addBcc' => $email->getBcc(), |
|
87
|
|
|
]; |
|
88
|
|
|
foreach ($destinations as $setter => $addresses) { |
|
89
|
|
|
foreach ($addresses as $address => $name) { |
|
90
|
|
|
$message->$setter($address, $name); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Default template contexts |
|
95
|
|
|
// foreach ($this->config['site'] as $key => $value) { |
|
96
|
|
|
// $email->addTemplateContext( |
|
97
|
|
|
// 'site_' . $key, |
|
98
|
|
|
// $value |
|
99
|
|
|
// ); |
|
100
|
|
|
// } |
|
101
|
|
|
|
|
102
|
|
|
// Content |
|
103
|
|
|
$context = $email->getTemplateContext(); |
|
104
|
|
|
$html = $this->twig->render( |
|
105
|
|
|
$email->getTemplateHtml(), |
|
106
|
|
|
$context |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
try { |
|
110
|
|
|
$text = $this->twig->render( |
|
111
|
|
|
$email->getTemplateText(), |
|
112
|
|
|
$context |
|
113
|
|
|
); |
|
114
|
|
|
} catch (LoaderError $ex) { |
|
115
|
|
|
$text = strip_tags($html); |
|
116
|
|
|
} |
|
117
|
|
|
$text = trim($text); |
|
118
|
|
|
$html = trim($html); |
|
119
|
|
|
$message->setBody($text); |
|
120
|
|
|
$message->addPart($html, 'text/html'); |
|
121
|
|
|
|
|
122
|
|
|
$failed = []; |
|
123
|
|
|
$result = $this->swiftMailer->send($message, $failed); |
|
124
|
|
|
Log::debug('Sent email', [ |
|
125
|
|
|
'recipient_count' => $result, |
|
126
|
|
|
'message' => $message, |
|
127
|
|
|
'failed' => $failed, |
|
128
|
|
|
]); |
|
129
|
|
|
if (0 == $result) { |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return true; |
|
134
|
|
|
} catch (Exception $ex) { |
|
135
|
|
|
Log::error($ex->getMessage(), [ |
|
136
|
|
|
'exception' => $ex, |
|
137
|
|
|
]); |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths