|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Contact; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
9
|
|
|
use Yiisoft\Mailer\File; |
|
|
|
|
|
|
10
|
|
|
use Yiisoft\Mailer\MailerInterface; |
|
|
|
|
|
|
11
|
|
|
use Yiisoft\Mailer\MessageBodyTemplate; |
|
|
|
|
|
|
12
|
|
|
use Yiisoft\Session\Flash\FlashInterface; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* ContactMailer sends an email from the contact form. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class ContactMailer |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct( |
|
20
|
|
|
private FlashInterface $flash, |
|
21
|
|
|
private LoggerInterface $logger, |
|
22
|
|
|
private MailerInterface $mailer, |
|
23
|
|
|
private string $sender, |
|
24
|
|
|
private string $to |
|
25
|
|
|
) { |
|
26
|
|
|
$this->mailer = $this->mailer->withTemplate(new MessageBodyTemplate(__DIR__ . '/mail/')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function send(ContactForm $form): void |
|
30
|
|
|
{ |
|
31
|
|
|
$message = $this->mailer |
|
32
|
|
|
->compose( |
|
33
|
|
|
'contact-email', |
|
34
|
|
|
[ |
|
35
|
|
|
'content' => $form->getPropertyValue('body'), |
|
36
|
|
|
] |
|
37
|
|
|
) |
|
38
|
|
|
->withSubject($form->getPropertyValue('subject')) |
|
39
|
|
|
->withFrom([$form->getPropertyValue('email') => $form->getPropertyValue('name')]) |
|
40
|
|
|
->withSender($this->sender) |
|
41
|
|
|
->withTo($this->to); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($form->getPropertyValue('attachFiles') as $attachFile) { |
|
44
|
|
|
foreach ($attachFile as $file) { |
|
45
|
|
|
if ($file[0]?->getError() === UPLOAD_ERR_OK) { |
|
46
|
|
|
$message = $message->withAttached( |
|
47
|
|
|
File::fromContent( |
|
48
|
|
|
(string) $file->getStream(), |
|
49
|
|
|
$file->getClientFilename(), |
|
50
|
|
|
$file->getClientMediaType() |
|
51
|
|
|
), |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
|
|
$this->mailer->send($message); |
|
59
|
|
|
$flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.'; |
|
60
|
|
|
} catch (Exception $e) { |
|
61
|
|
|
$flashMsg = $e->getMessage(); |
|
62
|
|
|
$this->logger->error($flashMsg); |
|
63
|
|
|
} finally { |
|
64
|
|
|
$this->flash->add( |
|
65
|
|
|
isset($e) ? 'danger' : 'success', |
|
66
|
|
|
[ |
|
67
|
|
|
'body' => $flashMsg, |
|
|
|
|
|
|
68
|
|
|
], |
|
69
|
|
|
true |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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