blob: ef20e19789c75a19999386b1b8943e29d10fe2c0 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Email Class
20 *
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/email.html
28 */
29class CI_Email {
30
31 var $useragent = "CodeIgniter";
32 var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
33 var $protocol = "mail"; // mail/sendmail/smtp
Derek Jones37f4b9c2011-07-01 17:56:50 -050034 var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
Derek Allard2067d1a2008-11-13 22:59:24 +000035 var $smtp_user = ""; // SMTP Username
36 var $smtp_pass = ""; // SMTP Password
37 var $smtp_port = "25"; // SMTP Port
38 var $smtp_timeout = 5; // SMTP Timeout in seconds
Radu Potopbbf04b02011-09-28 13:57:51 +030039 var $smtp_crypto = ""; // SMTP Encryption. Can be null, tls or ssl.
Derek Jones37f4b9c2011-07-01 17:56:50 -050040 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
Derek Allard2067d1a2008-11-13 22:59:24 +000041 var $wrapchars = "76"; // Number of characters to wrap at.
Derek Jones37f4b9c2011-07-01 17:56:50 -050042 var $mailtype = "text"; // text/html Defines email formatting
Derek Allard2067d1a2008-11-13 22:59:24 +000043 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
44 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
45 var $alt_message = ''; // Alternative message for HTML emails
Derek Jones37f4b9c2011-07-01 17:56:50 -050046 var $validate = FALSE; // TRUE/FALSE. Enables email validation
Derek Allard2067d1a2008-11-13 22:59:24 +000047 var $priority = "3"; // Default priority (1 - 5)
48 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
Derek Jones37f4b9c2011-07-01 17:56:50 -050049 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000050 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
51 // distasteful, is the only thing that seems to work for all environments.
Derek Jones37f4b9c2011-07-01 17:56:50 -050052 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
53 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
55 var $_safe_mode = FALSE;
56 var $_subject = "";
57 var $_body = "";
58 var $_finalbody = "";
59 var $_alt_boundary = "";
60 var $_atc_boundary = "";
61 var $_header_str = "";
62 var $_smtp_connect = "";
63 var $_encoding = "8bit";
64 var $_IP = FALSE;
65 var $_smtp_auth = FALSE;
66 var $_replyto_flag = FALSE;
67 var $_debug_msg = array();
68 var $_recipients = array();
69 var $_cc_array = array();
70 var $_bcc_array = array();
71 var $_headers = array();
72 var $_attach_name = array();
73 var $_attach_type = array();
74 var $_attach_disp = array();
75 var $_protocols = array('mail', 'sendmail', 'smtp');
76 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
77 var $_bit_depths = array('7bit', '8bit');
78 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
79
80
81 /**
82 * Constructor - Sets Email Preferences
83 *
84 * The constructor can be passed an array of config values
85 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000086 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 if (count($config) > 0)
89 {
90 $this->initialize($config);
91 }
92 else
93 {
94 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
95 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
96 }
97
98 log_message('debug', "Email Class Initialized");
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Initialize preferences
105 *
106 * @access public
107 * @param array
108 * @return void
109 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000110 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 foreach ($config as $key => $val)
113 {
114 if (isset($this->$key))
115 {
116 $method = 'set_'.$key;
117
118 if (method_exists($this, $method))
119 {
120 $this->$method($val);
121 }
122 else
123 {
124 $this->$key = $val;
125 }
126 }
127 }
Eric Barnes6113f542010-12-29 13:36:12 -0500128 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
131 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000132
133 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Initialize the Email Data
140 *
141 * @access public
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800142 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @return void
144 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000145 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 $this->_subject = "";
148 $this->_body = "";
149 $this->_finalbody = "";
150 $this->_header_str = "";
151 $this->_replyto_flag = FALSE;
152 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500153 $this->_cc_array = array();
154 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $this->_headers = array();
156 $this->_debug_msg = array();
157
158 $this->_set_header('User-Agent', $this->useragent);
159 $this->_set_header('Date', $this->_set_date());
160
161 if ($clear_attachments !== FALSE)
162 {
163 $this->_attach_name = array();
164 $this->_attach_type = array();
165 $this->_attach_disp = array();
166 }
Eric Barnes6113f542010-12-29 13:36:12 -0500167
Greg Akera769deb2010-11-10 15:47:29 -0600168 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
174 * Set FROM
175 *
176 * @access public
177 * @param string
178 * @param string
179 * @return void
180 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000181 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
183 if (preg_match( '/\<(.*)\>/', $from, $match))
184 {
185 $from = $match['1'];
186 }
187
188 if ($this->validate)
189 {
190 $this->validate_email($this->_str_to_array($from));
191 }
192
193 // prepare the display name
194 if ($name != '')
195 {
196 // only use Q encoding if there are characters that would require it
197 if ( ! preg_match('/[\200-\377]/', $name))
198 {
199 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000200 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202 else
203 {
204 $name = $this->_prep_q_encoding($name, TRUE);
205 }
206 }
207
208 $this->_set_header('From', $name.' <'.$from.'>');
209 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500210
Greg Akera769deb2010-11-10 15:47:29 -0600211 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * Set Reply-to
218 *
219 * @access public
220 * @param string
221 * @param string
222 * @return void
223 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000224 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if (preg_match( '/\<(.*)\>/', $replyto, $match))
227 {
228 $replyto = $match['1'];
229 }
230
231 if ($this->validate)
232 {
233 $this->validate_email($this->_str_to_array($replyto));
234 }
235
236 if ($name == '')
237 {
238 $name = $replyto;
239 }
240
241 if (strncmp($name, '"', 1) != 0)
242 {
243 $name = '"'.$name.'"';
244 }
245
246 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
247 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600248
249 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
253
254 /**
255 * Set Recipients
256 *
257 * @access public
258 * @param string
259 * @return void
260 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000261 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $to = $this->_str_to_array($to);
264 $to = $this->clean_email($to);
265
266 if ($this->validate)
267 {
268 $this->validate_email($to);
269 }
270
271 if ($this->_get_protocol() != 'mail')
272 {
273 $this->_set_header('To', implode(", ", $to));
274 }
275
276 switch ($this->_get_protocol())
277 {
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000278 case 'smtp' :
279 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 break;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000281 case 'sendmail' :
282 case 'mail' :
283 $this->_recipients = implode(", ", $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 break;
285 }
Eric Barnes6113f542010-12-29 13:36:12 -0500286
Greg Akera769deb2010-11-10 15:47:29 -0600287 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Set CC
294 *
295 * @access public
296 * @param string
297 * @return void
298 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000299 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 $cc = $this->_str_to_array($cc);
302 $cc = $this->clean_email($cc);
303
304 if ($this->validate)
305 {
306 $this->validate_email($cc);
307 }
308
309 $this->_set_header('Cc', implode(", ", $cc));
310
311 if ($this->_get_protocol() == "smtp")
312 {
313 $this->_cc_array = $cc;
314 }
Eric Barnes6113f542010-12-29 13:36:12 -0500315
Greg Akera769deb2010-11-10 15:47:29 -0600316 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
320
321 /**
322 * Set BCC
323 *
324 * @access public
325 * @param string
326 * @param string
327 * @return void
328 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000329 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 if ($limit != '' && is_numeric($limit))
332 {
333 $this->bcc_batch_mode = TRUE;
334 $this->bcc_batch_size = $limit;
335 }
336
337 $bcc = $this->_str_to_array($bcc);
338 $bcc = $this->clean_email($bcc);
339
340 if ($this->validate)
341 {
342 $this->validate_email($bcc);
343 }
344
345 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
346 {
347 $this->_bcc_array = $bcc;
348 }
349 else
350 {
351 $this->_set_header('Bcc', implode(", ", $bcc));
352 }
Eric Barnes6113f542010-12-29 13:36:12 -0500353
Greg Akera769deb2010-11-10 15:47:29 -0600354 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
358
359 /**
360 * Set Email Subject
361 *
362 * @access public
363 * @param string
364 * @return void
365 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000366 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $subject = $this->_prep_q_encoding($subject);
369 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600370 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // --------------------------------------------------------------------
374
375 /**
376 * Set Body
377 *
378 * @access public
379 * @param string
380 * @return void
381 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000382 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
Greg Akera769deb2010-11-10 15:47:29 -0600385 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
389
390 /**
391 * Assign file attachments
392 *
393 * @access public
394 * @param string
395 * @return void
396 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000397 public function attach($filename, $disposition = 'attachment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $this->_attach_name[] = $filename;
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600400 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Derek Jones37f4b9c2011-07-01 17:56:50 -0500401 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600402 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Add a Header Item
409 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600410 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param string
412 * @param string
413 * @return void
414 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600415 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 $this->_headers[$header] = $value;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
421
422 /**
423 * Convert a String to an Array
424 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600425 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @param string
427 * @return array
428 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600429 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 if ( ! is_array($email))
432 {
433 if (strpos($email, ',') !== FALSE)
434 {
435 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
436 }
437 else
438 {
439 $email = trim($email);
440 settype($email, "array");
441 }
442 }
443 return $email;
444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 // --------------------------------------------------------------------
447
448 /**
449 * Set Multipart Value
450 *
451 * @access public
452 * @param string
453 * @return void
454 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000455 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400457 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600458 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * Set Mailtype
465 *
466 * @access public
467 * @param string
468 * @return void
469 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000470 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600473 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * Set Wordwrap
480 *
481 * @access public
Dan Horrigan628e6602011-08-21 09:08:31 -0400482 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return void
484 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000485 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400487 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600488 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * Set Protocol
495 *
496 * @access public
497 * @param string
498 * @return void
499 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000500 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
502 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
Greg Akera769deb2010-11-10 15:47:29 -0600503 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
509 * Set Priority
510 *
511 * @access public
512 * @param integer
513 * @return void
514 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000515 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 {
517 if ( ! is_numeric($n))
518 {
519 $this->priority = 3;
520 return;
521 }
522
523 if ($n < 1 OR $n > 5)
524 {
525 $this->priority = 3;
526 return;
527 }
528
529 $this->priority = $n;
Greg Akera769deb2010-11-10 15:47:29 -0600530 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // --------------------------------------------------------------------
534
535 /**
536 * Set Newline Character
537 *
538 * @access public
539 * @param string
540 * @return void
541 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000542 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 {
544 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
545 {
546 $this->newline = "\n";
547 return;
548 }
549
550 $this->newline = $newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500551
Greg Akera769deb2010-11-10 15:47:29 -0600552 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * Set CRLF
559 *
560 * @access public
561 * @param string
562 * @return void
563 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000564 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
567 {
568 $this->crlf = "\n";
569 return;
570 }
571
572 $this->crlf = $crlf;
Eric Barnes6113f542010-12-29 13:36:12 -0500573
Greg Akera769deb2010-11-10 15:47:29 -0600574 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 // --------------------------------------------------------------------
578
579 /**
580 * Set Message Boundary
581 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600582 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @return void
584 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600585 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
588 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
589 }
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 // --------------------------------------------------------------------
592
593 /**
594 * Get the Message ID
595 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600596 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 * @return string
598 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600599 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
601 $from = $this->_headers['Return-Path'];
602 $from = str_replace(">", "", $from);
603 $from = str_replace("<", "", $from);
604
Derek Jones37f4b9c2011-07-01 17:56:50 -0500605 return "<".uniqid('').strstr($from, '@').">";
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 // --------------------------------------------------------------------
609
610 /**
611 * Get Mail Protocol
612 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600613 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @param bool
615 * @return string
616 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600617 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 $this->protocol = strtolower($this->protocol);
620 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
621
622 if ($return == TRUE)
623 {
624 return $this->protocol;
625 }
626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // --------------------------------------------------------------------
629
630 /**
631 * Get Mail Encoding
632 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600633 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param bool
635 * @return string
636 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600637 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 {
639 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
640
641 foreach ($this->_base_charsets as $charset)
642 {
643 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
644 {
645 $this->_encoding = '7bit';
646 }
647 }
648
649 if ($return == TRUE)
650 {
651 return $this->_encoding;
652 }
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Get content type (text/html/attachment)
659 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600660 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 * @return string
662 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600663 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500665 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 {
667 return 'html';
668 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500669 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
671 return 'html-attach';
672 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500673 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return 'plain-attach';
676 }
677 else
678 {
679 return 'plain';
680 }
681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 // --------------------------------------------------------------------
684
685 /**
686 * Set RFC 822 Date
687 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600688 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 * @return string
690 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600691 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 $timezone = date("Z");
694 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
695 $timezone = abs($timezone);
696 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
697
698 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
699 }
Barry Mienydd671972010-10-04 16:33:58 +0200700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 // --------------------------------------------------------------------
702
703 /**
704 * Mime message
705 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600706 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 * @return string
708 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600709 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 {
711 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
712 }
Barry Mienydd671972010-10-04 16:33:58 +0200713
Derek Allard2067d1a2008-11-13 22:59:24 +0000714 // --------------------------------------------------------------------
715
716 /**
717 * Validate Email Address
718 *
719 * @access public
720 * @param string
721 * @return bool
722 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000723 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 {
725 if ( ! is_array($email))
726 {
patworkb0707982011-04-08 15:10:05 +0200727 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 return FALSE;
729 }
730
731 foreach ($email as $val)
732 {
733 if ( ! $this->valid_email($val))
734 {
patworkb0707982011-04-08 15:10:05 +0200735 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 return FALSE;
737 }
738 }
739
740 return TRUE;
741 }
Barry Mienydd671972010-10-04 16:33:58 +0200742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 // --------------------------------------------------------------------
744
745 /**
746 * Email Validation
747 *
748 * @access public
749 * @param string
750 * @return bool
751 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000752 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
755 }
Barry Mienydd671972010-10-04 16:33:58 +0200756
Derek Allard2067d1a2008-11-13 22:59:24 +0000757 // --------------------------------------------------------------------
758
759 /**
760 * Clean Extended Email Address: Joe Smith <joe@smith.com>
761 *
762 * @access public
763 * @param string
764 * @return string
765 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000766 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000767 {
768 if ( ! is_array($email))
769 {
770 if (preg_match('/\<(.*)\>/', $email, $match))
771 {
Barry Mienydd671972010-10-04 16:33:58 +0200772 return $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 }
Barry Mienydd671972010-10-04 16:33:58 +0200774 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
Barry Mienydd671972010-10-04 16:33:58 +0200776 return $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 }
778 }
779
780 $clean_email = array();
781
782 foreach ($email as $addy)
783 {
784 if (preg_match( '/\<(.*)\>/', $addy, $match))
785 {
Barry Mienydd671972010-10-04 16:33:58 +0200786 $clean_email[] = $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 }
Barry Mienydd671972010-10-04 16:33:58 +0200788 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 {
Barry Mienydd671972010-10-04 16:33:58 +0200790 $clean_email[] = $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000791 }
792 }
793
794 return $clean_email;
795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 // --------------------------------------------------------------------
798
799 /**
800 * Build alternative plain text message
801 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000802 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 * in plain-text headers of HTML-formatted emails.
804 * If the user hasn't specified his own alternative message
805 * it creates one by stripping the HTML
806 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600807 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000808 * @return string
809 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600810 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 {
812 if ($this->alt_message != "")
813 {
814 return $this->word_wrap($this->alt_message, '76');
815 }
816
817 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
818 {
819 $body = $match['1'];
820 }
821 else
822 {
823 $body = $this->_body;
824 }
825
826 $body = trim(strip_tags($body));
827 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
828 $body = str_replace("\t", "", $body);
829
830 for ($i = 20; $i >= 3; $i--)
831 {
832 $n = "";
833
834 for ($x = 1; $x <= $i; $x ++)
835 {
Barry Mienydd671972010-10-04 16:33:58 +0200836 $n .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000837 }
838
839 $body = str_replace($n, "\n\n", $body);
840 }
841
842 return $this->word_wrap($body, '76');
843 }
Barry Mienydd671972010-10-04 16:33:58 +0200844
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 // --------------------------------------------------------------------
846
847 /**
848 * Word Wrap
849 *
850 * @access public
851 * @param string
852 * @param integer
853 * @return string
854 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000855 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 {
857 // Se the character limit
858 if ($charlim == '')
859 {
860 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
861 }
862
863 // Reduce multiple spaces
864 $str = preg_replace("| +|", " ", $str);
865
866 // Standardize newlines
867 if (strpos($str, "\r") !== FALSE)
868 {
869 $str = str_replace(array("\r\n", "\r"), "\n", $str);
870 }
871
872 // If the current word is surrounded by {unwrap} tags we'll
873 // strip the entire chunk and replace it with a marker.
874 $unwrap = array();
875 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
876 {
877 for ($i = 0; $i < count($matches['0']); $i++)
878 {
879 $unwrap[] = $matches['1'][$i];
880 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
881 }
882 }
883
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000884 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500886 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 $str = wordwrap($str, $charlim, "\n", FALSE);
888
889 // Split the string into individual lines of text and cycle through them
890 $output = "";
891 foreach (explode("\n", $str) as $line)
892 {
893 // Is the line within the allowed character count?
894 // If so we'll join it to the output and continue
895 if (strlen($line) <= $charlim)
896 {
897 $output .= $line.$this->newline;
898 continue;
899 }
900
901 $temp = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500902 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000903 {
904 // If the over-length word is a URL we won't wrap it
905 if (preg_match("!\[url.+\]|://|wwww.!", $line))
906 {
907 break;
908 }
909
910 // Trim the word down
911 $temp .= substr($line, 0, $charlim-1);
912 $line = substr($line, $charlim-1);
913 }
914
915 // If $temp contains data it means we had to split up an over-length
916 // word into smaller chunks so we'll add it back to our current line
917 if ($temp != '')
918 {
919 $output .= $temp.$this->newline.$line;
920 }
921 else
922 {
923 $output .= $line;
924 }
925
926 $output .= $this->newline;
927 }
928
929 // Put our markers back
930 if (count($unwrap) > 0)
931 {
932 foreach ($unwrap as $key => $val)
933 {
934 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
935 }
936 }
937
938 return $output;
939 }
Barry Mienydd671972010-10-04 16:33:58 +0200940
Derek Allard2067d1a2008-11-13 22:59:24 +0000941 // --------------------------------------------------------------------
942
943 /**
944 * Build final headers
945 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600946 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 * @param string
948 * @return string
949 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600950 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 {
952 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
953 $this->_set_header('X-Mailer', $this->useragent);
954 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
955 $this->_set_header('Message-ID', $this->_get_message_id());
956 $this->_set_header('Mime-Version', '1.0');
957 }
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // --------------------------------------------------------------------
960
961 /**
962 * Write Headers as a string
963 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600964 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 * @return void
966 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600967 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000968 {
969 if ($this->protocol == 'mail')
970 {
971 $this->_subject = $this->_headers['Subject'];
972 unset($this->_headers['Subject']);
973 }
974
975 reset($this->_headers);
976 $this->_header_str = "";
977
Pascal Kriete14287f32011-02-14 13:39:34 -0500978 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000979 {
980 $val = trim($val);
981
982 if ($val != "")
983 {
984 $this->_header_str .= $key.": ".$val.$this->newline;
985 }
986 }
987
988 if ($this->_get_protocol() == 'mail')
989 {
Derek Jones1d890882009-02-10 20:32:14 +0000990 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 }
992 }
Barry Mienydd671972010-10-04 16:33:58 +0200993
Derek Allard2067d1a2008-11-13 22:59:24 +0000994 // --------------------------------------------------------------------
995
996 /**
997 * Build Final Body and attachments
998 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600999 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 * @return void
1001 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001002 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001004 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001005 {
1006 $this->_body = $this->word_wrap($this->_body);
1007 }
1008
1009 $this->_set_boundaries();
1010 $this->_write_headers();
1011
1012 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001013 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001014
1015 switch ($this->_get_content_type())
1016 {
1017 case 'plain' :
1018
1019 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1020 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1021
1022 if ($this->_get_protocol() == 'mail')
1023 {
1024 $this->_header_str .= $hdr;
1025 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
Brandon Jones485d7412010-11-09 16:38:17 -05001027 else
1028 {
1029 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1030 }
Eric Barnes6113f542010-12-29 13:36:12 -05001031
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 return;
1033
1034 break;
1035 case 'html' :
1036
1037 if ($this->send_multipart === FALSE)
1038 {
1039 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1040 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1041 }
1042 else
1043 {
Derek Jonesa45e7612009-02-10 18:33:01 +00001044 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001045
Brandon Jones485d7412010-11-09 16:38:17 -05001046 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1047 $body .= "--" . $this->_alt_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001048
Brandon Jones485d7412010-11-09 16:38:17 -05001049 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1050 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1051 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1052
1053 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1054 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001055 }
Eric Barnes6113f542010-12-29 13:36:12 -05001056
Brandon Jones485d7412010-11-09 16:38:17 -05001057 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001058
1059
Derek Allard2067d1a2008-11-13 22:59:24 +00001060 if ($this->_get_protocol() == 'mail')
1061 {
1062 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001063 }
1064 else
1065 {
1066 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 }
1068
Derek Allard2067d1a2008-11-13 22:59:24 +00001069
1070 if ($this->send_multipart !== FALSE)
1071 {
Brandon Jones485d7412010-11-09 16:38:17 -05001072 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
1074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 return;
1076
1077 break;
1078 case 'plain-attach' :
1079
Derek Jonesa45e7612009-02-10 18:33:01 +00001080 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001081
1082 if ($this->_get_protocol() == 'mail')
1083 {
1084 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001085 }
1086
Brandon Jones485d7412010-11-09 16:38:17 -05001087 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1088 $body .= "--" . $this->_atc_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
Brandon Jones485d7412010-11-09 16:38:17 -05001090 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1091 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001092
Brandon Jones485d7412010-11-09 16:38:17 -05001093 $body .= $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001094
1095 break;
1096 case 'html-attach' :
1097
Derek Jonesa45e7612009-02-10 18:33:01 +00001098 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001099
Derek Allard2067d1a2008-11-13 22:59:24 +00001100 if ($this->_get_protocol() == 'mail')
1101 {
1102 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 }
1104
Brandon Jones485d7412010-11-09 16:38:17 -05001105 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1106 $body .= "--" . $this->_atc_boundary . $this->newline;
1107
1108 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1109 $body .= "--" . $this->_alt_boundary . $this->newline;
1110
1111 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1112 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1113 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1114
1115 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1116 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
1117
1118 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1119 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001120
1121 break;
1122 }
1123
1124 $attachment = array();
1125
1126 $z = 0;
1127
1128 for ($i=0; $i < count($this->_attach_name); $i++)
1129 {
1130 $filename = $this->_attach_name[$i];
1131 $basename = basename($filename);
1132 $ctype = $this->_attach_type[$i];
1133
1134 if ( ! file_exists($filename))
1135 {
patworkb0707982011-04-08 15:10:05 +02001136 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001137 return FALSE;
1138 }
1139
Derek Jones37f4b9c2011-07-01 17:56:50 -05001140 $h = "--".$this->_atc_boundary.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001141 $h .= "Content-type: ".$ctype."; ";
1142 $h .= "name=\"".$basename."\"".$this->newline;
1143 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1144 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1145
1146 $attachment[$z++] = $h;
1147 $file = filesize($filename) +1;
1148
1149 if ( ! $fp = fopen($filename, FOPEN_READ))
1150 {
patworkb0707982011-04-08 15:10:05 +02001151 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001152 return FALSE;
1153 }
1154
1155 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1156 fclose($fp);
1157 }
1158
Brandon Jones485d7412010-11-09 16:38:17 -05001159 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Eric Barnes6113f542010-12-29 13:36:12 -05001160
Brandon Jones485d7412010-11-09 16:38:17 -05001161
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 if ($this->_get_protocol() == 'mail')
1163 {
Brandon Jones485d7412010-11-09 16:38:17 -05001164 $this->_finalbody = $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
Brandon Jones485d7412010-11-09 16:38:17 -05001166 else
1167 {
1168 $this->_finalbody = $hdr . $body;
1169 }
Eric Barnes6113f542010-12-29 13:36:12 -05001170
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 return;
1172 }
Barry Mienydd671972010-10-04 16:33:58 +02001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 // --------------------------------------------------------------------
1175
1176 /**
1177 * Prep Quoted Printable
1178 *
1179 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1180 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1181 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001182 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 * @param string
1184 * @param integer
1185 * @return string
1186 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001187 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001188 {
1189 // Set the character limit
1190 // Don't allow over 76, as that will make servers and MUAs barf
1191 // all over quoted-printable data
1192 if ($charlim == '' OR $charlim > '76')
1193 {
1194 $charlim = '76';
1195 }
1196
1197 // Reduce multiple spaces
1198 $str = preg_replace("| +|", " ", $str);
1199
1200 // kill nulls
1201 $str = preg_replace('/\x00+/', '', $str);
1202
1203 // Standardize newlines
1204 if (strpos($str, "\r") !== FALSE)
1205 {
1206 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1207 }
1208
1209 // We are intentionally wrapping so mail servers will encode characters
1210 // properly and MUAs will behave, so {unwrap} must go!
1211 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1212
1213 // Break into an array of lines
1214 $lines = explode("\n", $str);
1215
1216 $escape = '=';
1217 $output = '';
1218
1219 foreach ($lines as $line)
1220 {
1221 $length = strlen($line);
1222 $temp = '';
1223
1224 // Loop through each character in the line to add soft-wrap
1225 // characters at the end of a line " =\r\n" and add the newly
1226 // processed line(s) to the output (see comment on $crlf class property)
1227 for ($i = 0; $i < $length; $i++)
1228 {
1229 // Grab the next character
1230 $char = substr($line, $i, 1);
1231 $ascii = ord($char);
1232
1233 // Convert spaces and tabs but only if it's the end of the line
1234 if ($i == ($length - 1))
1235 {
1236 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1237 }
1238
1239 // encode = signs
1240 if ($ascii == '61')
1241 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001242 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001243 }
1244
1245 // If we're at the character limit, add the line to the output,
1246 // reset our temp variable, and keep on chuggin'
1247 if ((strlen($temp) + strlen($char)) >= $charlim)
1248 {
1249 $output .= $temp.$escape.$this->crlf;
1250 $temp = '';
1251 }
1252
1253 // Add the character to our temporary line
1254 $temp .= $char;
1255 }
1256
1257 // Add our completed line to the output
1258 $output .= $temp.$this->crlf;
1259 }
1260
1261 // get rid of extra CRLF tacked onto the end
1262 $output = substr($output, 0, strlen($this->crlf) * -1);
1263
1264 return $output;
1265 }
1266
1267 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001268
Derek Allard2067d1a2008-11-13 22:59:24 +00001269 /**
1270 * Prep Q Encoding
1271 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001272 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 * but not identical to quoted-printable, so it has its own method
1274 *
1275 * @access public
1276 * @param str
1277 * @param bool // set to TRUE for processing From: headers
1278 * @return str
1279 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001280 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 {
1282 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1283
1284 // Line length must not exceed 76 characters, so we adjust for
1285 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1286 $limit = 75 - 7 - strlen($this->charset);
1287
1288 // these special characters must be converted too
1289 $convert = array('_', '=', '?');
1290
1291 if ($from === TRUE)
1292 {
1293 $convert[] = ',';
1294 $convert[] = ';';
1295 }
1296
1297 $output = '';
1298 $temp = '';
1299
1300 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1301 {
1302 // Grab the next character
1303 $char = substr($str, $i, 1);
1304 $ascii = ord($char);
1305
1306 // convert ALL non-printable ASCII characters and our specials
1307 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1308 {
1309 $char = '='.dechex($ascii);
1310 }
1311
1312 // handle regular spaces a bit more compactly than =20
1313 if ($ascii == 32)
1314 {
1315 $char = '_';
1316 }
1317
1318 // If we're at the character limit, add the line to the output,
1319 // reset our temp variable, and keep on chuggin'
1320 if ((strlen($temp) + strlen($char)) >= $limit)
1321 {
1322 $output .= $temp.$this->crlf;
1323 $temp = '';
1324 }
1325
1326 // Add the character to our temporary line
1327 $temp .= $char;
1328 }
1329
1330 $str = $output.$temp;
1331
1332 // wrap each line with the shebang, charset, and transfer encoding
1333 // the preceding space on successive lines is required for header "folding"
1334 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
1335
1336 return $str;
1337 }
1338
1339 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001340
Derek Allard2067d1a2008-11-13 22:59:24 +00001341 /**
1342 * Send Email
1343 *
1344 * @access public
1345 * @return bool
1346 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001347 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001348 {
1349 if ($this->_replyto_flag == FALSE)
1350 {
1351 $this->reply_to($this->_headers['From']);
1352 }
1353
Derek Jones37f4b9c2011-07-01 17:56:50 -05001354 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1356 ( ! isset($this->_headers['Cc'])))
1357 {
patworkb0707982011-04-08 15:10:05 +02001358 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 return FALSE;
1360 }
1361
1362 $this->_build_headers();
1363
Derek Jones37f4b9c2011-07-01 17:56:50 -05001364 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 {
1366 if (count($this->_bcc_array) > $this->bcc_batch_size)
1367 return $this->batch_bcc_send();
1368 }
1369
1370 $this->_build_message();
1371
1372 if ( ! $this->_spool_email())
1373 {
1374 return FALSE;
1375 }
1376 else
1377 {
1378 return TRUE;
1379 }
1380 }
Barry Mienydd671972010-10-04 16:33:58 +02001381
Derek Allard2067d1a2008-11-13 22:59:24 +00001382 // --------------------------------------------------------------------
1383
1384 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001385 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001386 *
1387 * @access public
1388 * @return bool
1389 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001390 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001391 {
1392 $float = $this->bcc_batch_size -1;
1393
1394 $set = "";
1395
1396 $chunk = array();
1397
1398 for ($i = 0; $i < count($this->_bcc_array); $i++)
1399 {
1400 if (isset($this->_bcc_array[$i]))
1401 {
1402 $set .= ", ".$this->_bcc_array[$i];
1403 }
1404
1405 if ($i == $float)
1406 {
1407 $chunk[] = substr($set, 1);
1408 $float = $float + $this->bcc_batch_size;
1409 $set = "";
1410 }
1411
1412 if ($i == count($this->_bcc_array)-1)
1413 {
1414 $chunk[] = substr($set, 1);
1415 }
1416 }
1417
1418 for ($i = 0; $i < count($chunk); $i++)
1419 {
1420 unset($this->_headers['Bcc']);
1421 unset($bcc);
1422
1423 $bcc = $this->_str_to_array($chunk[$i]);
1424 $bcc = $this->clean_email($bcc);
1425
1426 if ($this->protocol != 'smtp')
1427 {
1428 $this->_set_header('Bcc', implode(", ", $bcc));
1429 }
1430 else
1431 {
1432 $this->_bcc_array = $bcc;
1433 }
1434
1435 $this->_build_message();
1436 $this->_spool_email();
1437 }
1438 }
Barry Mienydd671972010-10-04 16:33:58 +02001439
Derek Allard2067d1a2008-11-13 22:59:24 +00001440 // --------------------------------------------------------------------
1441
1442 /**
1443 * Unwrap special elements
1444 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001445 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 * @return void
1447 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001448 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001449 {
1450 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1451 }
Barry Mienydd671972010-10-04 16:33:58 +02001452
Derek Allard2067d1a2008-11-13 22:59:24 +00001453 // --------------------------------------------------------------------
1454
1455 /**
1456 * Strip line-breaks via callback
1457 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001458 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 * @return string
1460 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001461 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001462 {
1463 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1464 {
1465 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1466 }
1467
1468 return $matches[1];
1469 }
Barry Mienydd671972010-10-04 16:33:58 +02001470
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 // --------------------------------------------------------------------
1472
1473 /**
1474 * Spool mail to the mail server
1475 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001476 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 * @return bool
1478 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001479 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001480 {
1481 $this->_unwrap_specials();
1482
1483 switch ($this->_get_protocol())
1484 {
1485 case 'mail' :
1486
1487 if ( ! $this->_send_with_mail())
1488 {
patworkb0707982011-04-08 15:10:05 +02001489 $this->_set_error_message('lang:email_send_failure_phpmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001490 return FALSE;
1491 }
1492 break;
1493 case 'sendmail' :
1494
1495 if ( ! $this->_send_with_sendmail())
1496 {
patworkb0707982011-04-08 15:10:05 +02001497 $this->_set_error_message('lang:email_send_failure_sendmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 return FALSE;
1499 }
1500 break;
1501 case 'smtp' :
1502
1503 if ( ! $this->_send_with_smtp())
1504 {
patworkb0707982011-04-08 15:10:05 +02001505 $this->_set_error_message('lang:email_send_failure_smtp');
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 return FALSE;
1507 }
1508 break;
1509
1510 }
1511
patworkb0707982011-04-08 15:10:05 +02001512 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001513 return TRUE;
1514 }
Barry Mienydd671972010-10-04 16:33:58 +02001515
Derek Allard2067d1a2008-11-13 22:59:24 +00001516 // --------------------------------------------------------------------
1517
1518 /**
1519 * Send using mail()
1520 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001521 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 * @return bool
1523 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001524 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 {
1526 if ($this->_safe_mode == TRUE)
1527 {
1528 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1529 {
1530 return FALSE;
1531 }
1532 else
1533 {
1534 return TRUE;
1535 }
1536 }
1537 else
1538 {
1539 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1540 // we've encountered servers that seem to require it to be in place.
Eric Barnes6113f542010-12-29 13:36:12 -05001541
Derek Allard2067d1a2008-11-13 22:59:24 +00001542 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1543 {
1544 return FALSE;
1545 }
1546 else
1547 {
1548 return TRUE;
1549 }
1550 }
1551 }
Barry Mienydd671972010-10-04 16:33:58 +02001552
Derek Allard2067d1a2008-11-13 22:59:24 +00001553 // --------------------------------------------------------------------
1554
1555 /**
1556 * Send using Sendmail
1557 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001558 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 * @return bool
1560 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001561 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001562 {
1563 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1564
Derek Jones4cefaa42009-04-29 19:13:56 +00001565 if ($fp === FALSE OR $fp === NULL)
1566 {
1567 // server probably has popen disabled, so nothing we can do to get a verbose error.
1568 return FALSE;
1569 }
Derek Jones71141ce2010-03-02 16:41:20 -06001570
Derek Jonesc630bcf2008-11-17 21:09:45 +00001571 fputs($fp, $this->_header_str);
1572 fputs($fp, $this->_finalbody);
1573
Barry Mienydd671972010-10-04 16:33:58 +02001574 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001575
Derek Jonesc630bcf2008-11-17 21:09:45 +00001576 if (version_compare(PHP_VERSION, '4.2.3') == -1)
1577 {
1578 $status = $status >> 8 & 0xFF;
Barry Mienydd671972010-10-04 16:33:58 +02001579 }
Derek Jones71141ce2010-03-02 16:41:20 -06001580
Derek Jones604873f2008-11-18 15:57:24 +00001581 if ($status != 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001582 {
patworkb0707982011-04-08 15:10:05 +02001583 $this->_set_error_message('lang:email_exit_status', $status);
1584 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001585 return FALSE;
1586 }
1587
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 return TRUE;
1589 }
Barry Mienydd671972010-10-04 16:33:58 +02001590
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 // --------------------------------------------------------------------
1592
1593 /**
1594 * Send using SMTP
1595 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001596 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 * @return bool
1598 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001599 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 {
1601 if ($this->smtp_host == '')
1602 {
patworkb0707982011-04-08 15:10:05 +02001603 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001604 return FALSE;
1605 }
1606
1607 $this->_smtp_connect();
1608 $this->_smtp_authenticate();
1609
1610 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1611
Pascal Kriete14287f32011-02-14 13:39:34 -05001612 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001613 {
1614 $this->_send_command('to', $val);
1615 }
1616
1617 if (count($this->_cc_array) > 0)
1618 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001619 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001620 {
1621 if ($val != "")
1622 {
1623 $this->_send_command('to', $val);
1624 }
1625 }
1626 }
1627
1628 if (count($this->_bcc_array) > 0)
1629 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001630 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001631 {
1632 if ($val != "")
1633 {
1634 $this->_send_command('to', $val);
1635 }
1636 }
1637 }
1638
1639 $this->_send_command('data');
1640
1641 // perform dot transformation on any lines that begin with a dot
1642 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1643
1644 $this->_send_data('.');
1645
1646 $reply = $this->_get_smtp_data();
1647
1648 $this->_set_error_message($reply);
1649
1650 if (strncmp($reply, '250', 3) != 0)
1651 {
patworkb0707982011-04-08 15:10:05 +02001652 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001653 return FALSE;
1654 }
1655
1656 $this->_send_command('quit');
1657 return TRUE;
1658 }
Barry Mienydd671972010-10-04 16:33:58 +02001659
Derek Allard2067d1a2008-11-13 22:59:24 +00001660 // --------------------------------------------------------------------
1661
1662 /**
1663 * SMTP Connect
1664 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001665 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 * @param string
1667 * @return string
1668 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001669 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001670 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001671 $ssl = NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001672
Radu Potopbbf04b02011-09-28 13:57:51 +03001673 if ($this->smtp_crypto == 'ssl')
Radu Potop4c589ae2011-09-29 10:19:55 +03001674 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001675 $ssl = 'ssl://';
Radu Potop4c589ae2011-09-29 10:19:55 +03001676 }
1677
Radu Potopbbf04b02011-09-28 13:57:51 +03001678 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001679 $this->smtp_port,
1680 $errno,
1681 $errstr,
1682 $this->smtp_timeout);
1683
Pascal Kriete14287f32011-02-14 13:39:34 -05001684 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001685 {
patworkb0707982011-04-08 15:10:05 +02001686 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 return FALSE;
1688 }
1689
1690 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001691
1692 if ($this->smtp_crypto == 'tls')
1693 {
1694 $this->_send_command('hello');
1695 $this->_send_command('starttls');
Radu Potop4c589ae2011-09-29 10:19:55 +03001696 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1697 }
1698
1699 if ($crypto !== TRUE)
1700 {
1701 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1702 return FALSE;
Radu Potopbbf04b02011-09-28 13:57:51 +03001703 }
1704
Derek Allard2067d1a2008-11-13 22:59:24 +00001705 return $this->_send_command('hello');
1706 }
Barry Mienydd671972010-10-04 16:33:58 +02001707
Derek Allard2067d1a2008-11-13 22:59:24 +00001708 // --------------------------------------------------------------------
1709
1710 /**
1711 * Send SMTP command
1712 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001713 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001714 * @param string
1715 * @param string
1716 * @return string
1717 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001718 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001719 {
1720 switch ($cmd)
1721 {
1722 case 'hello' :
1723
1724 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1725 $this->_send_data('EHLO '.$this->_get_hostname());
1726 else
1727 $this->_send_data('HELO '.$this->_get_hostname());
1728
1729 $resp = 250;
1730 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001731 case 'starttls' :
1732
1733 $this->_send_data('STARTTLS');
1734
1735 $resp = 220;
1736 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001737 case 'from' :
1738
1739 $this->_send_data('MAIL FROM:<'.$data.'>');
1740
1741 $resp = 250;
1742 break;
1743 case 'to' :
1744
1745 $this->_send_data('RCPT TO:<'.$data.'>');
1746
1747 $resp = 250;
1748 break;
1749 case 'data' :
1750
1751 $this->_send_data('DATA');
1752
1753 $resp = 354;
1754 break;
1755 case 'quit' :
1756
1757 $this->_send_data('QUIT');
1758
1759 $resp = 221;
1760 break;
1761 }
1762
1763 $reply = $this->_get_smtp_data();
1764
1765 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1766
1767 if (substr($reply, 0, 3) != $resp)
1768 {
patworkb0707982011-04-08 15:10:05 +02001769 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001770 return FALSE;
1771 }
1772
1773 if ($cmd == 'quit')
1774 {
1775 fclose($this->_smtp_connect);
1776 }
1777
1778 return TRUE;
1779 }
Barry Mienydd671972010-10-04 16:33:58 +02001780
Derek Allard2067d1a2008-11-13 22:59:24 +00001781 // --------------------------------------------------------------------
1782
1783 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001784 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001785 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001786 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001787 * @return bool
1788 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001789 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001790 {
1791 if ( ! $this->_smtp_auth)
1792 {
1793 return TRUE;
1794 }
1795
Derek Jones37f4b9c2011-07-01 17:56:50 -05001796 if ($this->smtp_user == "" AND $this->smtp_pass == "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 {
patworkb0707982011-04-08 15:10:05 +02001798 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 return FALSE;
1800 }
1801
1802 $this->_send_data('AUTH LOGIN');
1803
1804 $reply = $this->_get_smtp_data();
1805
1806 if (strncmp($reply, '334', 3) != 0)
1807 {
patworkb0707982011-04-08 15:10:05 +02001808 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 return FALSE;
1810 }
1811
1812 $this->_send_data(base64_encode($this->smtp_user));
1813
1814 $reply = $this->_get_smtp_data();
1815
1816 if (strncmp($reply, '334', 3) != 0)
1817 {
patworkb0707982011-04-08 15:10:05 +02001818 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001819 return FALSE;
1820 }
1821
1822 $this->_send_data(base64_encode($this->smtp_pass));
1823
1824 $reply = $this->_get_smtp_data();
1825
1826 if (strncmp($reply, '235', 3) != 0)
1827 {
patworkb0707982011-04-08 15:10:05 +02001828 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001829 return FALSE;
1830 }
1831
1832 return TRUE;
1833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 // --------------------------------------------------------------------
1836
1837 /**
1838 * Send SMTP data
1839 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001840 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 * @return bool
1842 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001843 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
1845 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1846 {
patworkb0707982011-04-08 15:10:05 +02001847 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001848 return FALSE;
1849 }
1850 else
1851 {
1852 return TRUE;
1853 }
1854 }
Barry Mienydd671972010-10-04 16:33:58 +02001855
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 // --------------------------------------------------------------------
1857
1858 /**
1859 * Get SMTP data
1860 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001861 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001862 * @return string
1863 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001864 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001865 {
1866 $data = "";
1867
1868 while ($str = fgets($this->_smtp_connect, 512))
1869 {
1870 $data .= $str;
1871
1872 if (substr($str, 3, 1) == " ")
1873 {
1874 break;
1875 }
1876 }
1877
1878 return $data;
1879 }
Barry Mienydd671972010-10-04 16:33:58 +02001880
Derek Allard2067d1a2008-11-13 22:59:24 +00001881 // --------------------------------------------------------------------
1882
1883 /**
1884 * Get Hostname
1885 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001886 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001887 * @return string
1888 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001889 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001890 {
1891 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1892 }
Barry Mienydd671972010-10-04 16:33:58 +02001893
Derek Allard2067d1a2008-11-13 22:59:24 +00001894 // --------------------------------------------------------------------
1895
1896 /**
1897 * Get IP
1898 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001899 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001900 * @return string
1901 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001902 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001903 {
1904 if ($this->_IP !== FALSE)
1905 {
1906 return $this->_IP;
1907 }
1908
1909 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1910 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1911 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1912
Barry Mienydd671972010-10-04 16:33:58 +02001913 if ($cip && $rip) $this->_IP = $cip;
Derek Allard2067d1a2008-11-13 22:59:24 +00001914 elseif ($rip) $this->_IP = $rip;
1915 elseif ($cip) $this->_IP = $cip;
1916 elseif ($fip) $this->_IP = $fip;
1917
Robin Sowell76b369e2010-03-19 11:15:28 -04001918 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001919 {
1920 $x = explode(',', $this->_IP);
1921 $this->_IP = end($x);
1922 }
1923
1924 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1925 {
1926 $this->_IP = '0.0.0.0';
1927 }
1928
1929 unset($cip);
1930 unset($rip);
1931 unset($fip);
1932
1933 return $this->_IP;
1934 }
Barry Mienydd671972010-10-04 16:33:58 +02001935
Derek Allard2067d1a2008-11-13 22:59:24 +00001936 // --------------------------------------------------------------------
1937
1938 /**
1939 * Get Debug Message
1940 *
1941 * @access public
1942 * @return string
1943 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001944 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 {
1946 $msg = '';
1947
1948 if (count($this->_debug_msg) > 0)
1949 {
1950 foreach ($this->_debug_msg as $val)
1951 {
1952 $msg .= $val;
1953 }
1954 }
1955
1956 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1957 return $msg;
1958 }
Barry Mienydd671972010-10-04 16:33:58 +02001959
Derek Allard2067d1a2008-11-13 22:59:24 +00001960 // --------------------------------------------------------------------
1961
1962 /**
1963 * Set Message
1964 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001965 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001966 * @param string
1967 * @return string
1968 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001969 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001970 {
1971 $CI =& get_instance();
1972 $CI->lang->load('email');
1973
patworkb0707982011-04-08 15:10:05 +02001974 if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001975 {
1976 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1977 }
1978 else
1979 {
1980 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1981 }
1982 }
Barry Mienydd671972010-10-04 16:33:58 +02001983
Derek Allard2067d1a2008-11-13 22:59:24 +00001984 // --------------------------------------------------------------------
1985
1986 /**
1987 * Mime Types
1988 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001989 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001990 * @param string
1991 * @return string
1992 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001993 protected function _mime_types($ext = "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001994 {
1995 $mimes = array( 'hqx' => 'application/mac-binhex40',
1996 'cpt' => 'application/mac-compactpro',
1997 'doc' => 'application/msword',
1998 'bin' => 'application/macbinary',
1999 'dms' => 'application/octet-stream',
2000 'lha' => 'application/octet-stream',
2001 'lzh' => 'application/octet-stream',
2002 'exe' => 'application/octet-stream',
2003 'class' => 'application/octet-stream',
2004 'psd' => 'application/octet-stream',
2005 'so' => 'application/octet-stream',
2006 'sea' => 'application/octet-stream',
2007 'dll' => 'application/octet-stream',
2008 'oda' => 'application/oda',
2009 'pdf' => 'application/pdf',
2010 'ai' => 'application/postscript',
2011 'eps' => 'application/postscript',
2012 'ps' => 'application/postscript',
2013 'smi' => 'application/smil',
2014 'smil' => 'application/smil',
2015 'mif' => 'application/vnd.mif',
2016 'xls' => 'application/vnd.ms-excel',
2017 'ppt' => 'application/vnd.ms-powerpoint',
2018 'wbxml' => 'application/vnd.wap.wbxml',
2019 'wmlc' => 'application/vnd.wap.wmlc',
2020 'dcr' => 'application/x-director',
2021 'dir' => 'application/x-director',
2022 'dxr' => 'application/x-director',
2023 'dvi' => 'application/x-dvi',
2024 'gtar' => 'application/x-gtar',
2025 'php' => 'application/x-httpd-php',
2026 'php4' => 'application/x-httpd-php',
2027 'php3' => 'application/x-httpd-php',
2028 'phtml' => 'application/x-httpd-php',
2029 'phps' => 'application/x-httpd-php-source',
2030 'js' => 'application/x-javascript',
2031 'swf' => 'application/x-shockwave-flash',
2032 'sit' => 'application/x-stuffit',
2033 'tar' => 'application/x-tar',
2034 'tgz' => 'application/x-tar',
2035 'xhtml' => 'application/xhtml+xml',
2036 'xht' => 'application/xhtml+xml',
2037 'zip' => 'application/zip',
2038 'mid' => 'audio/midi',
2039 'midi' => 'audio/midi',
2040 'mpga' => 'audio/mpeg',
2041 'mp2' => 'audio/mpeg',
2042 'mp3' => 'audio/mpeg',
2043 'aif' => 'audio/x-aiff',
2044 'aiff' => 'audio/x-aiff',
2045 'aifc' => 'audio/x-aiff',
2046 'ram' => 'audio/x-pn-realaudio',
2047 'rm' => 'audio/x-pn-realaudio',
2048 'rpm' => 'audio/x-pn-realaudio-plugin',
2049 'ra' => 'audio/x-realaudio',
2050 'rv' => 'video/vnd.rn-realvideo',
2051 'wav' => 'audio/x-wav',
2052 'bmp' => 'image/bmp',
2053 'gif' => 'image/gif',
2054 'jpeg' => 'image/jpeg',
2055 'jpg' => 'image/jpeg',
2056 'jpe' => 'image/jpeg',
2057 'png' => 'image/png',
2058 'tiff' => 'image/tiff',
2059 'tif' => 'image/tiff',
2060 'css' => 'text/css',
2061 'html' => 'text/html',
2062 'htm' => 'text/html',
2063 'shtml' => 'text/html',
2064 'txt' => 'text/plain',
2065 'text' => 'text/plain',
2066 'log' => 'text/plain',
2067 'rtx' => 'text/richtext',
2068 'rtf' => 'text/rtf',
2069 'xml' => 'text/xml',
2070 'xsl' => 'text/xml',
2071 'mpeg' => 'video/mpeg',
2072 'mpg' => 'video/mpeg',
2073 'mpe' => 'video/mpeg',
2074 'qt' => 'video/quicktime',
2075 'mov' => 'video/quicktime',
2076 'avi' => 'video/x-msvideo',
2077 'movie' => 'video/x-sgi-movie',
2078 'doc' => 'application/msword',
2079 'word' => 'application/msword',
2080 'xl' => 'application/excel',
2081 'eml' => 'message/rfc822'
2082 );
2083
2084 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2085 }
2086
2087}
2088// END CI_Email class
2089
2090/* End of file Email.php */
patworkb0707982011-04-08 15:10:05 +02002091/* Location: ./system/libraries/Email.php */