1 | <?php |
||
10 | class Email |
||
11 | { |
||
12 | /** |
||
13 | * Application Object |
||
14 | * |
||
15 | * @var \System\Application |
||
16 | */ |
||
17 | private $app; |
||
18 | |||
19 | /** |
||
20 | * PHPMailer Object |
||
21 | * |
||
22 | * @var PHPMailer |
||
23 | */ |
||
24 | private $mail; |
||
25 | |||
26 | /** |
||
27 | * Constructor |
||
28 | * |
||
29 | * @param \System\Application $app |
||
30 | */ |
||
31 | public function __construct(Application $app) |
||
39 | |||
40 | /** |
||
41 | * Set up the configrations |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | private function setUp() |
||
56 | |||
57 | /** |
||
58 | * To add addresses or attachments easily to the object |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | private function add($input, $add) |
||
63 | { |
||
64 | if (!is_array($input)) { |
||
65 | $input = [$input]; |
||
66 | } |
||
67 | |||
68 | foreach ($input as $key => $value) { |
||
69 | if (is_numeric($key)) { |
||
70 | $this->mail->$add($value); |
||
71 | } else { |
||
72 | $this->mail->$add($value, $key); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Add recipients to email |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function recipients($addresses, array $replayTo = [], $cc = null, $bcc = null) |
||
83 | { |
||
84 | $this->mail->setFrom($_ENV['EMAIL_ADMIN'], $_ENV['EMAIL_NAME']); |
||
85 | |||
86 | $this->addresses($addresses); |
||
87 | |||
88 | if (!empty($replayTo)) { |
||
89 | $this->mail->addReplyTo(array_values($replayTo)[0], array_keys($replayTo)[0]); |
||
90 | } |
||
91 | |||
92 | if ($cc) { |
||
93 | $this->mail->addCC($cc); |
||
94 | } |
||
95 | |||
96 | if ($bcc) { |
||
97 | $this->mail->addBCC($bcc); |
||
98 | } |
||
99 | |||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Add addresses |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | private function addresses($addresses) |
||
109 | { |
||
110 | $this->add($addresses, 'addAddress'); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Add attachments to email |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function attachments($attachments) |
||
119 | { |
||
120 | $this->add($attachments, 'addAttachment'); |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Add content to email |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function content($html, $subject, $body, $altBody) |
||
139 | |||
140 | /** |
||
141 | * Send email |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public function send() |
||
154 | } |
||
155 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: