PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Services / Email / Mailer.php

Mailer.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Services/Email/Mailer.php

197 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Email;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Services\Libs\Emogrifier\Emogrifier;
7 use FluentCart\App\Services\ShortCodeParser\ShortcodeTemplateBuilder;
8 use FluentCart\Framework\Support\Arr;
9
10 class Mailer
11 {
12 private string $subject = '';
13
14 private string $body = '';
15
16 private string $footer = '';
17
18 private $to = '';
19
20 private string $from = '';
21
22 private array $cc = [];
23
24 private array $bcc = [];
25
26 private string $replyTo = '';
27
28 private bool $isHtml = true;
29
30 private array $attachments = [];
31
32 public function __construct($to = '', string $subject = '', string $body = '')
33 {
34 $this->to = $to;
35 $this->subject = $subject;
36 $this->body = $body;
37
38 $this->setDefaults();
39 }
40
41
42 public function subject(string $subject): Mailer
43 {
44 $this->subject = $subject;
45 return $this;
46 }
47
48 public function setDefaults($settings = null): Mailer
49 {
50 if (!$settings) {
51 $settings = EmailNotifications::getSettings();
52 }
53
54 $sendFromName = Arr::get($settings, 'from_name', '');
55 $sendFromEmail = Arr::get($settings, 'from_email', '');
56
57 if ($sendFromName && $sendFromEmail) {
58 $this->from = $sendFromName . ' <' . $sendFromEmail . '>';
59 } else if ($sendFromEmail) {
60 $this->from = $sendFromEmail;
61 }
62
63 $replyToEmail = Arr::get($settings, 'reply_to_email', '');
64 $replyToName = Arr::get($settings, 'reply_to_name', '');
65
66 if ($replyToEmail && $replyToName) {
67 $this->replyTo = $replyToName . ' <' . $replyToEmail . '>';
68 } else if ($replyToEmail) {
69 $this->replyTo = $replyToEmail;
70 }
71
72 $this->footer = '';
73
74 return $this;
75 }
76
77 public function setSubject($subject): Mailer
78 {
79 $this->subject = $subject;
80 return $this;
81 }
82
83 public function body($body): Mailer
84 {
85 $this->body = $body;
86 return $this;
87 }
88
89 public function to($email, $name = ''): Mailer
90 {
91 if ($name) {
92 $this->to = $name . ' <' . $email . '>';
93 } else {
94 $this->to = $email;
95 }
96 return $this;
97 }
98
99 public function setIsHtml($isHtml): Mailer
100 {
101 $this->isHtml = $isHtml;
102 return $this;
103 }
104
105 public function setFrom($from): Mailer
106 {
107 $this->from = $from;
108 return $this;
109 }
110
111 public function addCC($cc): Mailer
112 {
113 $this->cc[] = $cc;
114 return $this;
115 }
116
117 public function addBCC($bcc): Mailer
118 {
119 $this->bcc[] = $bcc;
120 return $this;
121 }
122
123 public function setReplyTo($replyTo): Mailer
124 {
125 $this->replyTo = $replyTo;
126 return $this;
127 }
128
129 public function addAttachment(string $filePath): Mailer
130 {
131 $this->attachments[] = $filePath;
132 return $this;
133 }
134
135 public function setAttachments(array $attachments): Mailer
136 {
137 $this->attachments = $attachments;
138 return $this;
139 }
140
141 public function send($cssInliner = false)
142 {
143 if (!$this->to && !$this->cc && !$this->bcc) {
144 return false;
145 }
146
147 if ($cssInliner) {
148 $this->body = (string)(new Emogrifier($this->body))->emogrify();
149 }
150
151 $headers = [];
152
153 if ($this->isHtml) {
154 $headers[] = 'Content-Type: text/html; charset=UTF-8';
155 } else {
156 $headers[] = 'Content-Type: text/plain; charset=UTF-8';
157 }
158
159 if ($this->from) {
160 $headers[] = 'From: ' . $this->from;
161 }
162
163 if ($this->cc) {
164 $headers[] = 'Cc: ' . implode(',', $this->cc);
165 }
166
167 if ($this->bcc) {
168 $headers[] = 'Bcc: ' . implode(',', $this->bcc);
169 }
170
171 if ($this->replyTo) {
172 $headers[] = 'Reply-To: ' . $this->replyTo;
173 }
174
175 return wp_mail($this->to, $this->subject, $this->body, $headers, $this->attachments);
176 }
177
178 public function wrapWithFooter($content): string
179 {
180 if (!empty($this->footer)) {
181 $content .= ShortcodeTemplateBuilder::make($this->footer, []);
182 }
183
184 if (!App::isProActive()) {
185 $cartFooter = "<div style='background: #fff;padding: 32px; text-align: center; font-size: 16px; color: #2F3448;'>Powered by <a href='https://fluentcart.com' style='color: #017EF3; text-decoration: none;'>FluentCart</a></div>";
186 $content .= $cartFooter;
187 }
188 return $content;
189
190 }
191
192 public static function make(string $to = '', string $subject = '', string $body = ''): Mailer
193 {
194 return new static($to, $subject, $body);
195 }
196 }
197