PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.11.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.11.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / app / Services / Libs / Mailer.php

Mailer.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.11.0, at app/Services/Libs/Mailer.php

159 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services\Libs;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\Framework\Support\Arr;
7
8 class Mailer
9 {
10 private $subject = '';
11
12 private $body = '';
13
14 private $to = '';
15
16 private $from = '';
17
18 private $cc = [];
19
20 private $bcc = [];
21
22 private $replyTo = '';
23
24 private $isHtml = true;
25
26 public function __construct($to = '', $subject = '', $body = '')
27 {
28 $this->to = $to;
29 $this->subject = $subject;
30 $this->body = $body;
31
32 $this->setDefaultHeaders();
33 }
34
35 public function setDefaultHeaders($settings = null)
36 {
37 if (!$settings) {
38 $settings = Utility::getEmailNotificationSettings();
39 }
40
41 $sendFromName = Arr::get($settings, 'send_from_name', '');
42 $sendFromEmail = Arr::get($settings, 'send_from_email', '');
43
44 if ($sendFromName && $sendFromEmail) {
45 $this->from = $sendFromName . ' <' . $sendFromEmail . '>';
46 } else if ($sendFromEmail) {
47 $this->from = $sendFromEmail;
48 }
49
50 $replyToEmail = Arr::get($settings, 'reply_to_email', '');
51 $replyToName = Arr::get($settings, 'reply_to_name', '');
52
53 if ($replyToEmail && $replyToName) {
54 $this->replyTo = $replyToName . ' <' . $replyToEmail . '>';
55 } else if ($replyToEmail) {
56 $this->replyTo = $replyToEmail;
57 }
58
59 return $this;
60 }
61
62 public function setSubject($subject)
63 {
64 $this->subject = $subject;
65 return $this;
66 }
67
68 public function setBody($body)
69 {
70 $this->body = $body;
71 return $this;
72 }
73
74 public function to($email, $name = '')
75 {
76 // wp_mail() splits a To string on commas, so a display name carrying a
77 // comma/semicolon injects extra recipients; angle brackets/quotes reframe
78 // the address and CR/LF inject headers. Strip them before building it.
79 if ($name) {
80 $name = trim(preg_replace('/[,;<>"\r\n\t]+/', ' ', $name));
81 }
82
83 if ($name) {
84 $this->to = $name . ' <' . $email . '>';
85 } else {
86 $this->to = $email;
87 }
88 return $this;
89 }
90
91 public function setIsHtml($isHtml)
92 {
93 $this->isHtml = $isHtml;
94 return $this;
95 }
96
97 public function setFrom($from)
98 {
99 $this->from = $from;
100 return $this;
101 }
102
103 public function addCC($cc)
104 {
105 $this->cc[] = $cc;
106 return $this;
107 }
108
109 public function addBCC($bcc)
110 {
111 $this->bcc[] = $bcc;
112 return $this;
113 }
114
115 public function setReplyTo($replyTo)
116 {
117 $this->replyTo = $replyTo;
118 return $this;
119 }
120
121 public function send()
122 {
123 if (!$this->to && !$this->cc && !$this->bcc) {
124 return false;
125 }
126
127 $headers = [];
128
129 if ($this->isHtml) {
130 $headers[] = 'Content-Type: text/html; charset=UTF-8';
131 } else {
132 $headers[] = 'Content-Type: text/plain; charset=UTF-8';
133 }
134
135 // A CR/LF in any header value starts an attacker-controlled header line.
136 $stripCrlf = function ($value) {
137 return str_replace(["\r", "\n"], '', $value);
138 };
139
140 if ($this->from) {
141 $headers[] = 'From: ' . $stripCrlf($this->from);
142 }
143
144 if ($this->cc) {
145 $headers[] = 'Cc: ' . $stripCrlf(implode(',', $this->cc));
146 }
147
148 if ($this->bcc) {
149 $headers[] = 'Bcc: ' . $stripCrlf(implode(',', $this->bcc));
150 }
151
152 if ($this->replyTo) {
153 $headers[] = 'Reply-To: ' . $stripCrlf($this->replyTo);
154 }
155
156 return wp_mail($this->to, $this->subject, $this->body, $headers);
157 }
158 }
159