1 <?php
2 /**
3 * Mail.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class use PHPMailer library to send mails from Hawk applications
13 *
14 * @package Network
15 */
16 class Mail{
17 use Utils;
18
19 /**
20 * The PHPMailer instance
21 */
22 private $mailer;
23
24 /**
25 * Default mailing engine
26 */
27 const DEFAULT_MAILER = 'mail';
28
29 /**
30 * Make a new mail
31 *
32 * @param array $param The parameters to pass to PHPMailer
33 */
34 public function __construct($param = array()){
35 $this->mailer = new \PHPMailer;
36
37 $param['Mailer'] = Option::get('main.mailer-type') ? Option::get('main.mailer-type') : self::DEFAULT_MAILER;
38 if($param['Mailer'] == 'smtp' || $param['Mailer'] == 'pop3') {
39 $param['Host'] = Option::get('main.mailer-host');
40 $param['Port'] = Option::get('main.mailer-port');
41 $param['Username'] = Option::get('main.mailer-username');
42 $param['Password'] = Option::get('main.mailer-password');
43 }
44
45 if($param['Mailer'] == 'smtp') {
46 $param['Secure'] = Option::get('main.smtp-secured');
47 }
48
49 $this->map($param, $this->mailer);
50
51 $this->mailer->CharSet = 'utf-8';
52 }
53
54 /**
55 * Static method to make a new mailer
56 *
57 * @param array $param The parameters to pass to PHPMailer
58 */
59 public static function getInstance($param){
60 return new self($param);
61 }
62
63 /**
64 * Set 'from'
65 *
66 * @param string $email The sender email address to set
67 * @param string $name The sender name to set
68 *
69 * @return Mail The instance itself, to permit chained actions
70 */
71 public function from($email, $name = null) {
72 $this->mailer->From = $email;
73 if($name !== null) {
74 $this->fromName($name);
75 }
76
77 return $this;
78 }
79
80 /**
81 * Set 'from-name'
82 *
83 * @param string $name The sender name to set
84 *
85 * @return Mail The instance itself, to permit chained actions
86 */
87 public function fromName($name) {
88 $this->mailer->FromName = $name;
89
90 return $this;
91 }
92
93 /**
94 * Add a recipient
95 *
96 * @param string $email The recipient email address
97 * @param string $name The recipient name
98 *
99 * @return Mail The instance itself, to permit chained actions
100 */
101 public function to($email, $name = '') {
102 $this->mailer->addAddress($email, $name);
103
104 return $this;
105 }
106
107 /**
108 * Set 'Reply-To'
109 *
110 * @param string $email The email address to reply to
111 * @param string $name The name of the person to reply to
112 *
113 * @return Mail The instance itself, to permit chained actions
114 */
115 public function replyTo($email, $name = '') {
116 $this->mailer->addReplyTo($email, $name);
117
118 return $this;
119 }
120
121 /**
122 * Add a recipient in copy
123 *
124 * @param string $email The email address to add in copy
125 * @param string $name The recipient's name to add in copy
126 *
127 * @return Mail The instance itself, to permit chained actions
128 */
129 public function cc($email, $name = '') {
130 $this->mailer->addCC($email, $name);
131
132 return $this;
133 }
134
135 /**
136 * Add a recipient in hidden copy
137 *
138 * @param string $email The recipient's email address to add in hidden copy
139 * @param string $name The recipient's name to add in hidden copy
140 *
141 * @return Mail The instance itself, to permit chained actions
142 */
143 public function bcc($email, $name){
144 $this->mailer->addBCC($email, $name);
145
146 return $this;
147 }
148
149 /**
150 * Attach a file
151 *
152 * @param string $path The file path to attach
153 * @param string $name The attachment name
154 * @param string $encoding The encoding system to add the attachment
155 * @param string $type The file MIME type
156 * @param string $disposition Disposition to use
157 *
158 * @return Mail The instance itself, to permit chained actions
159 */
160 public function attach($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment'){
161 $this->mailer->addAttachment($path, $name, $encoding, $type, $disposition);
162
163 return $this;
164 }
165
166 /**
167 * Set email subject
168 *
169 * @param string $subject The email subject
170 *
171 * @return Mail The instance itself, to permit chained actions
172 */
173 public function subject($subject){
174 $this->mailer->Subject = $subject;
175
176 return $this;
177 }
178
179 /**
180 * Set HTML content
181 *
182 * @param string $html The html content to set
183 *
184 * @return Mail The instance itself, to permit chained actions
185 */
186 public function html($html){
187 $this->mailer->isHTML(true);
188
189 $this->mailer->Body = $html;
190
191 if(empty($this->mailer->AltBody)) {
192 $text = preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $html);
193 $text = strip_tags($text);
194
195 $this->text($text);
196 }
197
198 return $this;
199 }
200
201 /**
202 * Set text content
203 *
204 * @param string $text The text to set
205 *
206 * @return Mail The instance itself, to permit chained actions
207 */
208 public function text($text){
209 $this->mailer->AltBody = $text;
210
211 return $this;
212 }
213
214
215 /**
216 * Send the mail
217 *
218 * @throws MailException
219 */
220 public function send(){
221 if(!$this->mailer->send()) {
222 App::logger()->error('The mail could not be sent because : ' . $this->mailer->ErrorInfo);
223 throw new MailException($this->mailer->ErrorInfo);
224 }
225 App::logger()->info('An email was sent to ' . implode(', ', $this->mailer->getAllRecipientAddresses()));
226 }
227 }
228
229 /**
230 * MailException
231 *
232 * @package Exceptions
233 */
234 class MailException extends \Exception{
235
236 }