| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services\Libs; |
| 4 |
|
| 5 |
class Mailer |
| 6 |
{ |
| 7 |
private $subject = ''; |
| 8 |
|
| 9 |
private $body = ''; |
| 10 |
|
| 11 |
private $to = ''; |
| 12 |
|
| 13 |
private $from = ''; |
| 14 |
|
| 15 |
private $cc = []; |
| 16 |
|
| 17 |
private $bcc = []; |
| 18 |
|
| 19 |
private $replyTo = ''; |
| 20 |
|
| 21 |
private $isHtml = true; |
| 22 |
|
| 23 |
public function __construct($to = '', $subject = '', $body = '') |
| 24 |
{ |
| 25 |
$this->to = $to; |
| 26 |
$this->subject = $subject; |
| 27 |
$this->body = $body; |
| 28 |
} |
| 29 |
|
| 30 |
public function setSubject($subject) |
| 31 |
{ |
| 32 |
$this->subject = $subject; |
| 33 |
return $this; |
| 34 |
} |
| 35 |
|
| 36 |
public function setBody($body) |
| 37 |
{ |
| 38 |
$this->body = $body; |
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function to($to) |
| 43 |
{ |
| 44 |
$this->to = $to; |
| 45 |
return $this; |
| 46 |
} |
| 47 |
|
| 48 |
public function setIsHtml($isHtml) |
| 49 |
{ |
| 50 |
$this->isHtml = $isHtml; |
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
public function setFrom($from) |
| 55 |
{ |
| 56 |
$this->from = $from; |
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
public function addCC($cc) |
| 61 |
{ |
| 62 |
$this->cc[] = $cc; |
| 63 |
return $this; |
| 64 |
} |
| 65 |
|
| 66 |
public function addBCC($bcc) |
| 67 |
{ |
| 68 |
$this->bcc[] = $bcc; |
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
public function setReplyTo($replyTo) |
| 73 |
{ |
| 74 |
$this->replyTo = $replyTo; |
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
public function send() |
| 79 |
{ |
| 80 |
if (!$this->to && !$this->cc && !$this->bcc) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$headers = []; |
| 85 |
|
| 86 |
if ($this->isHtml) { |
| 87 |
$headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 88 |
} else { |
| 89 |
$headers[] = 'Content-Type: text/plain; charset=UTF-8'; |
| 90 |
} |
| 91 |
|
| 92 |
if ($this->from) { |
| 93 |
$headers[] = 'From: ' . $this->from; |
| 94 |
} |
| 95 |
|
| 96 |
if ($this->cc) { |
| 97 |
$headers[] = 'Cc: ' . implode(',', $this->cc); |
| 98 |
} |
| 99 |
|
| 100 |
if ($this->bcc) { |
| 101 |
$headers[] = 'Bcc: ' . implode(',', $this->bcc); |
| 102 |
} |
| 103 |
|
| 104 |
if ($this->replyTo) { |
| 105 |
$headers[] = 'Reply-To: ' . $this->replyTo; |
| 106 |
} |
| 107 |
|
| 108 |
return wp_mail($this->to, $this->subject, $this->body, $headers); |
| 109 |
} |
| 110 |
} |
| 111 |
|