blob: 5f8d486827892caaeb21a6341a00d8c9c1c8b6ee [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
Derek Jones37f4b9c2011-07-01 17:56:50 -050039 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $wrapchars = "76"; // Number of characters to wrap at.
Derek Jones37f4b9c2011-07-01 17:56:50 -050041 var $mailtype = "text"; // text/html Defines email formatting
Derek Allard2067d1a2008-11-13 22:59:24 +000042 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
43 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
44 var $alt_message = ''; // Alternative message for HTML emails
Derek Jones37f4b9c2011-07-01 17:56:50 -050045 var $validate = FALSE; // TRUE/FALSE. Enables email validation
Derek Allard2067d1a2008-11-13 22:59:24 +000046 var $priority = "3"; // Default priority (1 - 5)
47 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
Derek Jones37f4b9c2011-07-01 17:56:50 -050048 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000049 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
50 // distasteful, is the only thing that seems to work for all environments.
Derek Jones37f4b9c2011-07-01 17:56:50 -050051 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
52 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
Derek Allard2067d1a2008-11-13 22:59:24 +000053 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
54 var $_safe_mode = FALSE;
55 var $_subject = "";
56 var $_body = "";
57 var $_finalbody = "";
58 var $_alt_boundary = "";
59 var $_atc_boundary = "";
60 var $_header_str = "";
61 var $_smtp_connect = "";
62 var $_encoding = "8bit";
63 var $_IP = FALSE;
64 var $_smtp_auth = FALSE;
65 var $_replyto_flag = FALSE;
66 var $_debug_msg = array();
67 var $_recipients = array();
68 var $_cc_array = array();
69 var $_bcc_array = array();
70 var $_headers = array();
71 var $_attach_name = array();
72 var $_attach_type = array();
73 var $_attach_disp = array();
74 var $_protocols = array('mail', 'sendmail', 'smtp');
75 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
76 var $_bit_depths = array('7bit', '8bit');
77 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
78
79
80 /**
81 * Constructor - Sets Email Preferences
82 *
83 * The constructor can be passed an array of config values
84 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000085 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 if (count($config) > 0)
88 {
89 $this->initialize($config);
90 }
91 else
92 {
93 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
94 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
95 }
96
97 log_message('debug', "Email Class Initialized");
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Initialize preferences
104 *
105 * @access public
106 * @param array
107 * @return void
108 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000109 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 foreach ($config as $key => $val)
112 {
113 if (isset($this->$key))
114 {
115 $method = 'set_'.$key;
116
117 if (method_exists($this, $method))
118 {
119 $this->$method($val);
120 }
121 else
122 {
123 $this->$key = $val;
124 }
125 }
126 }
Eric Barnes6113f542010-12-29 13:36:12 -0500127 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000128
129 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
130 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000131
132 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
138 * Initialize the Email Data
139 *
140 * @access public
141 * @return void
142 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000143 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 $this->_subject = "";
146 $this->_body = "";
147 $this->_finalbody = "";
148 $this->_header_str = "";
149 $this->_replyto_flag = FALSE;
150 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500151 $this->_cc_array = array();
152 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $this->_headers = array();
154 $this->_debug_msg = array();
155
156 $this->_set_header('User-Agent', $this->useragent);
157 $this->_set_header('Date', $this->_set_date());
158
159 if ($clear_attachments !== FALSE)
160 {
161 $this->_attach_name = array();
162 $this->_attach_type = array();
163 $this->_attach_disp = array();
164 }
Eric Barnes6113f542010-12-29 13:36:12 -0500165
Greg Akera769deb2010-11-10 15:47:29 -0600166 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
170
171 /**
172 * Set FROM
173 *
174 * @access public
175 * @param string
176 * @param string
177 * @return void
178 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000179 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 if (preg_match( '/\<(.*)\>/', $from, $match))
182 {
183 $from = $match['1'];
184 }
185
186 if ($this->validate)
187 {
188 $this->validate_email($this->_str_to_array($from));
189 }
190
191 // prepare the display name
192 if ($name != '')
193 {
194 // only use Q encoding if there are characters that would require it
195 if ( ! preg_match('/[\200-\377]/', $name))
196 {
197 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000198 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
200 else
201 {
202 $name = $this->_prep_q_encoding($name, TRUE);
203 }
204 }
205
206 $this->_set_header('From', $name.' <'.$from.'>');
207 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500208
Greg Akera769deb2010-11-10 15:47:29 -0600209 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // --------------------------------------------------------------------
213
214 /**
215 * Set Reply-to
216 *
217 * @access public
218 * @param string
219 * @param string
220 * @return void
221 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000222 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 if (preg_match( '/\<(.*)\>/', $replyto, $match))
225 {
226 $replyto = $match['1'];
227 }
228
229 if ($this->validate)
230 {
231 $this->validate_email($this->_str_to_array($replyto));
232 }
233
234 if ($name == '')
235 {
236 $name = $replyto;
237 }
238
239 if (strncmp($name, '"', 1) != 0)
240 {
241 $name = '"'.$name.'"';
242 }
243
244 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
245 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600246
247 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
253 * Set Recipients
254 *
255 * @access public
256 * @param string
257 * @return void
258 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000259 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $to = $this->_str_to_array($to);
262 $to = $this->clean_email($to);
263
264 if ($this->validate)
265 {
266 $this->validate_email($to);
267 }
268
269 if ($this->_get_protocol() != 'mail')
270 {
271 $this->_set_header('To', implode(", ", $to));
272 }
273
274 switch ($this->_get_protocol())
275 {
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000276 case 'smtp' :
277 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 break;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000279 case 'sendmail' :
280 case 'mail' :
281 $this->_recipients = implode(", ", $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 break;
283 }
Eric Barnes6113f542010-12-29 13:36:12 -0500284
Greg Akera769deb2010-11-10 15:47:29 -0600285 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
289
290 /**
291 * Set CC
292 *
293 * @access public
294 * @param string
295 * @return void
296 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000297 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 $cc = $this->_str_to_array($cc);
300 $cc = $this->clean_email($cc);
301
302 if ($this->validate)
303 {
304 $this->validate_email($cc);
305 }
306
307 $this->_set_header('Cc', implode(", ", $cc));
308
309 if ($this->_get_protocol() == "smtp")
310 {
311 $this->_cc_array = $cc;
312 }
Eric Barnes6113f542010-12-29 13:36:12 -0500313
Greg Akera769deb2010-11-10 15:47:29 -0600314 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
318
319 /**
320 * Set BCC
321 *
322 * @access public
323 * @param string
324 * @param string
325 * @return void
326 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000327 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 if ($limit != '' && is_numeric($limit))
330 {
331 $this->bcc_batch_mode = TRUE;
332 $this->bcc_batch_size = $limit;
333 }
334
335 $bcc = $this->_str_to_array($bcc);
336 $bcc = $this->clean_email($bcc);
337
338 if ($this->validate)
339 {
340 $this->validate_email($bcc);
341 }
342
343 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
344 {
345 $this->_bcc_array = $bcc;
346 }
347 else
348 {
349 $this->_set_header('Bcc', implode(", ", $bcc));
350 }
Eric Barnes6113f542010-12-29 13:36:12 -0500351
Greg Akera769deb2010-11-10 15:47:29 -0600352 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
356
357 /**
358 * Set Email Subject
359 *
360 * @access public
361 * @param string
362 * @return void
363 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000364 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 $subject = $this->_prep_q_encoding($subject);
367 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600368 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // --------------------------------------------------------------------
372
373 /**
374 * Set Body
375 *
376 * @access public
377 * @param string
378 * @return void
379 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000380 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
diegorivera13095cb2011-10-19 02:56:15 -0200382 $this->_body = rtrim(str_replace("\r", "", $body));
383
384 //strip slashes only if magic quotes is ON
385 //if we do it with magic quotes OFF, it strips real, user-inputted chars.
diegorivera6eab49a2011-10-19 11:18:45 -0200386 if (get_magic_quotes_gpc())
387 {
diegorivera13095cb2011-10-19 02:56:15 -0200388 $this->_body = stripslashes($this->_body);
diegorivera6eab49a2011-10-19 11:18:45 -0200389 }
diegorivera13095cb2011-10-19 02:56:15 -0200390
Greg Akera769deb2010-11-10 15:47:29 -0600391 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 // --------------------------------------------------------------------
395
396 /**
397 * Assign file attachments
398 *
399 * @access public
400 * @param string
401 * @return void
402 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000403 public function attach($filename, $disposition = 'attachment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 $this->_attach_name[] = $filename;
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600406 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Derek Jones37f4b9c2011-07-01 17:56:50 -0500407 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600408 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Add a Header Item
415 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600416 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @param string
418 * @param string
419 * @return void
420 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600421 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 $this->_headers[$header] = $value;
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // --------------------------------------------------------------------
427
428 /**
429 * Convert a String to an Array
430 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600431 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string
433 * @return array
434 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600435 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
437 if ( ! is_array($email))
438 {
439 if (strpos($email, ',') !== FALSE)
440 {
441 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
442 }
443 else
444 {
445 $email = trim($email);
446 settype($email, "array");
447 }
448 }
449 return $email;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * Set Multipart Value
456 *
457 * @access public
458 * @param string
459 * @return void
460 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000461 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Frank de Jonge9aa89292011-08-21 05:45:55 +0300463 $this->alt_message = $str;
Greg Akera769deb2010-11-10 15:47:29 -0600464 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
468
469 /**
470 * Set Mailtype
471 *
472 * @access public
473 * @param string
474 * @return void
475 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000476 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
478 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600479 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Set Wordwrap
486 *
487 * @access public
488 * @param string
489 * @return void
490 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000491 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600494 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Set Protocol
501 *
502 * @access public
503 * @param string
504 * @return void
505 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000506 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
Greg Akera769deb2010-11-10 15:47:29 -0600509 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
513
514 /**
515 * Set Priority
516 *
517 * @access public
518 * @param integer
519 * @return void
520 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000521 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 if ( ! is_numeric($n))
524 {
525 $this->priority = 3;
526 return;
527 }
528
529 if ($n < 1 OR $n > 5)
530 {
531 $this->priority = 3;
532 return;
533 }
534
535 $this->priority = $n;
Greg Akera769deb2010-11-10 15:47:29 -0600536 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * Set Newline Character
543 *
544 * @access public
545 * @param string
546 * @return void
547 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000548 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
550 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
551 {
552 $this->newline = "\n";
553 return;
554 }
555
556 $this->newline = $newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500557
Greg Akera769deb2010-11-10 15:47:29 -0600558 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 // --------------------------------------------------------------------
562
563 /**
564 * Set CRLF
565 *
566 * @access public
567 * @param string
568 * @return void
569 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000570 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
572 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
573 {
574 $this->crlf = "\n";
575 return;
576 }
577
578 $this->crlf = $crlf;
Eric Barnes6113f542010-12-29 13:36:12 -0500579
Greg Akera769deb2010-11-10 15:47:29 -0600580 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 }
Barry Mienydd671972010-10-04 16:33:58 +0200582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 // --------------------------------------------------------------------
584
585 /**
586 * Set Message Boundary
587 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600588 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 * @return void
590 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600591 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
594 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Get the Message ID
601 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600602 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return string
604 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600605 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
607 $from = $this->_headers['Return-Path'];
608 $from = str_replace(">", "", $from);
609 $from = str_replace("<", "", $from);
610
Derek Jones37f4b9c2011-07-01 17:56:50 -0500611 return "<".uniqid('').strstr($from, '@').">";
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 // --------------------------------------------------------------------
615
616 /**
617 * Get Mail Protocol
618 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600619 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param bool
621 * @return string
622 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600623 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
625 $this->protocol = strtolower($this->protocol);
626 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
627
628 if ($return == TRUE)
629 {
630 return $this->protocol;
631 }
632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // --------------------------------------------------------------------
635
636 /**
637 * Get Mail Encoding
638 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600639 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @param bool
641 * @return string
642 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600643 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
646
647 foreach ($this->_base_charsets as $charset)
648 {
649 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
650 {
651 $this->_encoding = '7bit';
652 }
653 }
654
655 if ($return == TRUE)
656 {
657 return $this->_encoding;
658 }
659 }
660
661 // --------------------------------------------------------------------
662
663 /**
664 * Get content type (text/html/attachment)
665 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600666 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @return string
668 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600669 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500671 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
673 return 'html';
674 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500675 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
677 return 'html-attach';
678 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500679 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 {
681 return 'plain-attach';
682 }
683 else
684 {
685 return 'plain';
686 }
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
690
691 /**
692 * Set RFC 822 Date
693 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600694 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 * @return string
696 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600697 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 {
699 $timezone = date("Z");
700 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
701 $timezone = abs($timezone);
702 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
703
704 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
705 }
Barry Mienydd671972010-10-04 16:33:58 +0200706
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 // --------------------------------------------------------------------
708
709 /**
710 * Mime message
711 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600712 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 * @return string
714 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600715 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000716 {
717 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
718 }
Barry Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 // --------------------------------------------------------------------
721
722 /**
723 * Validate Email Address
724 *
725 * @access public
726 * @param string
727 * @return bool
728 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000729 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 {
731 if ( ! is_array($email))
732 {
patworkb0707982011-04-08 15:10:05 +0200733 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 return FALSE;
735 }
736
737 foreach ($email as $val)
738 {
739 if ( ! $this->valid_email($val))
740 {
patworkb0707982011-04-08 15:10:05 +0200741 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000742 return FALSE;
743 }
744 }
745
746 return TRUE;
747 }
Barry Mienydd671972010-10-04 16:33:58 +0200748
Derek Allard2067d1a2008-11-13 22:59:24 +0000749 // --------------------------------------------------------------------
750
751 /**
752 * Email Validation
753 *
754 * @access public
755 * @param string
756 * @return bool
757 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000758 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 {
760 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
761 }
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 // --------------------------------------------------------------------
764
765 /**
766 * Clean Extended Email Address: Joe Smith <joe@smith.com>
767 *
768 * @access public
769 * @param string
770 * @return string
771 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000772 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000773 {
774 if ( ! is_array($email))
775 {
776 if (preg_match('/\<(.*)\>/', $email, $match))
777 {
Barry Mienydd671972010-10-04 16:33:58 +0200778 return $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 {
Barry Mienydd671972010-10-04 16:33:58 +0200782 return $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 }
784 }
785
786 $clean_email = array();
787
788 foreach ($email as $addy)
789 {
790 if (preg_match( '/\<(.*)\>/', $addy, $match))
791 {
Barry Mienydd671972010-10-04 16:33:58 +0200792 $clean_email[] = $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000793 }
Barry Mienydd671972010-10-04 16:33:58 +0200794 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 {
Barry Mienydd671972010-10-04 16:33:58 +0200796 $clean_email[] = $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 }
798 }
799
800 return $clean_email;
801 }
Barry Mienydd671972010-10-04 16:33:58 +0200802
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 // --------------------------------------------------------------------
804
805 /**
806 * Build alternative plain text message
807 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000808 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 * in plain-text headers of HTML-formatted emails.
810 * If the user hasn't specified his own alternative message
811 * it creates one by stripping the HTML
812 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600813 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000814 * @return string
815 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600816 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 {
818 if ($this->alt_message != "")
819 {
820 return $this->word_wrap($this->alt_message, '76');
821 }
822
823 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
824 {
825 $body = $match['1'];
826 }
827 else
828 {
829 $body = $this->_body;
830 }
831
832 $body = trim(strip_tags($body));
833 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
834 $body = str_replace("\t", "", $body);
835
836 for ($i = 20; $i >= 3; $i--)
837 {
838 $n = "";
839
840 for ($x = 1; $x <= $i; $x ++)
841 {
Barry Mienydd671972010-10-04 16:33:58 +0200842 $n .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000843 }
844
845 $body = str_replace($n, "\n\n", $body);
846 }
847
848 return $this->word_wrap($body, '76');
849 }
Barry Mienydd671972010-10-04 16:33:58 +0200850
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // --------------------------------------------------------------------
852
853 /**
854 * Word Wrap
855 *
856 * @access public
857 * @param string
858 * @param integer
859 * @return string
860 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000861 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000862 {
863 // Se the character limit
864 if ($charlim == '')
865 {
866 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
867 }
868
869 // Reduce multiple spaces
870 $str = preg_replace("| +|", " ", $str);
871
872 // Standardize newlines
873 if (strpos($str, "\r") !== FALSE)
874 {
875 $str = str_replace(array("\r\n", "\r"), "\n", $str);
876 }
877
878 // If the current word is surrounded by {unwrap} tags we'll
879 // strip the entire chunk and replace it with a marker.
880 $unwrap = array();
881 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
882 {
883 for ($i = 0; $i < count($matches['0']); $i++)
884 {
885 $unwrap[] = $matches['1'][$i];
886 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
887 }
888 }
889
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000890 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500892 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 $str = wordwrap($str, $charlim, "\n", FALSE);
894
895 // Split the string into individual lines of text and cycle through them
896 $output = "";
897 foreach (explode("\n", $str) as $line)
898 {
899 // Is the line within the allowed character count?
900 // If so we'll join it to the output and continue
901 if (strlen($line) <= $charlim)
902 {
903 $output .= $line.$this->newline;
904 continue;
905 }
906
907 $temp = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500908 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000909 {
910 // If the over-length word is a URL we won't wrap it
911 if (preg_match("!\[url.+\]|://|wwww.!", $line))
912 {
913 break;
914 }
915
916 // Trim the word down
917 $temp .= substr($line, 0, $charlim-1);
918 $line = substr($line, $charlim-1);
919 }
920
921 // If $temp contains data it means we had to split up an over-length
922 // word into smaller chunks so we'll add it back to our current line
923 if ($temp != '')
924 {
925 $output .= $temp.$this->newline.$line;
926 }
927 else
928 {
929 $output .= $line;
930 }
931
932 $output .= $this->newline;
933 }
934
935 // Put our markers back
936 if (count($unwrap) > 0)
937 {
938 foreach ($unwrap as $key => $val)
939 {
940 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
941 }
942 }
943
944 return $output;
945 }
Barry Mienydd671972010-10-04 16:33:58 +0200946
Derek Allard2067d1a2008-11-13 22:59:24 +0000947 // --------------------------------------------------------------------
948
949 /**
950 * Build final headers
951 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600952 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 * @param string
954 * @return string
955 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600956 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000957 {
958 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
959 $this->_set_header('X-Mailer', $this->useragent);
960 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
961 $this->_set_header('Message-ID', $this->_get_message_id());
962 $this->_set_header('Mime-Version', '1.0');
963 }
Barry Mienydd671972010-10-04 16:33:58 +0200964
Derek Allard2067d1a2008-11-13 22:59:24 +0000965 // --------------------------------------------------------------------
966
967 /**
968 * Write Headers as a string
969 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600970 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 * @return void
972 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600973 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000974 {
975 if ($this->protocol == 'mail')
976 {
977 $this->_subject = $this->_headers['Subject'];
978 unset($this->_headers['Subject']);
979 }
980
981 reset($this->_headers);
982 $this->_header_str = "";
983
Pascal Kriete14287f32011-02-14 13:39:34 -0500984 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000985 {
986 $val = trim($val);
987
988 if ($val != "")
989 {
990 $this->_header_str .= $key.": ".$val.$this->newline;
991 }
992 }
993
994 if ($this->_get_protocol() == 'mail')
995 {
Derek Jones1d890882009-02-10 20:32:14 +0000996 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000997 }
998 }
Barry Mienydd671972010-10-04 16:33:58 +0200999
Derek Allard2067d1a2008-11-13 22:59:24 +00001000 // --------------------------------------------------------------------
1001
1002 /**
1003 * Build Final Body and attachments
1004 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001005 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 * @return void
1007 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001008 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001009 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001010 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
1012 $this->_body = $this->word_wrap($this->_body);
1013 }
1014
1015 $this->_set_boundaries();
1016 $this->_write_headers();
1017
1018 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001019 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001020
1021 switch ($this->_get_content_type())
1022 {
1023 case 'plain' :
1024
1025 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1026 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1027
1028 if ($this->_get_protocol() == 'mail')
1029 {
1030 $this->_header_str .= $hdr;
1031 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001032 }
Brandon Jones485d7412010-11-09 16:38:17 -05001033 else
1034 {
1035 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1036 }
Eric Barnes6113f542010-12-29 13:36:12 -05001037
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 return;
1039
1040 break;
1041 case 'html' :
1042
1043 if ($this->send_multipart === FALSE)
1044 {
1045 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1046 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1047 }
1048 else
1049 {
Derek Jonesa45e7612009-02-10 18:33:01 +00001050 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001051
Brandon Jones485d7412010-11-09 16:38:17 -05001052 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1053 $body .= "--" . $this->_alt_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001054
Brandon Jones485d7412010-11-09 16:38:17 -05001055 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1056 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1057 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1058
1059 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1060 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001061 }
Eric Barnes6113f542010-12-29 13:36:12 -05001062
Brandon Jones485d7412010-11-09 16:38:17 -05001063 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001064
1065
Derek Allard2067d1a2008-11-13 22:59:24 +00001066 if ($this->_get_protocol() == 'mail')
1067 {
1068 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001069 }
1070 else
1071 {
1072 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001073 }
1074
Derek Allard2067d1a2008-11-13 22:59:24 +00001075
1076 if ($this->send_multipart !== FALSE)
1077 {
Brandon Jones485d7412010-11-09 16:38:17 -05001078 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 return;
1082
1083 break;
1084 case 'plain-attach' :
1085
Derek Jonesa45e7612009-02-10 18:33:01 +00001086 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001087
1088 if ($this->_get_protocol() == 'mail')
1089 {
1090 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001091 }
1092
Brandon Jones485d7412010-11-09 16:38:17 -05001093 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1094 $body .= "--" . $this->_atc_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001095
Brandon Jones485d7412010-11-09 16:38:17 -05001096 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1097 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001098
Brandon Jones485d7412010-11-09 16:38:17 -05001099 $body .= $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
1101 break;
1102 case 'html-attach' :
1103
Derek Jonesa45e7612009-02-10 18:33:01 +00001104 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001105
Derek Allard2067d1a2008-11-13 22:59:24 +00001106 if ($this->_get_protocol() == 'mail')
1107 {
1108 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001109 }
1110
Brandon Jones485d7412010-11-09 16:38:17 -05001111 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1112 $body .= "--" . $this->_atc_boundary . $this->newline;
1113
1114 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1115 $body .= "--" . $this->_alt_boundary . $this->newline;
1116
1117 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1118 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1119 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1120
1121 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1122 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
1123
1124 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1125 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001126
1127 break;
1128 }
1129
1130 $attachment = array();
1131
1132 $z = 0;
1133
1134 for ($i=0; $i < count($this->_attach_name); $i++)
1135 {
1136 $filename = $this->_attach_name[$i];
1137 $basename = basename($filename);
1138 $ctype = $this->_attach_type[$i];
1139
1140 if ( ! file_exists($filename))
1141 {
patworkb0707982011-04-08 15:10:05 +02001142 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001143 return FALSE;
1144 }
1145
Derek Jones37f4b9c2011-07-01 17:56:50 -05001146 $h = "--".$this->_atc_boundary.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001147 $h .= "Content-type: ".$ctype."; ";
1148 $h .= "name=\"".$basename."\"".$this->newline;
1149 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1150 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1151
1152 $attachment[$z++] = $h;
1153 $file = filesize($filename) +1;
1154
1155 if ( ! $fp = fopen($filename, FOPEN_READ))
1156 {
patworkb0707982011-04-08 15:10:05 +02001157 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 return FALSE;
1159 }
1160
1161 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1162 fclose($fp);
1163 }
1164
Brandon Jones485d7412010-11-09 16:38:17 -05001165 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Eric Barnes6113f542010-12-29 13:36:12 -05001166
Brandon Jones485d7412010-11-09 16:38:17 -05001167
Derek Allard2067d1a2008-11-13 22:59:24 +00001168 if ($this->_get_protocol() == 'mail')
1169 {
Brandon Jones485d7412010-11-09 16:38:17 -05001170 $this->_finalbody = $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001171 }
Brandon Jones485d7412010-11-09 16:38:17 -05001172 else
1173 {
1174 $this->_finalbody = $hdr . $body;
1175 }
Eric Barnes6113f542010-12-29 13:36:12 -05001176
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 return;
1178 }
Barry Mienydd671972010-10-04 16:33:58 +02001179
Derek Allard2067d1a2008-11-13 22:59:24 +00001180 // --------------------------------------------------------------------
1181
1182 /**
1183 * Prep Quoted Printable
1184 *
1185 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1186 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1187 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001188 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 * @param string
1190 * @param integer
1191 * @return string
1192 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001193 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001194 {
1195 // Set the character limit
1196 // Don't allow over 76, as that will make servers and MUAs barf
1197 // all over quoted-printable data
1198 if ($charlim == '' OR $charlim > '76')
1199 {
1200 $charlim = '76';
1201 }
1202
1203 // Reduce multiple spaces
1204 $str = preg_replace("| +|", " ", $str);
1205
1206 // kill nulls
1207 $str = preg_replace('/\x00+/', '', $str);
1208
1209 // Standardize newlines
1210 if (strpos($str, "\r") !== FALSE)
1211 {
1212 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1213 }
1214
1215 // We are intentionally wrapping so mail servers will encode characters
1216 // properly and MUAs will behave, so {unwrap} must go!
1217 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1218
1219 // Break into an array of lines
1220 $lines = explode("\n", $str);
1221
1222 $escape = '=';
1223 $output = '';
1224
1225 foreach ($lines as $line)
1226 {
1227 $length = strlen($line);
1228 $temp = '';
1229
1230 // Loop through each character in the line to add soft-wrap
1231 // characters at the end of a line " =\r\n" and add the newly
1232 // processed line(s) to the output (see comment on $crlf class property)
1233 for ($i = 0; $i < $length; $i++)
1234 {
1235 // Grab the next character
1236 $char = substr($line, $i, 1);
1237 $ascii = ord($char);
1238
1239 // Convert spaces and tabs but only if it's the end of the line
1240 if ($i == ($length - 1))
1241 {
1242 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1243 }
1244
1245 // encode = signs
1246 if ($ascii == '61')
1247 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001248 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001249 }
1250
1251 // If we're at the character limit, add the line to the output,
1252 // reset our temp variable, and keep on chuggin'
1253 if ((strlen($temp) + strlen($char)) >= $charlim)
1254 {
1255 $output .= $temp.$escape.$this->crlf;
1256 $temp = '';
1257 }
1258
1259 // Add the character to our temporary line
1260 $temp .= $char;
1261 }
1262
1263 // Add our completed line to the output
1264 $output .= $temp.$this->crlf;
1265 }
1266
1267 // get rid of extra CRLF tacked onto the end
1268 $output = substr($output, 0, strlen($this->crlf) * -1);
1269
1270 return $output;
1271 }
1272
1273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001274
Derek Allard2067d1a2008-11-13 22:59:24 +00001275 /**
1276 * Prep Q Encoding
1277 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001278 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001279 * but not identical to quoted-printable, so it has its own method
1280 *
1281 * @access public
1282 * @param str
1283 * @param bool // set to TRUE for processing From: headers
1284 * @return str
1285 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001286 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001287 {
1288 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1289
1290 // Line length must not exceed 76 characters, so we adjust for
1291 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1292 $limit = 75 - 7 - strlen($this->charset);
1293
1294 // these special characters must be converted too
1295 $convert = array('_', '=', '?');
1296
1297 if ($from === TRUE)
1298 {
1299 $convert[] = ',';
1300 $convert[] = ';';
1301 }
1302
1303 $output = '';
1304 $temp = '';
1305
1306 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1307 {
1308 // Grab the next character
1309 $char = substr($str, $i, 1);
1310 $ascii = ord($char);
1311
1312 // convert ALL non-printable ASCII characters and our specials
1313 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1314 {
1315 $char = '='.dechex($ascii);
1316 }
1317
1318 // handle regular spaces a bit more compactly than =20
1319 if ($ascii == 32)
1320 {
1321 $char = '_';
1322 }
1323
1324 // If we're at the character limit, add the line to the output,
1325 // reset our temp variable, and keep on chuggin'
1326 if ((strlen($temp) + strlen($char)) >= $limit)
1327 {
1328 $output .= $temp.$this->crlf;
1329 $temp = '';
1330 }
1331
1332 // Add the character to our temporary line
1333 $temp .= $char;
1334 }
1335
1336 $str = $output.$temp;
1337
1338 // wrap each line with the shebang, charset, and transfer encoding
1339 // the preceding space on successive lines is required for header "folding"
1340 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
1341
1342 return $str;
1343 }
1344
1345 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001346
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 /**
1348 * Send Email
1349 *
1350 * @access public
1351 * @return bool
1352 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001353 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001354 {
1355 if ($this->_replyto_flag == FALSE)
1356 {
1357 $this->reply_to($this->_headers['From']);
1358 }
1359
Derek Jones37f4b9c2011-07-01 17:56:50 -05001360 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
Derek Allard2067d1a2008-11-13 22:59:24 +00001361 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1362 ( ! isset($this->_headers['Cc'])))
1363 {
patworkb0707982011-04-08 15:10:05 +02001364 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001365 return FALSE;
1366 }
1367
1368 $this->_build_headers();
1369
Derek Jones37f4b9c2011-07-01 17:56:50 -05001370 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 {
1372 if (count($this->_bcc_array) > $this->bcc_batch_size)
1373 return $this->batch_bcc_send();
1374 }
1375
1376 $this->_build_message();
1377
1378 if ( ! $this->_spool_email())
1379 {
1380 return FALSE;
1381 }
1382 else
1383 {
1384 return TRUE;
1385 }
1386 }
Barry Mienydd671972010-10-04 16:33:58 +02001387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 // --------------------------------------------------------------------
1389
1390 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001391 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001392 *
1393 * @access public
1394 * @return bool
1395 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001396 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001397 {
1398 $float = $this->bcc_batch_size -1;
1399
1400 $set = "";
1401
1402 $chunk = array();
1403
1404 for ($i = 0; $i < count($this->_bcc_array); $i++)
1405 {
1406 if (isset($this->_bcc_array[$i]))
1407 {
1408 $set .= ", ".$this->_bcc_array[$i];
1409 }
1410
1411 if ($i == $float)
1412 {
1413 $chunk[] = substr($set, 1);
1414 $float = $float + $this->bcc_batch_size;
1415 $set = "";
1416 }
1417
1418 if ($i == count($this->_bcc_array)-1)
1419 {
1420 $chunk[] = substr($set, 1);
1421 }
1422 }
1423
1424 for ($i = 0; $i < count($chunk); $i++)
1425 {
1426 unset($this->_headers['Bcc']);
1427 unset($bcc);
1428
1429 $bcc = $this->_str_to_array($chunk[$i]);
1430 $bcc = $this->clean_email($bcc);
1431
1432 if ($this->protocol != 'smtp')
1433 {
1434 $this->_set_header('Bcc', implode(", ", $bcc));
1435 }
1436 else
1437 {
1438 $this->_bcc_array = $bcc;
1439 }
1440
1441 $this->_build_message();
1442 $this->_spool_email();
1443 }
1444 }
Barry Mienydd671972010-10-04 16:33:58 +02001445
Derek Allard2067d1a2008-11-13 22:59:24 +00001446 // --------------------------------------------------------------------
1447
1448 /**
1449 * Unwrap special elements
1450 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001451 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 * @return void
1453 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001454 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001455 {
1456 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1457 }
Barry Mienydd671972010-10-04 16:33:58 +02001458
Derek Allard2067d1a2008-11-13 22:59:24 +00001459 // --------------------------------------------------------------------
1460
1461 /**
1462 * Strip line-breaks via callback
1463 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001464 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 * @return string
1466 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001467 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001468 {
1469 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1470 {
1471 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1472 }
1473
1474 return $matches[1];
1475 }
Barry Mienydd671972010-10-04 16:33:58 +02001476
Derek Allard2067d1a2008-11-13 22:59:24 +00001477 // --------------------------------------------------------------------
1478
1479 /**
1480 * Spool mail to the mail server
1481 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001482 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 * @return bool
1484 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001485 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001486 {
1487 $this->_unwrap_specials();
1488
1489 switch ($this->_get_protocol())
1490 {
1491 case 'mail' :
1492
1493 if ( ! $this->_send_with_mail())
1494 {
patworkb0707982011-04-08 15:10:05 +02001495 $this->_set_error_message('lang:email_send_failure_phpmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001496 return FALSE;
1497 }
1498 break;
1499 case 'sendmail' :
1500
1501 if ( ! $this->_send_with_sendmail())
1502 {
patworkb0707982011-04-08 15:10:05 +02001503 $this->_set_error_message('lang:email_send_failure_sendmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001504 return FALSE;
1505 }
1506 break;
1507 case 'smtp' :
1508
1509 if ( ! $this->_send_with_smtp())
1510 {
patworkb0707982011-04-08 15:10:05 +02001511 $this->_set_error_message('lang:email_send_failure_smtp');
Derek Allard2067d1a2008-11-13 22:59:24 +00001512 return FALSE;
1513 }
1514 break;
1515
1516 }
1517
patworkb0707982011-04-08 15:10:05 +02001518 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001519 return TRUE;
1520 }
Barry Mienydd671972010-10-04 16:33:58 +02001521
Derek Allard2067d1a2008-11-13 22:59:24 +00001522 // --------------------------------------------------------------------
1523
1524 /**
1525 * Send using mail()
1526 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001527 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 * @return bool
1529 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001530 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001531 {
1532 if ($this->_safe_mode == TRUE)
1533 {
1534 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1535 {
1536 return FALSE;
1537 }
1538 else
1539 {
1540 return TRUE;
1541 }
1542 }
1543 else
1544 {
1545 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1546 // we've encountered servers that seem to require it to be in place.
Eric Barnes6113f542010-12-29 13:36:12 -05001547
Derek Allard2067d1a2008-11-13 22:59:24 +00001548 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1549 {
1550 return FALSE;
1551 }
1552 else
1553 {
1554 return TRUE;
1555 }
1556 }
1557 }
Barry Mienydd671972010-10-04 16:33:58 +02001558
Derek Allard2067d1a2008-11-13 22:59:24 +00001559 // --------------------------------------------------------------------
1560
1561 /**
1562 * Send using Sendmail
1563 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001564 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 * @return bool
1566 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001567 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001568 {
1569 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1570
Derek Jones4cefaa42009-04-29 19:13:56 +00001571 if ($fp === FALSE OR $fp === NULL)
1572 {
1573 // server probably has popen disabled, so nothing we can do to get a verbose error.
1574 return FALSE;
1575 }
Derek Jones71141ce2010-03-02 16:41:20 -06001576
Derek Jonesc630bcf2008-11-17 21:09:45 +00001577 fputs($fp, $this->_header_str);
1578 fputs($fp, $this->_finalbody);
1579
Barry Mienydd671972010-10-04 16:33:58 +02001580 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001581
Derek Jonesc630bcf2008-11-17 21:09:45 +00001582 if (version_compare(PHP_VERSION, '4.2.3') == -1)
1583 {
1584 $status = $status >> 8 & 0xFF;
Barry Mienydd671972010-10-04 16:33:58 +02001585 }
Derek Jones71141ce2010-03-02 16:41:20 -06001586
Derek Jones604873f2008-11-18 15:57:24 +00001587 if ($status != 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001588 {
patworkb0707982011-04-08 15:10:05 +02001589 $this->_set_error_message('lang:email_exit_status', $status);
1590 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001591 return FALSE;
1592 }
1593
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 return TRUE;
1595 }
Barry Mienydd671972010-10-04 16:33:58 +02001596
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 // --------------------------------------------------------------------
1598
1599 /**
1600 * Send using SMTP
1601 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001602 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 * @return bool
1604 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001605 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001606 {
1607 if ($this->smtp_host == '')
1608 {
patworkb0707982011-04-08 15:10:05 +02001609 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001610 return FALSE;
1611 }
1612
1613 $this->_smtp_connect();
1614 $this->_smtp_authenticate();
1615
1616 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1617
Pascal Kriete14287f32011-02-14 13:39:34 -05001618 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001619 {
1620 $this->_send_command('to', $val);
1621 }
1622
1623 if (count($this->_cc_array) > 0)
1624 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001625 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001626 {
1627 if ($val != "")
1628 {
1629 $this->_send_command('to', $val);
1630 }
1631 }
1632 }
1633
1634 if (count($this->_bcc_array) > 0)
1635 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001636 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001637 {
1638 if ($val != "")
1639 {
1640 $this->_send_command('to', $val);
1641 }
1642 }
1643 }
1644
1645 $this->_send_command('data');
1646
1647 // perform dot transformation on any lines that begin with a dot
1648 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1649
1650 $this->_send_data('.');
1651
1652 $reply = $this->_get_smtp_data();
1653
1654 $this->_set_error_message($reply);
1655
1656 if (strncmp($reply, '250', 3) != 0)
1657 {
patworkb0707982011-04-08 15:10:05 +02001658 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001659 return FALSE;
1660 }
1661
1662 $this->_send_command('quit');
1663 return TRUE;
1664 }
Barry Mienydd671972010-10-04 16:33:58 +02001665
Derek Allard2067d1a2008-11-13 22:59:24 +00001666 // --------------------------------------------------------------------
1667
1668 /**
1669 * SMTP Connect
1670 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001671 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 * @param string
1673 * @return string
1674 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001675 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001676 {
1677 $this->_smtp_connect = fsockopen($this->smtp_host,
1678 $this->smtp_port,
1679 $errno,
1680 $errstr,
1681 $this->smtp_timeout);
1682
Pascal Kriete14287f32011-02-14 13:39:34 -05001683 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001684 {
patworkb0707982011-04-08 15:10:05 +02001685 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001686 return FALSE;
1687 }
1688
1689 $this->_set_error_message($this->_get_smtp_data());
1690 return $this->_send_command('hello');
1691 }
Barry Mienydd671972010-10-04 16:33:58 +02001692
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 // --------------------------------------------------------------------
1694
1695 /**
1696 * Send SMTP command
1697 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001698 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 * @param string
1700 * @param string
1701 * @return string
1702 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001703 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001704 {
1705 switch ($cmd)
1706 {
1707 case 'hello' :
1708
1709 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1710 $this->_send_data('EHLO '.$this->_get_hostname());
1711 else
1712 $this->_send_data('HELO '.$this->_get_hostname());
1713
1714 $resp = 250;
1715 break;
1716 case 'from' :
1717
1718 $this->_send_data('MAIL FROM:<'.$data.'>');
1719
1720 $resp = 250;
1721 break;
1722 case 'to' :
1723
1724 $this->_send_data('RCPT TO:<'.$data.'>');
1725
1726 $resp = 250;
1727 break;
1728 case 'data' :
1729
1730 $this->_send_data('DATA');
1731
1732 $resp = 354;
1733 break;
1734 case 'quit' :
1735
1736 $this->_send_data('QUIT');
1737
1738 $resp = 221;
1739 break;
1740 }
1741
1742 $reply = $this->_get_smtp_data();
1743
1744 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1745
1746 if (substr($reply, 0, 3) != $resp)
1747 {
patworkb0707982011-04-08 15:10:05 +02001748 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 return FALSE;
1750 }
1751
1752 if ($cmd == 'quit')
1753 {
1754 fclose($this->_smtp_connect);
1755 }
1756
1757 return TRUE;
1758 }
Barry Mienydd671972010-10-04 16:33:58 +02001759
Derek Allard2067d1a2008-11-13 22:59:24 +00001760 // --------------------------------------------------------------------
1761
1762 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001763 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001764 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001765 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001766 * @return bool
1767 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001768 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001769 {
1770 if ( ! $this->_smtp_auth)
1771 {
1772 return TRUE;
1773 }
1774
Derek Jones37f4b9c2011-07-01 17:56:50 -05001775 if ($this->smtp_user == "" AND $this->smtp_pass == "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001776 {
patworkb0707982011-04-08 15:10:05 +02001777 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 return FALSE;
1779 }
1780
1781 $this->_send_data('AUTH LOGIN');
1782
1783 $reply = $this->_get_smtp_data();
1784
1785 if (strncmp($reply, '334', 3) != 0)
1786 {
patworkb0707982011-04-08 15:10:05 +02001787 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001788 return FALSE;
1789 }
1790
1791 $this->_send_data(base64_encode($this->smtp_user));
1792
1793 $reply = $this->_get_smtp_data();
1794
1795 if (strncmp($reply, '334', 3) != 0)
1796 {
patworkb0707982011-04-08 15:10:05 +02001797 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 return FALSE;
1799 }
1800
1801 $this->_send_data(base64_encode($this->smtp_pass));
1802
1803 $reply = $this->_get_smtp_data();
1804
1805 if (strncmp($reply, '235', 3) != 0)
1806 {
patworkb0707982011-04-08 15:10:05 +02001807 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001808 return FALSE;
1809 }
1810
1811 return TRUE;
1812 }
Barry Mienydd671972010-10-04 16:33:58 +02001813
Derek Allard2067d1a2008-11-13 22:59:24 +00001814 // --------------------------------------------------------------------
1815
1816 /**
1817 * Send SMTP data
1818 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001819 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001820 * @return bool
1821 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001822 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001823 {
1824 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1825 {
patworkb0707982011-04-08 15:10:05 +02001826 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 return FALSE;
1828 }
1829 else
1830 {
1831 return TRUE;
1832 }
1833 }
Barry Mienydd671972010-10-04 16:33:58 +02001834
Derek Allard2067d1a2008-11-13 22:59:24 +00001835 // --------------------------------------------------------------------
1836
1837 /**
1838 * Get SMTP data
1839 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001840 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 * @return string
1842 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001843 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001844 {
1845 $data = "";
1846
1847 while ($str = fgets($this->_smtp_connect, 512))
1848 {
1849 $data .= $str;
1850
1851 if (substr($str, 3, 1) == " ")
1852 {
1853 break;
1854 }
1855 }
1856
1857 return $data;
1858 }
Barry Mienydd671972010-10-04 16:33:58 +02001859
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 // --------------------------------------------------------------------
1861
1862 /**
1863 * Get Hostname
1864 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001865 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001866 * @return string
1867 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001868 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001869 {
1870 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1871 }
Barry Mienydd671972010-10-04 16:33:58 +02001872
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 // --------------------------------------------------------------------
1874
1875 /**
1876 * Get IP
1877 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001878 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001879 * @return string
1880 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001881 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001882 {
1883 if ($this->_IP !== FALSE)
1884 {
1885 return $this->_IP;
1886 }
1887
1888 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1889 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1890 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1891
Barry Mienydd671972010-10-04 16:33:58 +02001892 if ($cip && $rip) $this->_IP = $cip;
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 elseif ($rip) $this->_IP = $rip;
1894 elseif ($cip) $this->_IP = $cip;
1895 elseif ($fip) $this->_IP = $fip;
1896
Robin Sowell76b369e2010-03-19 11:15:28 -04001897 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 {
1899 $x = explode(',', $this->_IP);
1900 $this->_IP = end($x);
1901 }
1902
1903 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1904 {
1905 $this->_IP = '0.0.0.0';
1906 }
1907
1908 unset($cip);
1909 unset($rip);
1910 unset($fip);
1911
1912 return $this->_IP;
1913 }
Barry Mienydd671972010-10-04 16:33:58 +02001914
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 // --------------------------------------------------------------------
1916
1917 /**
1918 * Get Debug Message
1919 *
1920 * @access public
1921 * @return string
1922 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001923 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001924 {
1925 $msg = '';
1926
1927 if (count($this->_debug_msg) > 0)
1928 {
1929 foreach ($this->_debug_msg as $val)
1930 {
1931 $msg .= $val;
1932 }
1933 }
1934
1935 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1936 return $msg;
1937 }
Barry Mienydd671972010-10-04 16:33:58 +02001938
Derek Allard2067d1a2008-11-13 22:59:24 +00001939 // --------------------------------------------------------------------
1940
1941 /**
1942 * Set Message
1943 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001944 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001945 * @param string
1946 * @return string
1947 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001948 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001949 {
1950 $CI =& get_instance();
1951 $CI->lang->load('email');
1952
patworkb0707982011-04-08 15:10:05 +02001953 if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001954 {
1955 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1956 }
1957 else
1958 {
1959 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1960 }
1961 }
Barry Mienydd671972010-10-04 16:33:58 +02001962
Derek Allard2067d1a2008-11-13 22:59:24 +00001963 // --------------------------------------------------------------------
1964
1965 /**
1966 * Mime Types
1967 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001968 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001969 * @param string
1970 * @return string
1971 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001972 protected function _mime_types($ext = "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001973 {
1974 $mimes = array( 'hqx' => 'application/mac-binhex40',
1975 'cpt' => 'application/mac-compactpro',
1976 'doc' => 'application/msword',
1977 'bin' => 'application/macbinary',
1978 'dms' => 'application/octet-stream',
1979 'lha' => 'application/octet-stream',
1980 'lzh' => 'application/octet-stream',
1981 'exe' => 'application/octet-stream',
1982 'class' => 'application/octet-stream',
1983 'psd' => 'application/octet-stream',
1984 'so' => 'application/octet-stream',
1985 'sea' => 'application/octet-stream',
1986 'dll' => 'application/octet-stream',
1987 'oda' => 'application/oda',
1988 'pdf' => 'application/pdf',
1989 'ai' => 'application/postscript',
1990 'eps' => 'application/postscript',
1991 'ps' => 'application/postscript',
1992 'smi' => 'application/smil',
1993 'smil' => 'application/smil',
1994 'mif' => 'application/vnd.mif',
1995 'xls' => 'application/vnd.ms-excel',
1996 'ppt' => 'application/vnd.ms-powerpoint',
1997 'wbxml' => 'application/vnd.wap.wbxml',
1998 'wmlc' => 'application/vnd.wap.wmlc',
1999 'dcr' => 'application/x-director',
2000 'dir' => 'application/x-director',
2001 'dxr' => 'application/x-director',
2002 'dvi' => 'application/x-dvi',
2003 'gtar' => 'application/x-gtar',
2004 'php' => 'application/x-httpd-php',
2005 'php4' => 'application/x-httpd-php',
2006 'php3' => 'application/x-httpd-php',
2007 'phtml' => 'application/x-httpd-php',
2008 'phps' => 'application/x-httpd-php-source',
2009 'js' => 'application/x-javascript',
2010 'swf' => 'application/x-shockwave-flash',
2011 'sit' => 'application/x-stuffit',
2012 'tar' => 'application/x-tar',
2013 'tgz' => 'application/x-tar',
2014 'xhtml' => 'application/xhtml+xml',
2015 'xht' => 'application/xhtml+xml',
2016 'zip' => 'application/zip',
2017 'mid' => 'audio/midi',
2018 'midi' => 'audio/midi',
2019 'mpga' => 'audio/mpeg',
2020 'mp2' => 'audio/mpeg',
2021 'mp3' => 'audio/mpeg',
2022 'aif' => 'audio/x-aiff',
2023 'aiff' => 'audio/x-aiff',
2024 'aifc' => 'audio/x-aiff',
2025 'ram' => 'audio/x-pn-realaudio',
2026 'rm' => 'audio/x-pn-realaudio',
2027 'rpm' => 'audio/x-pn-realaudio-plugin',
2028 'ra' => 'audio/x-realaudio',
2029 'rv' => 'video/vnd.rn-realvideo',
2030 'wav' => 'audio/x-wav',
2031 'bmp' => 'image/bmp',
2032 'gif' => 'image/gif',
2033 'jpeg' => 'image/jpeg',
2034 'jpg' => 'image/jpeg',
2035 'jpe' => 'image/jpeg',
2036 'png' => 'image/png',
2037 'tiff' => 'image/tiff',
2038 'tif' => 'image/tiff',
2039 'css' => 'text/css',
2040 'html' => 'text/html',
2041 'htm' => 'text/html',
2042 'shtml' => 'text/html',
2043 'txt' => 'text/plain',
2044 'text' => 'text/plain',
2045 'log' => 'text/plain',
2046 'rtx' => 'text/richtext',
2047 'rtf' => 'text/rtf',
2048 'xml' => 'text/xml',
2049 'xsl' => 'text/xml',
2050 'mpeg' => 'video/mpeg',
2051 'mpg' => 'video/mpeg',
2052 'mpe' => 'video/mpeg',
2053 'qt' => 'video/quicktime',
2054 'mov' => 'video/quicktime',
2055 'avi' => 'video/x-msvideo',
2056 'movie' => 'video/x-sgi-movie',
2057 'doc' => 'application/msword',
2058 'word' => 'application/msword',
2059 'xl' => 'application/excel',
2060 'eml' => 'message/rfc822'
2061 );
2062
2063 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2064 }
2065
2066}
2067// END CI_Email class
2068
2069/* End of file Email.php */
patworkb0707982011-04-08 15:10:05 +02002070/* Location: ./system/libraries/Email.php */