blob: b7d9d2ebfbcb120acdb2f24dda1bb0aaeebe7afd [file] [log] [blame]
Derek Allard0fe82972008-01-15 13:58:47 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allard0fe82972008-01-15 13:58:47 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard0fe82972008-01-15 13:58:47 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000026 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/libraries/email.html
Derek Allard0fe82972008-01-15 13:58:47 +000028 */
29class CI_Email {
30
31 var $useragent = "CodeIgniter";
32 var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
33 var $protocol = "mail"; // mail/sendmail/smtp
34 var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
35 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 Jonese7c4c322008-01-22 18:16:39 +000039 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
Derek Allard0fe82972008-01-15 13:58:47 +000040 var $wrapchars = "76"; // Number of characters to wrap at.
41 var $mailtype = "text"; // text/html Defines email formatting
42 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 Jonese7c4c322008-01-22 18:16:39 +000045 var $validate = FALSE; // TRUE/FALSE. Enables email validation
Derek Allard0fe82972008-01-15 13:58:47 +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 Allard0fe82972008-01-15 13:58:47 +000048 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
49 // 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 Allard80ddb6b2008-02-25 12:51:00 +000051 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
Derek Jonese7c4c322008-01-22 18:16:39 +000052 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
53 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
Derek Allard80ddb6b2008-02-25 12:51:00 +000054 var $_safe_mode = FALSE;
Derek Allard0fe82972008-01-15 13:58:47 +000055 var $_subject = "";
56 var $_body = "";
57 var $_finalbody = "";
58 var $_alt_boundary = "";
59 var $_atc_boundary = "";
60 var $_header_str = "";
61 var $_smtp_connect = "";
Derek Allard80ddb6b2008-02-25 12:51:00 +000062 var $_encoding = "8bit";
Derek Allard0fe82972008-01-15 13:58:47 +000063 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');
Derek Jonese7c4c322008-01-22 18:16:39 +000075 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
Derek Allard0fe82972008-01-15 13:58:47 +000076 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 */
85 function CI_Email($config = array())
86 {
87 if (count($config) > 0)
88 {
89 $this->initialize($config);
90 }
91
Derek Allard80ddb6b2008-02-25 12:51:00 +000092 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
93 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
94
Derek Allard0fe82972008-01-15 13:58:47 +000095 log_message('debug', "Email Class Initialized");
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Initialize preferences
102 *
103 * @access public
104 * @param array
105 * @return void
106 */
107 function initialize($config = array())
108 {
109 $this->clear();
110 foreach ($config as $key => $val)
111 {
112 if (isset($this->$key))
113 {
114 $method = 'set_'.$key;
115
116 if (method_exists($this, $method))
117 {
118 $this->$method($val);
119 }
120 else
121 {
122 $this->$key = $val;
123 }
124 }
125 }
Derek Allard0fe82972008-01-15 13:58:47 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Initialize the Email Data
132 *
133 * @access public
134 * @return void
135 */
136 function clear($clear_attachments = FALSE)
137 {
138 $this->_subject = "";
139 $this->_body = "";
140 $this->_finalbody = "";
141 $this->_header_str = "";
142 $this->_replyto_flag = FALSE;
143 $this->_recipients = array();
144 $this->_headers = array();
145 $this->_debug_msg = array();
146
147 $this->_set_header('User-Agent', $this->useragent);
148 $this->_set_header('Date', $this->_set_date());
149
150 if ($clear_attachments !== FALSE)
151 {
152 $this->_attach_name = array();
153 $this->_attach_type = array();
154 $this->_attach_disp = array();
155 }
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Set FROM
162 *
163 * @access public
164 * @param string
165 * @param string
166 * @return void
167 */
168 function from($from, $name = '')
169 {
170 if (preg_match( '/\<(.*)\>/', $from, $match))
Derek Allard20d24052008-05-12 22:12:11 +0000171 {
Derek Allard0fe82972008-01-15 13:58:47 +0000172 $from = $match['1'];
Derek Allard20d24052008-05-12 22:12:11 +0000173 }
Derek Allard0fe82972008-01-15 13:58:47 +0000174
175 if ($this->validate)
Derek Allard20d24052008-05-12 22:12:11 +0000176 {
Derek Allard0fe82972008-01-15 13:58:47 +0000177 $this->validate_email($this->_str_to_array($from));
Derek Allard20d24052008-05-12 22:12:11 +0000178 }
Derek Allard0fe82972008-01-15 13:58:47 +0000179
Derek Allard20d24052008-05-12 22:12:11 +0000180 if ($name != '' && strncmp($name, '"', 1) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +0000181 {
182 $name = '"'.$name.'"';
183 }
184
185 $this->_set_header('From', $name.' <'.$from.'>');
186 $this->_set_header('Return-Path', '<'.$from.'>');
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Set Reply-to
193 *
194 * @access public
195 * @param string
196 * @param string
197 * @return void
198 */
199 function reply_to($replyto, $name = '')
200 {
201 if (preg_match( '/\<(.*)\>/', $replyto, $match))
Derek Allard20d24052008-05-12 22:12:11 +0000202 {
Derek Allard0fe82972008-01-15 13:58:47 +0000203 $replyto = $match['1'];
Derek Allard20d24052008-05-12 22:12:11 +0000204 }
Derek Allard0fe82972008-01-15 13:58:47 +0000205
206 if ($this->validate)
Derek Allard20d24052008-05-12 22:12:11 +0000207 {
Derek Allard0fe82972008-01-15 13:58:47 +0000208 $this->validate_email($this->_str_to_array($replyto));
Derek Allard20d24052008-05-12 22:12:11 +0000209 }
Derek Allard0fe82972008-01-15 13:58:47 +0000210
211 if ($name == '')
212 {
213 $name = $replyto;
214 }
215
Derek Allard20d24052008-05-12 22:12:11 +0000216 if (strncmp($name, '"', 1) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +0000217 {
218 $name = '"'.$name.'"';
219 }
220
221 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
222 $this->_replyto_flag = TRUE;
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Set Recipients
229 *
230 * @access public
231 * @param string
232 * @return void
233 */
234 function to($to)
235 {
236 $to = $this->_str_to_array($to);
237 $to = $this->clean_email($to);
238
239 if ($this->validate)
Derek Allard20d24052008-05-12 22:12:11 +0000240 {
Derek Allard0fe82972008-01-15 13:58:47 +0000241 $this->validate_email($to);
Derek Allard20d24052008-05-12 22:12:11 +0000242 }
Derek Allard0fe82972008-01-15 13:58:47 +0000243
244 if ($this->_get_protocol() != 'mail')
Derek Allard20d24052008-05-12 22:12:11 +0000245 {
Derek Allard0fe82972008-01-15 13:58:47 +0000246 $this->_set_header('To', implode(", ", $to));
Derek Allard20d24052008-05-12 22:12:11 +0000247 }
Derek Allard0fe82972008-01-15 13:58:47 +0000248
249 switch ($this->_get_protocol())
250 {
251 case 'smtp' : $this->_recipients = $to;
252 break;
253 case 'sendmail' : $this->_recipients = implode(", ", $to);
254 break;
255 case 'mail' : $this->_recipients = implode(", ", $to);
256 break;
257 }
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Set CC
264 *
265 * @access public
266 * @param string
267 * @return void
268 */
269 function cc($cc)
270 {
271 $cc = $this->_str_to_array($cc);
272 $cc = $this->clean_email($cc);
273
274 if ($this->validate)
275 $this->validate_email($cc);
276
277 $this->_set_header('Cc', implode(", ", $cc));
278
279 if ($this->_get_protocol() == "smtp")
Derek Allard20d24052008-05-12 22:12:11 +0000280 {
Derek Allard0fe82972008-01-15 13:58:47 +0000281 $this->_cc_array = $cc;
Derek Allard20d24052008-05-12 22:12:11 +0000282 }
Derek Allard0fe82972008-01-15 13:58:47 +0000283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Set BCC
289 *
290 * @access public
291 * @param string
292 * @param string
293 * @return void
294 */
295 function bcc($bcc, $limit = '')
296 {
297 if ($limit != '' && is_numeric($limit))
298 {
Derek Jonese7c4c322008-01-22 18:16:39 +0000299 $this->bcc_batch_mode = TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +0000300 $this->bcc_batch_size = $limit;
301 }
302
303 $bcc = $this->_str_to_array($bcc);
304 $bcc = $this->clean_email($bcc);
305
306 if ($this->validate)
Derek Allard20d24052008-05-12 22:12:11 +0000307 {
Derek Allard0fe82972008-01-15 13:58:47 +0000308 $this->validate_email($bcc);
Derek Allard20d24052008-05-12 22:12:11 +0000309 }
Derek Allard0fe82972008-01-15 13:58:47 +0000310
311 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
Derek Allard20d24052008-05-12 22:12:11 +0000312 {
Derek Allard0fe82972008-01-15 13:58:47 +0000313 $this->_bcc_array = $bcc;
Derek Allard20d24052008-05-12 22:12:11 +0000314 }
Derek Allard0fe82972008-01-15 13:58:47 +0000315 else
Derek Allard20d24052008-05-12 22:12:11 +0000316 {
Derek Allard0fe82972008-01-15 13:58:47 +0000317 $this->_set_header('Bcc', implode(", ", $bcc));
Derek Allard20d24052008-05-12 22:12:11 +0000318 }
Derek Allard0fe82972008-01-15 13:58:47 +0000319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Set Email Subject
325 *
326 * @access public
327 * @param string
328 * @return void
329 */
330 function subject($subject)
331 {
332 $subject = preg_replace("/(\r\n)|(\r)|(\n)/", "", $subject);
333 $subject = preg_replace("/(\t)/", " ", $subject);
334
335 $this->_set_header('Subject', trim($subject));
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Set Body
342 *
343 * @access public
344 * @param string
345 * @return void
346 */
347 function message($body)
348 {
349 $this->_body = stripslashes(rtrim(str_replace("\r", "", $body)));
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Assign file attachments
356 *
357 * @access public
358 * @param string
359 * @return string
360 */
361 function attach($filename, $disposition = 'attachment')
362 {
363 $this->_attach_name[] = $filename;
364 $this->_attach_type[] = $this->_mime_types(next(explode('.', basename($filename))));
365 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Add a Header Item
372 *
373 * @access public
374 * @param string
375 * @param string
376 * @return void
377 */
378 function _set_header($header, $value)
379 {
380 $this->_headers[$header] = $value;
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Convert a String to an Array
387 *
388 * @access public
389 * @param string
390 * @return array
391 */
392 function _str_to_array($email)
393 {
Derek Allard73274992008-05-05 16:39:18 +0000394 if (! is_array($email))
Derek Allard80ddb6b2008-02-25 12:51:00 +0000395 {
396 if (strpos($email, ',') !== FALSE)
397 {
398 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
Derek Allard0fe82972008-01-15 13:58:47 +0000399 }
400 else
401 {
402 $email = trim($email);
403 settype($email, "array");
404 }
405 }
406 return $email;
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * Set Multipart Value
413 *
414 * @access public
415 * @param string
416 * @return void
417 */
418 function set_alt_message($str = '')
419 {
420 $this->alt_message = ($str == '') ? '' : $str;
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Set Mailtype
427 *
428 * @access public
429 * @param string
430 * @return void
431 */
432 function set_mailtype($type = 'text')
433 {
434 $this->mailtype = ($type == 'html') ? 'html' : 'text';
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Set Wordwrap
441 *
442 * @access public
443 * @param string
444 * @return void
445 */
446 function set_wordwrap($wordwrap = TRUE)
447 {
448 $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Set Protocol
455 *
456 * @access public
457 * @param string
458 * @return void
459 */
460 function set_protocol($protocol = 'mail')
461 {
Derek Allard73274992008-05-05 16:39:18 +0000462 $this->protocol = (! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
Derek Allard0fe82972008-01-15 13:58:47 +0000463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Set Priority
469 *
470 * @access public
471 * @param integer
472 * @return void
473 */
474 function set_priority($n = 3)
475 {
Derek Allard73274992008-05-05 16:39:18 +0000476 if (! is_numeric($n))
Derek Allard0fe82972008-01-15 13:58:47 +0000477 {
478 $this->priority = 3;
479 return;
480 }
481
482 if ($n < 1 OR $n > 5)
483 {
484 $this->priority = 3;
485 return;
486 }
487
488 $this->priority = $n;
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Set Newline Character
495 *
496 * @access public
497 * @param string
498 * @return void
499 */
500 function set_newline($newline = "\n")
501 {
502 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
503 {
504 $this->newline = "\n";
505 return;
506 }
507
508 $this->newline = $newline;
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
Derek Allard7c53be42008-04-22 12:02:43 +0000514 * Set CRLF
515 *
516 * @access public
517 * @param string
518 * @return void
519 */
520 function set_crlf($crlf = "\n")
521 {
522 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
523 {
524 $this->crlf = "\n";
525 return;
526 }
527
528 $this->crlf = $crlf;
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
Derek Allard0fe82972008-01-15 13:58:47 +0000534 * Set Message Boundary
535 *
536 * @access private
537 * @return void
538 */
539 function _set_boundaries()
540 {
541 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
542 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Get the Message ID
549 *
550 * @access private
551 * @return string
552 */
553 function _get_message_id()
554 {
555 $from = $this->_headers['Return-Path'];
556 $from = str_replace(">", "", $from);
557 $from = str_replace("<", "", $from);
558
559 return "<".uniqid('').strstr($from, '@').">";
560 }
561
562 // --------------------------------------------------------------------
563
564 /**
565 * Get Mail Protocol
566 *
567 * @access private
568 * @param bool
569 * @return string
570 */
Derek Jonese7c4c322008-01-22 18:16:39 +0000571 function _get_protocol($return = TRUE)
Derek Allard0fe82972008-01-15 13:58:47 +0000572 {
573 $this->protocol = strtolower($this->protocol);
Derek Allard73274992008-05-05 16:39:18 +0000574 $this->protocol = (! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
Derek Allard0fe82972008-01-15 13:58:47 +0000575
Derek Jonese7c4c322008-01-22 18:16:39 +0000576 if ($return == TRUE)
Derek Allard20d24052008-05-12 22:12:11 +0000577 {
Derek Allard0fe82972008-01-15 13:58:47 +0000578 return $this->protocol;
Derek Allard20d24052008-05-12 22:12:11 +0000579 }
Derek Allard0fe82972008-01-15 13:58:47 +0000580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Get Mail Encoding
586 *
587 * @access private
588 * @param bool
589 * @return string
590 */
Derek Jonese7c4c322008-01-22 18:16:39 +0000591 function _get_encoding($return = TRUE)
Derek Allard0fe82972008-01-15 13:58:47 +0000592 {
Derek Allard73274992008-05-05 16:39:18 +0000593 $this->_encoding = (! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
Derek Allard0fe82972008-01-15 13:58:47 +0000594
Derek Jonese7c4c322008-01-22 18:16:39 +0000595 foreach ($this->_base_charsets as $charset)
596 {
597 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
598 {
599 $this->_encoding = '7bit';
600 }
601 }
Derek Allard0fe82972008-01-15 13:58:47 +0000602
Derek Jonese7c4c322008-01-22 18:16:39 +0000603 if ($return == TRUE)
604 {
605 return $this->_encoding;
606 }
Derek Allard0fe82972008-01-15 13:58:47 +0000607 }
608
609 // --------------------------------------------------------------------
610
611 /**
612 * Get content type (text/html/attachment)
613 *
614 * @access private
615 * @return string
616 */
617 function _get_content_type()
618 {
Derek Allard20d24052008-05-12 22:12:11 +0000619 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
620 {
Derek Allard0fe82972008-01-15 13:58:47 +0000621 return 'html';
Derek Allard20d24052008-05-12 22:12:11 +0000622 }
Derek Allard0fe82972008-01-15 13:58:47 +0000623 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
Derek Allard20d24052008-05-12 22:12:11 +0000624 {
Derek Allard0fe82972008-01-15 13:58:47 +0000625 return 'html-attach';
Derek Allard20d24052008-05-12 22:12:11 +0000626 }
Derek Allard0fe82972008-01-15 13:58:47 +0000627 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
Derek Allard20d24052008-05-12 22:12:11 +0000628 {
Derek Allard0fe82972008-01-15 13:58:47 +0000629 return 'plain-attach';
Derek Allard20d24052008-05-12 22:12:11 +0000630 }
631 else
632 {
633 return 'plain';
634 }
Derek Allard0fe82972008-01-15 13:58:47 +0000635 }
636
637 // --------------------------------------------------------------------
638
639 /**
640 * Set RFC 822 Date
641 *
642 * @access public
643 * @return string
644 */
645 function _set_date()
646 {
647 $timezone = date("Z");
Derek Allard20d24052008-05-12 22:12:11 +0000648 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
Derek Allard0fe82972008-01-15 13:58:47 +0000649 $timezone = abs($timezone);
650 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
651
652 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
653 }
654
655 // --------------------------------------------------------------------
656
657 /**
658 * Mime message
659 *
660 * @access private
661 * @return string
662 */
663 function _get_mime_message()
664 {
665 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
666 }
667
668 // --------------------------------------------------------------------
669
670 /**
671 * Validate Email Address
672 *
673 * @access public
674 * @param string
675 * @return bool
676 */
677 function validate_email($email)
678 {
Derek Allard73274992008-05-05 16:39:18 +0000679 if (! is_array($email))
Derek Allard0fe82972008-01-15 13:58:47 +0000680 {
681 $this->_set_error_message('email_must_be_array');
682 return FALSE;
683 }
684
685 foreach ($email as $val)
686 {
Derek Allard73274992008-05-05 16:39:18 +0000687 if (! $this->valid_email($val))
Derek Allard0fe82972008-01-15 13:58:47 +0000688 {
689 $this->_set_error_message('email_invalid_address', $val);
690 return FALSE;
691 }
692 }
693 }
694
695 // --------------------------------------------------------------------
696
697 /**
698 * Email Validation
699 *
700 * @access public
701 * @param string
702 * @return bool
703 */
704 function valid_email($address)
705 {
Derek Allard73274992008-05-05 16:39:18 +0000706 return (! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +0000707 }
708
709 // --------------------------------------------------------------------
710
711 /**
712 * Clean Extended Email Address: Joe Smith <joe@smith.com>
713 *
714 * @access public
715 * @param string
716 * @return string
717 */
718 function clean_email($email)
719 {
Derek Allard73274992008-05-05 16:39:18 +0000720 if (! is_array($email))
Derek Allard0fe82972008-01-15 13:58:47 +0000721 {
722 if (preg_match('/\<(.*)\>/', $email, $match))
Derek Allard20d24052008-05-12 22:12:11 +0000723 {
Derek Allard0fe82972008-01-15 13:58:47 +0000724 return $match['1'];
Derek Allard20d24052008-05-12 22:12:11 +0000725 }
Derek Allard0fe82972008-01-15 13:58:47 +0000726 else
Derek Allard20d24052008-05-12 22:12:11 +0000727 {
Derek Allard0fe82972008-01-15 13:58:47 +0000728 return $email;
Derek Allard20d24052008-05-12 22:12:11 +0000729 }
Derek Allard0fe82972008-01-15 13:58:47 +0000730 }
731
732 $clean_email = array();
Derek Allard80ddb6b2008-02-25 12:51:00 +0000733
Derek Jones80e14042008-01-16 17:46:56 +0000734 foreach ($email as $addy)
Derek Allard0fe82972008-01-15 13:58:47 +0000735 {
Derek Jones80e14042008-01-16 17:46:56 +0000736 if (preg_match( '/\<(.*)\>/', $addy, $match))
737 {
738 $clean_email[] = $match['1'];
739 }
Derek Allard0fe82972008-01-15 13:58:47 +0000740 else
Derek Jones80e14042008-01-16 17:46:56 +0000741 {
742 $clean_email[] = $addy;
743 }
Derek Allard0fe82972008-01-15 13:58:47 +0000744 }
745
746 return $clean_email;
747 }
748
749 // --------------------------------------------------------------------
750
751 /**
752 * Build alternative plain text message
753 *
754 * This function provides the raw message for use
755 * in plain-text headers of HTML-formatted emails.
756 * If the user hasn't specified his own alternative message
757 * it creates one by stripping the HTML
758 *
759 * @access private
760 * @return string
761 */
762 function _get_alt_message()
763 {
764 if ($this->alt_message != "")
765 {
766 return $this->word_wrap($this->alt_message, '76');
767 }
Derek Allard80ddb6b2008-02-25 12:51:00 +0000768
769 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
Derek Allard0fe82972008-01-15 13:58:47 +0000770 {
771 $body = $match['1'];
Derek Allard0fe82972008-01-15 13:58:47 +0000772 }
773 else
774 {
775 $body = $this->_body;
776 }
777
778 $body = trim(strip_tags($body));
779 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
780 $body = str_replace("\t", "", $body);
781
782 for ($i = 20; $i >= 3; $i--)
783 {
784 $n = "";
785
786 for ($x = 1; $x <= $i; $x ++)
Derek Allard20d24052008-05-12 22:12:11 +0000787 {
Derek Allard0fe82972008-01-15 13:58:47 +0000788 $n .= "\n";
Derek Allard20d24052008-05-12 22:12:11 +0000789 }
Derek Allard0fe82972008-01-15 13:58:47 +0000790
791 $body = str_replace($n, "\n\n", $body);
792 }
793
794 return $this->word_wrap($body, '76');
795 }
796
797 // --------------------------------------------------------------------
798
799 /**
800 * Word Wrap
801 *
802 * @access public
803 * @param string
804 * @param integer
805 * @return string
806 */
807 function word_wrap($str, $charlim = '')
808 {
809 // Se the character limit
810 if ($charlim == '')
811 {
812 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
813 }
814
815 // Reduce multiple spaces
816 $str = preg_replace("| +|", " ", $str);
817
818 // Standardize newlines
819 $str = preg_replace("/\r\n|\r/", "\n", $str);
820
821 // If the current word is surrounded by {unwrap} tags we'll
822 // strip the entire chunk and replace it with a marker.
823 $unwrap = array();
824 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
825 {
826 for ($i = 0; $i < count($matches['0']); $i++)
827 {
828 $unwrap[] = $matches['1'][$i];
829 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
830 }
831 }
832
833 // Use PHP's native function to do the initial wordwrap.
834 // We set the cut flag to FALSE so that any individual words that are
835 // too long get left alone. In the next step we'll deal with them.
836 $str = wordwrap($str, $charlim, "\n", FALSE);
837
838 // Split the string into individual lines of text and cycle through them
839 $output = "";
840 foreach (explode("\n", $str) as $line)
841 {
842 // Is the line within the allowed character count?
843 // If so we'll join it to the output and continue
844 if (strlen($line) <= $charlim)
845 {
846 $output .= $line.$this->newline;
847 continue;
848 }
849
850 $temp = '';
851 while((strlen($line)) > $charlim)
852 {
853 // If the over-length word is a URL we won't wrap it
854 if (preg_match("!\[url.+\]|://|wwww.!", $line))
855 {
856 break;
857 }
858
859 // Trim the word down
Derek Allard20d24052008-05-12 22:12:11 +0000860 $temp .= ($line, 0, $charlim-1);
Derek Allard0fe82972008-01-15 13:58:47 +0000861 $line = substr($line, $charlim-1);
862 }
863
864 // If $temp contains data it means we had to split up an over-length
865 // word into smaller chunks so we'll add it back to our current line
866 if ($temp != '')
867 {
868 $output .= $temp.$this->newline.$line;
869 }
870 else
871 {
872 $output .= $line;
873 }
874
875 $output .= $this->newline;
876 }
877
878 // Put our markers back
879 if (count($unwrap) > 0)
880 {
881 foreach ($unwrap as $key => $val)
882 {
883 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
884 }
885 }
886
887 return $output;
888 }
889
890 // --------------------------------------------------------------------
891
892 /**
893 * Build final headers
894 *
895 * @access public
896 * @param string
897 * @return string
898 */
899 function _build_headers()
900 {
901 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
902 $this->_set_header('X-Mailer', $this->useragent);
903 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
904 $this->_set_header('Message-ID', $this->_get_message_id());
905 $this->_set_header('Mime-Version', '1.0');
906 }
907
908 // --------------------------------------------------------------------
909
910 /**
911 * Write Headers as a string
912 *
913 * @access public
914 * @return void
915 */
916 function _write_headers()
917 {
918 if ($this->protocol == 'mail')
919 {
920 $this->_subject = $this->_headers['Subject'];
921 unset($this->_headers['Subject']);
922 }
923
924 reset($this->_headers);
925 $this->_header_str = "";
926
927 foreach($this->_headers as $key => $val)
928 {
929 $val = trim($val);
930
931 if ($val != "")
932 {
933 $this->_header_str .= $key.": ".$val.$this->newline;
934 }
935 }
936
937 if ($this->_get_protocol() == 'mail')
938 $this->_header_str = substr($this->_header_str, 0, -1);
939 }
940
941 // --------------------------------------------------------------------
942
943 /**
944 * Build Final Body and attachments
945 *
946 * @access public
947 * @return void
948 */
949 function _build_message()
950 {
951 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
952 {
953 $this->_body = $this->word_wrap($this->_body);
954 }
955
956 $this->_set_boundaries();
957 $this->_write_headers();
958
959 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
960
961 switch ($this->_get_content_type())
962 {
963 case 'plain' :
964
965 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
966 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
967
968 if ($this->_get_protocol() == 'mail')
969 {
970 $this->_header_str .= $hdr;
971 $this->_finalbody = $this->_body;
972
973 return;
974 }
975
976 $hdr .= $this->newline . $this->newline . $this->_body;
977
978 $this->_finalbody = $hdr;
979 return;
980
981 break;
982 case 'html' :
Derek Allard80ddb6b2008-02-25 12:51:00 +0000983
984 if ($this->send_multipart === FALSE)
985 {
986 $hdr .= "Content-Type: text/html;". $this->newline;
987 }
988 else
989 {
990 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline;
991 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
992 $hdr .= "--" . $this->_alt_boundary . $this->newline;
993
994 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
995 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
996 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
Derek Allard0fe82972008-01-15 13:58:47 +0000997
Derek Allard80ddb6b2008-02-25 12:51:00 +0000998 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
999 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1000 }
Derek Allard0fe82972008-01-15 13:58:47 +00001001
1002 $this->_body = $this->_prep_quoted_printable($this->_body);
1003
1004 if ($this->_get_protocol() == 'mail')
1005 {
1006 $this->_header_str .= $hdr;
Derek Allard80ddb6b2008-02-25 12:51:00 +00001007 $this->_finalbody = $this->_body . $this->newline . $this->newline;
1008
1009 if ($this->send_multipart !== FALSE)
1010 {
1011 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
1012 }
Derek Allard0fe82972008-01-15 13:58:47 +00001013
1014 return;
1015 }
1016
1017 $hdr .= $this->newline . $this->newline;
Derek Allard80ddb6b2008-02-25 12:51:00 +00001018 $hdr .= $this->_body . $this->newline . $this->newline;
1019
1020 if ($this->send_multipart !== FALSE)
1021 {
1022 $hdr .= "--" . $this->_alt_boundary . "--";
1023 }
Derek Allard0fe82972008-01-15 13:58:47 +00001024
1025 $this->_finalbody = $hdr;
1026 return;
1027
1028 break;
1029 case 'plain-attach' :
1030
1031 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
1032 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
1033 $hdr .= "--" . $this->_atc_boundary . $this->newline;
1034
1035 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1036 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1037
1038 if ($this->_get_protocol() == 'mail')
1039 {
1040 $this->_header_str .= $hdr;
1041
1042 $body = $this->_body . $this->newline . $this->newline;
1043 }
1044
1045 $hdr .= $this->newline . $this->newline;
1046 $hdr .= $this->_body . $this->newline . $this->newline;
1047
1048 break;
1049 case 'html-attach' :
1050
1051 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline;
1052 $hdr .= $this->_get_mime_message() . $this->newline . $this->newline;
1053 $hdr .= "--" . $this->_atc_boundary . $this->newline;
1054
1055 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1056 $hdr .= "--" . $this->_alt_boundary . $this->newline;
1057
1058 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1059 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1060 $hdr .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1061
1062 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1063 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1064
1065 $this->_body = $this->_prep_quoted_printable($this->_body);
1066
1067 if ($this->_get_protocol() == 'mail')
1068 {
1069 $this->_header_str .= $hdr;
1070
1071 $body = $this->_body . $this->newline . $this->newline;
1072 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
1073 }
1074
1075 $hdr .= $this->newline . $this->newline;
1076 $hdr .= $this->_body . $this->newline . $this->newline;
1077 $hdr .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
1078
1079 break;
1080 }
1081
1082 $attachment = array();
1083
1084 $z = 0;
1085
1086 for ($i=0; $i < count($this->_attach_name); $i++)
1087 {
1088 $filename = $this->_attach_name[$i];
1089 $basename = basename($filename);
1090 $ctype = $this->_attach_type[$i];
1091
Derek Allard73274992008-05-05 16:39:18 +00001092 if (! file_exists($filename))
Derek Allard0fe82972008-01-15 13:58:47 +00001093 {
1094 $this->_set_error_message('email_attachment_missing', $filename);
1095 return FALSE;
1096 }
1097
1098 $h = "--".$this->_atc_boundary.$this->newline;
1099 $h .= "Content-type: ".$ctype."; ";
1100 $h .= "name=\"".$basename."\"".$this->newline;
1101 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1102 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1103
1104 $attachment[$z++] = $h;
1105 $file = filesize($filename) +1;
1106
Derek Jones3be20e22008-05-05 20:07:09 +00001107 if (! $fp = fopen($filename, FOPEN_READ))
Derek Allard0fe82972008-01-15 13:58:47 +00001108 {
1109 $this->_set_error_message('email_attachment_unreadable', $filename);
1110 return FALSE;
1111 }
1112
1113 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1114 fclose($fp);
1115 }
1116
1117 if ($this->_get_protocol() == 'mail')
1118 {
1119 $this->_finalbody = $body . implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1120
1121 return;
1122 }
1123
1124 $this->_finalbody = $hdr.implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
1125
1126 return;
1127 }
1128
1129 // --------------------------------------------------------------------
1130
1131 /**
1132 * Prep Quoted Printable
1133 *
1134 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1135 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1136 *
1137 * @access public
1138 * @param string
1139 * @param integer
1140 * @return string
1141 */
1142 function _prep_quoted_printable($str, $charlim = '')
1143 {
1144 // Set the character limit
1145 // Don't allow over 76, as that will make servers and MUAs barf
1146 // all over quoted-printable data
1147 if ($charlim == '' OR $charlim > '76')
1148 {
1149 $charlim = '76';
1150 }
1151
1152 // Reduce multiple spaces
1153 $str = preg_replace("| +|", " ", $str);
1154
1155 // Standardize newlines
1156 $str = preg_replace("/\r\n|\r/", "\n", $str);
1157
1158 // We are intentionally wrapping so mail servers will encode characters
1159 // properly and MUAs will behave, so {unwrap} must go!
1160 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1161
1162 // Break into an array of lines
1163 $lines = preg_split("/\n/", $str);
1164
1165 $escape = '=';
1166 $output = '';
1167
1168 foreach ($lines as $line)
1169 {
1170 $length = strlen($line);
1171 $temp = '';
1172
1173 // Loop through each character in the line to add soft-wrap
1174 // characters at the end of a line " =\r\n" and add the newly
1175 // processed line(s) to the output (see comment on $crlf class property)
1176 for ($i = 0; $i < $length; $i++)
1177 {
1178 // Grab the next character
1179 $char = substr($line, $i, 1);
1180 $ascii = ord($char);
1181
1182 // Convert spaces and tabs but only if it's the end of the line
1183 if ($i == ($length - 1))
1184 {
1185 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($char)) : $char;
1186 }
1187
1188 // encode = signs
1189 if ($ascii == '61')
1190 {
1191 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
1192 }
1193
1194 // If we're at the character limit, add the line to the output,
1195 // reset our temp variable, and keep on chuggin'
1196 if ((strlen($temp) + strlen($char)) >= $charlim)
1197 {
1198 $output .= $temp.$escape.$this->crlf;
1199 $temp = '';
1200 }
1201
1202 // Add the character to our temporary line
1203 $temp .= $char;
1204 }
1205
1206 // Add our completed line to the output
1207 $output .= $temp.$this->crlf;
1208 }
1209
1210 // get rid of extra CRLF tacked onto the end
1211 $output = substr($output, 0, strlen($this->crlf) * -1);
1212
1213 return $output;
1214 }
1215
1216 // --------------------------------------------------------------------
1217
1218 /**
1219 * Send Email
1220 *
1221 * @access public
1222 * @return bool
1223 */
1224 function send()
1225 {
1226 if ($this->_replyto_flag == FALSE)
1227 {
1228 $this->reply_to($this->_headers['From']);
1229 }
1230
Derek Allard73274992008-05-05 16:39:18 +00001231 if ((! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
1232 (! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1233 (! isset($this->_headers['Cc'])))
Derek Allard0fe82972008-01-15 13:58:47 +00001234 {
1235 $this->_set_error_message('email_no_recipients');
1236 return FALSE;
1237 }
1238
1239 $this->_build_headers();
1240
1241 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
1242 {
1243 if (count($this->_bcc_array) > $this->bcc_batch_size)
1244 return $this->batch_bcc_send();
1245 }
1246
1247 $this->_build_message();
1248
Derek Allard73274992008-05-05 16:39:18 +00001249 if (! $this->_spool_email())
Derek Allard20d24052008-05-12 22:12:11 +00001250 {
Derek Allard0fe82972008-01-15 13:58:47 +00001251 return FALSE;
Derek Allard20d24052008-05-12 22:12:11 +00001252 }
Derek Allard0fe82972008-01-15 13:58:47 +00001253 else
Derek Allard20d24052008-05-12 22:12:11 +00001254 {
Derek Allard0fe82972008-01-15 13:58:47 +00001255 return TRUE;
Derek Allard20d24052008-05-12 22:12:11 +00001256 }
Derek Allard0fe82972008-01-15 13:58:47 +00001257 }
1258
1259 // --------------------------------------------------------------------
1260
1261 /**
1262 * Batch Bcc Send. Sends groups of BCCs in batches
1263 *
1264 * @access public
1265 * @return bool
1266 */
1267 function batch_bcc_send()
1268 {
1269 $float = $this->bcc_batch_size -1;
1270
1271 $flag = 0;
1272 $set = "";
1273
1274 $chunk = array();
1275
1276 for ($i = 0; $i < count($this->_bcc_array); $i++)
1277 {
1278 if (isset($this->_bcc_array[$i]))
Derek Allard20d24052008-05-12 22:12:11 +00001279 {
Derek Allard0fe82972008-01-15 13:58:47 +00001280 $set .= ", ".$this->_bcc_array[$i];
Derek Allard20d24052008-05-12 22:12:11 +00001281 }
Derek Allard0fe82972008-01-15 13:58:47 +00001282
1283 if ($i == $float)
1284 {
1285 $chunk[] = substr($set, 1);
1286 $float = $float + $this->bcc_batch_size;
1287 $set = "";
1288 }
1289
1290 if ($i == count($this->_bcc_array)-1)
Derek Allard20d24052008-05-12 22:12:11 +00001291 {
1292 $chunk[] = substr($set, 1);
1293 }
Derek Allard0fe82972008-01-15 13:58:47 +00001294 }
1295
1296 for ($i = 0; $i < count($chunk); $i++)
1297 {
1298 unset($this->_headers['Bcc']);
1299 unset($bcc);
1300
1301 $bcc = $this->_str_to_array($chunk[$i]);
1302 $bcc = $this->clean_email($bcc);
1303
1304 if ($this->protocol != 'smtp')
Derek Allard20d24052008-05-12 22:12:11 +00001305 {
Derek Allard0fe82972008-01-15 13:58:47 +00001306 $this->_set_header('Bcc', implode(", ", $bcc));
Derek Allard20d24052008-05-12 22:12:11 +00001307 }
Derek Allard0fe82972008-01-15 13:58:47 +00001308 else
Derek Allard20d24052008-05-12 22:12:11 +00001309 {
Derek Allard0fe82972008-01-15 13:58:47 +00001310 $this->_bcc_array = $bcc;
Derek Allard20d24052008-05-12 22:12:11 +00001311 }
Derek Allard0fe82972008-01-15 13:58:47 +00001312
1313 $this->_build_message();
1314 $this->_spool_email();
1315 }
1316 }
1317
1318 // --------------------------------------------------------------------
1319
1320 /**
1321 * Unwrap special elements
1322 *
1323 * @access private
1324 * @return void
1325 */
1326 function _unwrap_specials()
1327 {
1328 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1329 }
1330
1331 // --------------------------------------------------------------------
1332
1333 /**
1334 * Strip line-breaks via callback
1335 *
1336 * @access private
1337 * @return string
1338 */
1339 function _remove_nl_callback($matches)
1340 {
1341 return preg_replace("/(\r\n)|(\r)|(\n)/", "", $matches['1']);
1342 }
1343
1344 // --------------------------------------------------------------------
1345
1346 /**
1347 * Spool mail to the mail server
1348 *
1349 * @access private
1350 * @return bool
1351 */
1352 function _spool_email()
1353 {
1354 $this->_unwrap_specials();
1355
1356 switch ($this->_get_protocol())
1357 {
1358 case 'mail' :
1359
Derek Allard73274992008-05-05 16:39:18 +00001360 if (! $this->_send_with_mail())
Derek Allard0fe82972008-01-15 13:58:47 +00001361 {
1362 $this->_set_error_message('email_send_failure_phpmail');
1363 return FALSE;
1364 }
1365 break;
1366 case 'sendmail' :
1367
Derek Allard73274992008-05-05 16:39:18 +00001368 if (! $this->_send_with_sendmail())
Derek Allard0fe82972008-01-15 13:58:47 +00001369 {
1370 $this->_set_error_message('email_send_failure_sendmail');
1371 return FALSE;
1372 }
1373 break;
1374 case 'smtp' :
1375
Derek Allard73274992008-05-05 16:39:18 +00001376 if (! $this->_send_with_smtp())
Derek Allard0fe82972008-01-15 13:58:47 +00001377 {
1378 $this->_set_error_message('email_send_failure_smtp');
1379 return FALSE;
1380 }
1381 break;
1382
1383 }
1384
1385 $this->_set_error_message('email_sent', $this->_get_protocol());
Derek Jonese7c4c322008-01-22 18:16:39 +00001386 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001387 }
1388
1389 // --------------------------------------------------------------------
1390
1391 /**
1392 * Send using mail()
1393 *
1394 * @access private
1395 * @return bool
1396 */
1397 function _send_with_mail()
1398 {
1399 if ($this->_safe_mode == TRUE)
1400 {
Derek Allard73274992008-05-05 16:39:18 +00001401 if (! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
Derek Allard0fe82972008-01-15 13:58:47 +00001402 return FALSE;
1403 else
1404 return TRUE;
1405 }
1406 else
1407 {
1408 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1409 // we've encountered servers that seem to require it to be in place.
Derek Allard73274992008-05-05 16:39:18 +00001410 if (! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
Derek Allard0fe82972008-01-15 13:58:47 +00001411 return FALSE;
1412 else
1413 return TRUE;
1414 }
1415 }
1416
1417 // --------------------------------------------------------------------
1418
1419 /**
1420 * Send using Sendmail
1421 *
1422 * @access private
1423 * @return bool
1424 */
1425 function _send_with_sendmail()
1426 {
1427 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1428
Derek Allard73274992008-05-05 16:39:18 +00001429 if (! is_resource($fp))
Derek Allard0fe82972008-01-15 13:58:47 +00001430 {
1431 $this->_set_error_message('email_no_socket');
1432 return FALSE;
1433 }
1434
1435 fputs($fp, $this->_header_str);
1436 fputs($fp, $this->_finalbody);
1437 pclose($fp) >> 8 & 0xFF;
1438
1439 return TRUE;
1440 }
1441
1442 // --------------------------------------------------------------------
1443
1444 /**
1445 * Send using SMTP
1446 *
1447 * @access private
1448 * @return bool
1449 */
1450 function _send_with_smtp()
1451 {
1452 if ($this->smtp_host == '')
1453 {
1454 $this->_set_error_message('email_no_hostname');
1455 return FALSE;
1456 }
1457
1458 $this->_smtp_connect();
1459 $this->_smtp_authenticate();
1460
1461 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1462
1463 foreach($this->_recipients as $val)
1464 $this->_send_command('to', $val);
1465
1466 if (count($this->_cc_array) > 0)
1467 {
1468 foreach($this->_cc_array as $val)
1469 {
1470 if ($val != "")
1471 $this->_send_command('to', $val);
1472 }
1473 }
1474
1475 if (count($this->_bcc_array) > 0)
1476 {
1477 foreach($this->_bcc_array as $val)
1478 {
1479 if ($val != "")
1480 $this->_send_command('to', $val);
1481 }
1482 }
1483
1484 $this->_send_command('data');
Derek Jonesaf4a8a02008-05-08 22:16:33 +00001485
1486 // perform dot transformation on any lines that begin with a dot
1487 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
Derek Allard0fe82972008-01-15 13:58:47 +00001488
1489 $this->_send_data('.');
1490
1491 $reply = $this->_get_smtp_data();
1492
1493 $this->_set_error_message($reply);
1494
Derek Allard20d24052008-05-12 22:12:11 +00001495 if (strncmp($reply, '250', 3) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +00001496 {
1497 $this->_set_error_message('email_smtp_error', $reply);
1498 return FALSE;
1499 }
1500
1501 $this->_send_command('quit');
Derek Jonese7c4c322008-01-22 18:16:39 +00001502 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001503 }
1504
1505 // --------------------------------------------------------------------
1506
1507 /**
1508 * SMTP Connect
1509 *
1510 * @access public
1511 * @param string
1512 * @return string
1513 */
1514 function _smtp_connect()
1515 {
Derek Allard0fe82972008-01-15 13:58:47 +00001516 $this->_smtp_connect = fsockopen($this->smtp_host,
1517 $this->smtp_port,
1518 $errno,
1519 $errstr,
1520 $this->smtp_timeout);
1521
Derek Allard73274992008-05-05 16:39:18 +00001522 if(! is_resource($this->_smtp_connect))
Derek Allard0fe82972008-01-15 13:58:47 +00001523 {
1524 $this->_set_error_message('email_smtp_error', $errno." ".$errstr);
1525 return FALSE;
1526 }
1527
1528 $this->_set_error_message($this->_get_smtp_data());
1529 return $this->_send_command('hello');
1530 }
1531
1532 // --------------------------------------------------------------------
1533
1534 /**
1535 * Send SMTP command
1536 *
1537 * @access private
1538 * @param string
1539 * @param string
1540 * @return string
1541 */
1542 function _send_command($cmd, $data = '')
1543 {
1544 switch ($cmd)
1545 {
1546 case 'hello' :
1547
1548 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1549 $this->_send_data('EHLO '.$this->_get_hostname());
1550 else
1551 $this->_send_data('HELO '.$this->_get_hostname());
1552
1553 $resp = 250;
1554 break;
1555 case 'from' :
1556
1557 $this->_send_data('MAIL FROM:<'.$data.'>');
1558
1559 $resp = 250;
1560 break;
1561 case 'to' :
1562
1563 $this->_send_data('RCPT TO:<'.$data.'>');
1564
1565 $resp = 250;
1566 break;
1567 case 'data' :
1568
1569 $this->_send_data('DATA');
1570
1571 $resp = 354;
1572 break;
1573 case 'quit' :
1574
1575 $this->_send_data('QUIT');
1576
1577 $resp = 221;
1578 break;
1579 }
1580
1581 $reply = $this->_get_smtp_data();
1582
1583 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1584
1585 if (substr($reply, 0, 3) != $resp)
1586 {
1587 $this->_set_error_message('email_smtp_error', $reply);
1588 return FALSE;
1589 }
1590
1591 if ($cmd == 'quit')
Derek Allard20d24052008-05-12 22:12:11 +00001592 {
Derek Allard0fe82972008-01-15 13:58:47 +00001593 fclose($this->_smtp_connect);
Derek Allard20d24052008-05-12 22:12:11 +00001594 }
Derek Allard0fe82972008-01-15 13:58:47 +00001595
Derek Jonese7c4c322008-01-22 18:16:39 +00001596 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001597 }
1598
1599 // --------------------------------------------------------------------
1600
1601 /**
1602 * SMTP Authenticate
1603 *
1604 * @access private
1605 * @return bool
1606 */
1607 function _smtp_authenticate()
1608 {
Derek Allard73274992008-05-05 16:39:18 +00001609 if (! $this->_smtp_auth)
Derek Allard20d24052008-05-12 22:12:11 +00001610 {
Derek Jonese7c4c322008-01-22 18:16:39 +00001611 return TRUE;
Derek Allard20d24052008-05-12 22:12:11 +00001612 }
Derek Allard0fe82972008-01-15 13:58:47 +00001613
1614 if ($this->smtp_user == "" AND $this->smtp_pass == "")
1615 {
1616 $this->_set_error_message('email_no_smtp_unpw');
1617 return FALSE;
1618 }
1619
1620 $this->_send_data('AUTH LOGIN');
1621
1622 $reply = $this->_get_smtp_data();
1623
Derek Allard20d24052008-05-12 22:12:11 +00001624 if (strncmp($reply, '334', 3) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +00001625 {
1626 $this->_set_error_message('email_failed_smtp_login', $reply);
1627 return FALSE;
1628 }
1629
1630 $this->_send_data(base64_encode($this->smtp_user));
1631
1632 $reply = $this->_get_smtp_data();
1633
Derek Allard20d24052008-05-12 22:12:11 +00001634 if (strncmp($reply, '334', 3) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +00001635 {
1636 $this->_set_error_message('email_smtp_auth_un', $reply);
1637 return FALSE;
1638 }
1639
1640 $this->_send_data(base64_encode($this->smtp_pass));
1641
1642 $reply = $this->_get_smtp_data();
1643
Derek Allard20d24052008-05-12 22:12:11 +00001644 if (strncmp($reply, '235', 3) != 0)
Derek Allard0fe82972008-01-15 13:58:47 +00001645 {
1646 $this->_set_error_message('email_smtp_auth_pw', $reply);
1647 return FALSE;
1648 }
1649
Derek Jonese7c4c322008-01-22 18:16:39 +00001650 return TRUE;
Derek Allard0fe82972008-01-15 13:58:47 +00001651 }
1652
1653 // --------------------------------------------------------------------
1654
1655 /**
1656 * Send SMTP data
1657 *
1658 * @access private
1659 * @return bool
1660 */
1661 function _send_data($data)
1662 {
Derek Allard73274992008-05-05 16:39:18 +00001663 if (! fwrite($this->_smtp_connect, $data . $this->newline))
Derek Allard0fe82972008-01-15 13:58:47 +00001664 {
1665 $this->_set_error_message('email_smtp_data_failure', $data);
1666 return FALSE;
1667 }
1668 else
Derek Allard20d24052008-05-12 22:12:11 +00001669 {
Derek Jonese7c4c322008-01-22 18:16:39 +00001670 return TRUE;
Derek Allard20d24052008-05-12 22:12:11 +00001671 }
Derek Allard0fe82972008-01-15 13:58:47 +00001672 }
1673
1674 // --------------------------------------------------------------------
1675
1676 /**
1677 * Get SMTP data
1678 *
1679 * @access private
1680 * @return string
1681 */
1682 function _get_smtp_data()
1683 {
1684 $data = "";
1685
1686 while ($str = fgets($this->_smtp_connect, 512))
1687 {
1688 $data .= $str;
1689
1690 if (substr($str, 3, 1) == " ")
Derek Allard20d24052008-05-12 22:12:11 +00001691 {
Derek Allard80ddb6b2008-02-25 12:51:00 +00001692 break;
Derek Allard20d24052008-05-12 22:12:11 +00001693 }
Derek Allard0fe82972008-01-15 13:58:47 +00001694 }
1695
1696 return $data;
1697 }
1698
1699 // --------------------------------------------------------------------
1700
1701 /**
1702 * Get Hostname
1703 *
1704 * @access private
1705 * @return string
1706 */
1707 function _get_hostname()
1708 {
1709 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1710 }
1711
1712 // --------------------------------------------------------------------
1713
1714 /**
1715 * Get IP
1716 *
1717 * @access private
1718 * @return string
1719 */
1720 function _get_ip()
1721 {
1722 if ($this->_IP !== FALSE)
1723 {
1724 return $this->_IP;
1725 }
1726
1727 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1728 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1729 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1730
1731 if ($cip && $rip) $this->_IP = $cip;
1732 elseif ($rip) $this->_IP = $rip;
1733 elseif ($cip) $this->_IP = $cip;
1734 elseif ($fip) $this->_IP = $fip;
1735
1736 if (strstr($this->_IP, ','))
1737 {
1738 $x = explode(',', $this->_IP);
1739 $this->_IP = end($x);
1740 }
1741
Derek Allard73274992008-05-05 16:39:18 +00001742 if (! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
Derek Allard20d24052008-05-12 22:12:11 +00001743 {
Derek Allard0fe82972008-01-15 13:58:47 +00001744 $this->_IP = '0.0.0.0';
Derek Allard20d24052008-05-12 22:12:11 +00001745 }
Derek Allard0fe82972008-01-15 13:58:47 +00001746
1747 unset($cip);
1748 unset($rip);
1749 unset($fip);
1750
1751 return $this->_IP;
1752 }
1753
1754 // --------------------------------------------------------------------
1755
1756 /**
1757 * Get Debug Message
1758 *
1759 * @access public
1760 * @return string
1761 */
1762 function print_debugger()
1763 {
1764 $msg = '';
1765
1766 if (count($this->_debug_msg) > 0)
1767 {
1768 foreach ($this->_debug_msg as $val)
1769 {
1770 $msg .= $val;
1771 }
1772 }
1773
1774 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1775 return $msg;
1776 }
1777
1778 // --------------------------------------------------------------------
1779
1780 /**
1781 * Set Message
1782 *
1783 * @access public
1784 * @param string
1785 * @return string
1786 */
1787 function _set_error_message($msg, $val = '')
1788 {
1789 $CI =& get_instance();
1790 $CI->lang->load('email');
1791
1792 if (FALSE === ($line = $CI->lang->line($msg)))
1793 {
1794 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1795 }
1796 else
1797 {
1798 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1799 }
1800 }
1801
1802 // --------------------------------------------------------------------
1803
1804 /**
1805 * Mime Types
1806 *
1807 * @access private
1808 * @param string
1809 * @return string
1810 */
1811 function _mime_types($ext = "")
1812 {
1813 $mimes = array( 'hqx' => 'application/mac-binhex40',
1814 'cpt' => 'application/mac-compactpro',
1815 'doc' => 'application/msword',
1816 'bin' => 'application/macbinary',
1817 'dms' => 'application/octet-stream',
1818 'lha' => 'application/octet-stream',
1819 'lzh' => 'application/octet-stream',
1820 'exe' => 'application/octet-stream',
1821 'class' => 'application/octet-stream',
1822 'psd' => 'application/octet-stream',
1823 'so' => 'application/octet-stream',
1824 'sea' => 'application/octet-stream',
1825 'dll' => 'application/octet-stream',
1826 'oda' => 'application/oda',
1827 'pdf' => 'application/pdf',
1828 'ai' => 'application/postscript',
1829 'eps' => 'application/postscript',
1830 'ps' => 'application/postscript',
1831 'smi' => 'application/smil',
1832 'smil' => 'application/smil',
1833 'mif' => 'application/vnd.mif',
1834 'xls' => 'application/vnd.ms-excel',
1835 'ppt' => 'application/vnd.ms-powerpoint',
1836 'wbxml' => 'application/vnd.wap.wbxml',
1837 'wmlc' => 'application/vnd.wap.wmlc',
1838 'dcr' => 'application/x-director',
1839 'dir' => 'application/x-director',
1840 'dxr' => 'application/x-director',
1841 'dvi' => 'application/x-dvi',
1842 'gtar' => 'application/x-gtar',
1843 'php' => 'application/x-httpd-php',
1844 'php4' => 'application/x-httpd-php',
1845 'php3' => 'application/x-httpd-php',
1846 'phtml' => 'application/x-httpd-php',
1847 'phps' => 'application/x-httpd-php-source',
1848 'js' => 'application/x-javascript',
1849 'swf' => 'application/x-shockwave-flash',
1850 'sit' => 'application/x-stuffit',
1851 'tar' => 'application/x-tar',
1852 'tgz' => 'application/x-tar',
1853 'xhtml' => 'application/xhtml+xml',
1854 'xht' => 'application/xhtml+xml',
1855 'zip' => 'application/zip',
1856 'mid' => 'audio/midi',
1857 'midi' => 'audio/midi',
1858 'mpga' => 'audio/mpeg',
1859 'mp2' => 'audio/mpeg',
1860 'mp3' => 'audio/mpeg',
1861 'aif' => 'audio/x-aiff',
1862 'aiff' => 'audio/x-aiff',
1863 'aifc' => 'audio/x-aiff',
1864 'ram' => 'audio/x-pn-realaudio',
1865 'rm' => 'audio/x-pn-realaudio',
1866 'rpm' => 'audio/x-pn-realaudio-plugin',
1867 'ra' => 'audio/x-realaudio',
1868 'rv' => 'video/vnd.rn-realvideo',
1869 'wav' => 'audio/x-wav',
1870 'bmp' => 'image/bmp',
1871 'gif' => 'image/gif',
1872 'jpeg' => 'image/jpeg',
1873 'jpg' => 'image/jpeg',
1874 'jpe' => 'image/jpeg',
1875 'png' => 'image/png',
1876 'tiff' => 'image/tiff',
1877 'tif' => 'image/tiff',
1878 'css' => 'text/css',
1879 'html' => 'text/html',
1880 'htm' => 'text/html',
1881 'shtml' => 'text/html',
1882 'txt' => 'text/plain',
1883 'text' => 'text/plain',
1884 'log' => 'text/plain',
1885 'rtx' => 'text/richtext',
1886 'rtf' => 'text/rtf',
1887 'xml' => 'text/xml',
1888 'xsl' => 'text/xml',
1889 'mpeg' => 'video/mpeg',
1890 'mpg' => 'video/mpeg',
1891 'mpe' => 'video/mpeg',
1892 'qt' => 'video/quicktime',
1893 'mov' => 'video/quicktime',
1894 'avi' => 'video/x-msvideo',
1895 'movie' => 'video/x-sgi-movie',
1896 'doc' => 'application/msword',
1897 'word' => 'application/msword',
1898 'xl' => 'application/excel',
1899 'eml' => 'message/rfc822'
1900 );
1901
Derek Allard73274992008-05-05 16:39:18 +00001902 return (! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
Derek Allard0fe82972008-01-15 13:58:47 +00001903 }
1904
1905}
1906// END CI_Email class
Derek Allard20d24052008-05-12 22:12:11 +00001907
1908/* End of file Email.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001909/* Location: ./system/libraries/Email.php */