1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * Saito - The Threaded Web Forum |
||
7 | * |
||
8 | * @copyright Copyright (c) the Saito Project Developers |
||
9 | * @link https://github.com/Schlaefer/Saito |
||
10 | * @license http://opensource.org/licenses/MIT |
||
11 | */ |
||
12 | |||
13 | namespace App\Controller\Component; |
||
14 | |||
15 | use Cake\Controller\Component; |
||
16 | use Cake\Core\Configure; |
||
17 | use Cake\Log\LogTrait; |
||
18 | use Cake\Mailer\Email; |
||
19 | use Cake\Mailer\Transport\DebugTransport; |
||
20 | use Cake\Routing\Router; |
||
21 | use Cake\Utility\Text; |
||
22 | use Saito\Contact\SaitoEmailContact; |
||
23 | |||
24 | class SaitoEmailComponent extends Component |
||
25 | { |
||
26 | use LogTrait; |
||
27 | |||
28 | /** |
||
29 | * send email |
||
30 | * |
||
31 | * @param array $params params |
||
32 | * - 'recipient' userId, predefined or User entity |
||
33 | * - 'sender' userId, predefined or User entity |
||
34 | * - 'ccsender' bool send carbon-copy to sender |
||
35 | * - 'template' string |
||
36 | * - 'message' string |
||
37 | * - 'viewVars' array |
||
38 | * @return void |
||
39 | */ |
||
40 | public function email($params = []) |
||
41 | { |
||
42 | $defaults = [ |
||
43 | 'ccsender' => false, |
||
44 | 'message' => '', |
||
45 | 'sender' => 'system', |
||
46 | 'viewVars' => [ |
||
47 | 'forumName' => Configure::read('Saito.Settings.forum_name'), |
||
48 | 'webroot' => Router::url('/', true), |
||
49 | ], |
||
50 | ]; |
||
51 | $params += $defaults; |
||
52 | |||
53 | $from = new SaitoEmailContact($params['sender']); |
||
54 | $to = new SaitoEmailContact($params['recipient']); |
||
55 | |||
56 | $email = new Email('saito'); |
||
57 | $email->setEmailFormat('text') |
||
58 | ->setFrom($from->toCake()) |
||
59 | ->setTo($to->toCake()) |
||
60 | ->setSubject($params['subject']) |
||
61 | ->viewBuilder()->setTemplate($params['template']); |
||
62 | |||
63 | $params['viewVars']['message'] = $params['message']; |
||
64 | $email->setViewVars($params['viewVars'] + $defaults['viewVars']); |
||
65 | |||
66 | if ($params['ccsender']) { |
||
67 | $this->_sendCopyToOriginalSender($email); |
||
68 | } |
||
69 | $this->_send($email); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Sends a copy of a completely configured email to the author |
||
74 | * |
||
75 | * @param Email $email email |
||
76 | * @return void |
||
77 | */ |
||
78 | protected function _sendCopyToOriginalSender(Email $email) |
||
79 | { |
||
80 | /* set new subject */ |
||
81 | $email = clone $email; |
||
82 | $to = new SaitoEmailContact($email->getTo()); |
||
83 | $subject = $email->getSubject(); |
||
84 | $data = ['subject' => $subject, 'recipient-name' => $to->getName()]; |
||
85 | $subject = __('Copy of your message: ":subject" to ":recipient-name"'); |
||
86 | $subject = Text::insert($subject, $data); |
||
87 | $email->setSubject($subject); |
||
88 | |||
89 | $email->setTo($email->getFrom()); |
||
90 | $from = new SaitoEmailContact('system'); |
||
91 | $email->setFrom($from->toCake()); |
||
92 | |||
93 | $this->_send($email); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Sends the completely configured email |
||
98 | * |
||
99 | * @param Email $email email |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function _send(Email $email) |
||
103 | { |
||
104 | $debug = Configure::read('Saito.debug.email'); |
||
105 | if ($debug) { |
||
106 | $transport = new DebugTransport(); |
||
107 | $email->transport($transport); |
||
0 ignored issues
–
show
|
|||
108 | }; |
||
109 | |||
110 | $sender = (new SaitoEmailContact('system'))->toCake(); |
||
111 | if ($email->getFrom() !== $sender) { |
||
112 | $email->setSender($sender); |
||
113 | } |
||
114 | $result = $email->send(); |
||
115 | |||
116 | if ($debug) { |
||
117 | $this->log($result, 'debug'); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.