| 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 |
if ($name) { |
| 77 |
$this->to = $name . ' <' . $email . '>'; |
| 78 |
} else { |
| 79 |
$this->to = $email; |
| 80 |
} |
| 81 |
return $this; |
| 82 |
} |
| 83 |
|
| 84 |
public function setIsHtml($isHtml) |
| 85 |
{ |
| 86 |
$this->isHtml = $isHtml; |
| 87 |
return $this; |
| 88 |
} |
| 89 |
|
| 90 |
public function setFrom($from) |
| 91 |
{ |
| 92 |
$this->from = $from; |
| 93 |
return $this; |
| 94 |
} |
| 95 |
|
| 96 |
public function addCC($cc) |
| 97 |
{ |
| 98 |
$this->cc[] = $cc; |
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
public function addBCC($bcc) |
| 103 |
{ |
| 104 |
$this->bcc[] = $bcc; |
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
public function setReplyTo($replyTo) |
| 109 |
{ |
| 110 |
$this->replyTo = $replyTo; |
| 111 |
return $this; |
| 112 |
} |
| 113 |
|
| 114 |
public function send() |
| 115 |
{ |
| 116 |
if (!$this->to && !$this->cc && !$this->bcc) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$headers = []; |
| 121 |
|
| 122 |
if ($this->isHtml) { |
| 123 |
$headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 124 |
} else { |
| 125 |
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; |
| 126 |
} |
| 127 |
|
| 128 |
if ($this->from) { |
| 129 |
$headers[] = 'From: ' . $this->from; |
| 130 |
} |
| 131 |
|
| 132 |
if ($this->cc) { |
| 133 |
$headers[] = 'Cc: ' . implode(',', $this->cc); |
| 134 |
} |
| 135 |
|
| 136 |
if ($this->bcc) { |
| 137 |
$headers[] = 'Bcc: ' . implode(',', $this->bcc); |
| 138 |
} |
| 139 |
|
| 140 |
if ($this->replyTo) { |
| 141 |
$headers[] = 'Reply-To: ' . $this->replyTo; |
| 142 |
} |
| 143 |
|
| 144 |
return wp_mail($this->to, $this->subject, $this->body, $headers); |
| 145 |
} |
| 146 |
} |
| 147 |
|