blob: 83a4eefb193220c22c44a36a4951b9957cbb2f09 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Email Class
20 *
21 * Permits email to be sent using Mail, Sendmail, or SMTP.
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/email.html
28 */
29class CI_Email {
30
31 var $useragent = "CodeIgniter";
32 var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
33 var $protocol = "mail"; // mail/sendmail/smtp
Derek Jones37f4b9c2011-07-01 17:56:50 -050034 var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
Derek Allard2067d1a2008-11-13 22:59:24 +000035 var $smtp_user = ""; // SMTP Username
36 var $smtp_pass = ""; // SMTP Password
37 var $smtp_port = "25"; // SMTP Port
38 var $smtp_timeout = 5; // SMTP Timeout in seconds
Radu Potopbbf04b02011-09-28 13:57:51 +030039 var $smtp_crypto = ""; // SMTP Encryption. Can be null, tls or ssl.
Derek Jones37f4b9c2011-07-01 17:56:50 -050040 var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
Derek Allard2067d1a2008-11-13 22:59:24 +000041 var $wrapchars = "76"; // Number of characters to wrap at.
Derek Jones37f4b9c2011-07-01 17:56:50 -050042 var $mailtype = "text"; // text/html Defines email formatting
Derek Allard2067d1a2008-11-13 22:59:24 +000043 var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
44 var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
45 var $alt_message = ''; // Alternative message for HTML emails
Derek Jones37f4b9c2011-07-01 17:56:50 -050046 var $validate = FALSE; // TRUE/FALSE. Enables email validation
Derek Allard2067d1a2008-11-13 22:59:24 +000047 var $priority = "3"; // Default priority (1 - 5)
48 var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
Derek Jones37f4b9c2011-07-01 17:56:50 -050049 var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
Derek Allard2067d1a2008-11-13 22:59:24 +000050 // even on the receiving end think they need to muck with CRLFs, so using "\n", while
51 // distasteful, is the only thing that seems to work for all environments.
Derek Jones37f4b9c2011-07-01 17:56:50 -050052 var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
53 var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
55 var $_safe_mode = FALSE;
56 var $_subject = "";
57 var $_body = "";
58 var $_finalbody = "";
59 var $_alt_boundary = "";
60 var $_atc_boundary = "";
61 var $_header_str = "";
62 var $_smtp_connect = "";
63 var $_encoding = "8bit";
64 var $_IP = FALSE;
65 var $_smtp_auth = FALSE;
66 var $_replyto_flag = FALSE;
67 var $_debug_msg = array();
68 var $_recipients = array();
69 var $_cc_array = array();
70 var $_bcc_array = array();
71 var $_headers = array();
72 var $_attach_name = array();
73 var $_attach_type = array();
74 var $_attach_disp = array();
75 var $_protocols = array('mail', 'sendmail', 'smtp');
76 var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
77 var $_bit_depths = array('7bit', '8bit');
78 var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
79
80
81 /**
82 * Constructor - Sets Email Preferences
83 *
84 * The constructor can be passed an array of config values
85 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +000086 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 if (count($config) > 0)
89 {
90 $this->initialize($config);
91 }
92 else
93 {
94 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
95 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
96 }
97
98 log_message('debug', "Email Class Initialized");
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Initialize preferences
105 *
106 * @access public
107 * @param array
108 * @return void
109 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000110 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 foreach ($config as $key => $val)
113 {
114 if (isset($this->$key))
115 {
116 $method = 'set_'.$key;
117
118 if (method_exists($this, $method))
119 {
120 $this->$method($val);
121 }
122 else
123 {
124 $this->$key = $val;
125 }
126 }
127 }
Eric Barnes6113f542010-12-29 13:36:12 -0500128 $this->clear();
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
131 $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000132
133 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
137
138 /**
139 * Initialize the Email Data
140 *
141 * @access public
Bo-Yi Wu83320eb2011-09-15 13:28:02 +0800142 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 * @return void
144 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000145 public function clear($clear_attachments = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 $this->_subject = "";
148 $this->_body = "";
149 $this->_finalbody = "";
150 $this->_header_str = "";
151 $this->_replyto_flag = FALSE;
152 $this->_recipients = array();
Derek Jonesd1606352010-09-01 11:16:07 -0500153 $this->_cc_array = array();
154 $this->_bcc_array = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $this->_headers = array();
156 $this->_debug_msg = array();
157
158 $this->_set_header('User-Agent', $this->useragent);
159 $this->_set_header('Date', $this->_set_date());
160
161 if ($clear_attachments !== FALSE)
162 {
163 $this->_attach_name = array();
164 $this->_attach_type = array();
165 $this->_attach_disp = array();
166 }
Eric Barnes6113f542010-12-29 13:36:12 -0500167
Greg Akera769deb2010-11-10 15:47:29 -0600168 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
174 * Set FROM
175 *
176 * @access public
177 * @param string
178 * @param string
179 * @return void
180 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000181 public function from($from, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
183 if (preg_match( '/\<(.*)\>/', $from, $match))
184 {
185 $from = $match['1'];
186 }
187
188 if ($this->validate)
189 {
190 $this->validate_email($this->_str_to_array($from));
191 }
192
193 // prepare the display name
194 if ($name != '')
195 {
196 // only use Q encoding if there are characters that would require it
197 if ( ! preg_match('/[\200-\377]/', $name))
198 {
199 // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
Derek Jonesc630bcf2008-11-17 21:09:45 +0000200 $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202 else
203 {
204 $name = $this->_prep_q_encoding($name, TRUE);
205 }
206 }
207
208 $this->_set_header('From', $name.' <'.$from.'>');
209 $this->_set_header('Return-Path', '<'.$from.'>');
Eric Barnes6113f542010-12-29 13:36:12 -0500210
Greg Akera769deb2010-11-10 15:47:29 -0600211 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
215
216 /**
217 * Set Reply-to
218 *
219 * @access public
220 * @param string
221 * @param string
222 * @return void
223 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000224 public function reply_to($replyto, $name = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 if (preg_match( '/\<(.*)\>/', $replyto, $match))
227 {
228 $replyto = $match['1'];
229 }
230
231 if ($this->validate)
232 {
233 $this->validate_email($this->_str_to_array($replyto));
234 }
235
236 if ($name == '')
237 {
238 $name = $replyto;
239 }
240
241 if (strncmp($name, '"', 1) != 0)
242 {
243 $name = '"'.$name.'"';
244 }
245
246 $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
247 $this->_replyto_flag = TRUE;
Greg Akera769deb2010-11-10 15:47:29 -0600248
249 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // --------------------------------------------------------------------
253
254 /**
255 * Set Recipients
256 *
257 * @access public
258 * @param string
259 * @return void
260 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000261 public function to($to)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $to = $this->_str_to_array($to);
264 $to = $this->clean_email($to);
265
266 if ($this->validate)
267 {
268 $this->validate_email($to);
269 }
270
271 if ($this->_get_protocol() != 'mail')
272 {
273 $this->_set_header('To', implode(", ", $to));
274 }
275
276 switch ($this->_get_protocol())
277 {
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000278 case 'smtp' :
279 $this->_recipients = $to;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 break;
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000281 case 'sendmail' :
282 case 'mail' :
283 $this->_recipients = implode(", ", $to);
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 break;
285 }
Eric Barnes6113f542010-12-29 13:36:12 -0500286
Greg Akera769deb2010-11-10 15:47:29 -0600287 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Set CC
294 *
295 * @access public
296 * @param string
297 * @return void
298 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000299 public function cc($cc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 $cc = $this->_str_to_array($cc);
302 $cc = $this->clean_email($cc);
303
304 if ($this->validate)
305 {
306 $this->validate_email($cc);
307 }
308
309 $this->_set_header('Cc', implode(", ", $cc));
310
311 if ($this->_get_protocol() == "smtp")
312 {
313 $this->_cc_array = $cc;
314 }
Eric Barnes6113f542010-12-29 13:36:12 -0500315
Greg Akera769deb2010-11-10 15:47:29 -0600316 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
320
321 /**
322 * Set BCC
323 *
324 * @access public
325 * @param string
326 * @param string
327 * @return void
328 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000329 public function bcc($bcc, $limit = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 if ($limit != '' && is_numeric($limit))
332 {
333 $this->bcc_batch_mode = TRUE;
334 $this->bcc_batch_size = $limit;
335 }
336
337 $bcc = $this->_str_to_array($bcc);
338 $bcc = $this->clean_email($bcc);
339
340 if ($this->validate)
341 {
342 $this->validate_email($bcc);
343 }
344
345 if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
346 {
347 $this->_bcc_array = $bcc;
348 }
349 else
350 {
351 $this->_set_header('Bcc', implode(", ", $bcc));
352 }
Eric Barnes6113f542010-12-29 13:36:12 -0500353
Greg Akera769deb2010-11-10 15:47:29 -0600354 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
358
359 /**
360 * Set Email Subject
361 *
362 * @access public
363 * @param string
364 * @return void
365 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000366 public function subject($subject)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $subject = $this->_prep_q_encoding($subject);
369 $this->_set_header('Subject', $subject);
Greg Akera769deb2010-11-10 15:47:29 -0600370 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 // --------------------------------------------------------------------
374
375 /**
376 * Set Body
377 *
378 * @access public
379 * @param string
380 * @return void
381 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000382 public function message($body)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
diegorivera33c32802011-10-19 02:56:15 -0200384 $this->_body = rtrim(str_replace("\r", "", $body));
385
Andrey Andreevaf728622011-10-20 10:11:59 +0300386 /* strip slashes only if magic quotes is ON
387 if we do it with magic quotes OFF, it strips real, user-inputted chars.
388
389 NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
390 it will probably not exist in future versions at all.
391 */
392 if ( ! is_php('5.4') && get_magic_quotes_gpc())
diegorivera9fca6152011-10-19 11:18:45 -0200393 {
diegorivera33c32802011-10-19 02:56:15 -0200394 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200395 }
diegorivera33c32802011-10-19 02:56:15 -0200396
Greg Akera769deb2010-11-10 15:47:29 -0600397 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
401
402 /**
403 * Assign file attachments
404 *
405 * @access public
406 * @param string
407 * @return void
408 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000409 public function attach($filename, $disposition = 'attachment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
411 $this->_attach_name[] = $filename;
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600412 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Derek Jones37f4b9c2011-07-01 17:56:50 -0500413 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600414 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Add a Header Item
421 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600422 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @param string
424 * @param string
425 * @return void
426 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600427 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 $this->_headers[$header] = $value;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // --------------------------------------------------------------------
433
434 /**
435 * Convert a String to an Array
436 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600437 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param string
439 * @return array
440 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600441 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ( ! is_array($email))
444 {
445 if (strpos($email, ',') !== FALSE)
446 {
447 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
448 }
449 else
450 {
451 $email = trim($email);
452 settype($email, "array");
453 }
454 }
455 return $email;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * Set Multipart Value
462 *
463 * @access public
464 * @param string
465 * @return void
466 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000467 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400469 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600470 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
474
475 /**
476 * Set Mailtype
477 *
478 * @access public
479 * @param string
480 * @return void
481 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000482 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600485 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // --------------------------------------------------------------------
489
490 /**
491 * Set Wordwrap
492 *
493 * @access public
Dan Horrigan628e6602011-08-21 09:08:31 -0400494 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @return void
496 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000497 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400499 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600500 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 // --------------------------------------------------------------------
504
505 /**
506 * Set Protocol
507 *
508 * @access public
509 * @param string
510 * @return void
511 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000512 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
Greg Akera769deb2010-11-10 15:47:29 -0600515 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Set Priority
522 *
523 * @access public
524 * @param integer
525 * @return void
526 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000527 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
529 if ( ! is_numeric($n))
530 {
531 $this->priority = 3;
532 return;
533 }
534
535 if ($n < 1 OR $n > 5)
536 {
537 $this->priority = 3;
538 return;
539 }
540
541 $this->priority = $n;
Greg Akera769deb2010-11-10 15:47:29 -0600542 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // --------------------------------------------------------------------
546
547 /**
548 * Set Newline Character
549 *
550 * @access public
551 * @param string
552 * @return void
553 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000554 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
556 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
557 {
558 $this->newline = "\n";
559 return;
560 }
561
562 $this->newline = $newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500563
Greg Akera769deb2010-11-10 15:47:29 -0600564 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // --------------------------------------------------------------------
568
569 /**
570 * Set CRLF
571 *
572 * @access public
573 * @param string
574 * @return void
575 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000576 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
579 {
580 $this->crlf = "\n";
581 return;
582 }
583
584 $this->crlf = $crlf;
Eric Barnes6113f542010-12-29 13:36:12 -0500585
Greg Akera769deb2010-11-10 15:47:29 -0600586 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 // --------------------------------------------------------------------
590
591 /**
592 * Set Message Boundary
593 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600594 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @return void
596 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600597 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
600 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
601 }
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 // --------------------------------------------------------------------
604
605 /**
606 * Get the Message ID
607 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600608 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * @return string
610 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600611 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 $from = $this->_headers['Return-Path'];
614 $from = str_replace(">", "", $from);
615 $from = str_replace("<", "", $from);
616
Derek Jones37f4b9c2011-07-01 17:56:50 -0500617 return "<".uniqid('').strstr($from, '@').">";
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // --------------------------------------------------------------------
621
622 /**
623 * Get Mail Protocol
624 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600625 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @param bool
627 * @return string
628 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600629 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
631 $this->protocol = strtolower($this->protocol);
632 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
633
634 if ($return == TRUE)
635 {
636 return $this->protocol;
637 }
638 }
Barry Mienydd671972010-10-04 16:33:58 +0200639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 // --------------------------------------------------------------------
641
642 /**
643 * Get Mail Encoding
644 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600645 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 * @param bool
647 * @return string
648 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600649 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
652
653 foreach ($this->_base_charsets as $charset)
654 {
655 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
656 {
657 $this->_encoding = '7bit';
658 }
659 }
660
661 if ($return == TRUE)
662 {
663 return $this->_encoding;
664 }
665 }
666
667 // --------------------------------------------------------------------
668
669 /**
670 * Get content type (text/html/attachment)
671 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600672 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @return string
674 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600675 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500677 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 return 'html';
680 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500681 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 return 'html-attach';
684 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500685 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 return 'plain-attach';
688 }
689 else
690 {
691 return 'plain';
692 }
693 }
Barry Mienydd671972010-10-04 16:33:58 +0200694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 // --------------------------------------------------------------------
696
697 /**
698 * Set RFC 822 Date
699 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600700 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 * @return string
702 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600703 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 {
705 $timezone = date("Z");
706 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
707 $timezone = abs($timezone);
708 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
709
710 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
711 }
Barry Mienydd671972010-10-04 16:33:58 +0200712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 // --------------------------------------------------------------------
714
715 /**
716 * Mime message
717 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600718 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 * @return string
720 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600721 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 {
723 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
724 }
Barry Mienydd671972010-10-04 16:33:58 +0200725
Derek Allard2067d1a2008-11-13 22:59:24 +0000726 // --------------------------------------------------------------------
727
728 /**
729 * Validate Email Address
730 *
731 * @access public
732 * @param string
733 * @return bool
734 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000735 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 {
737 if ( ! is_array($email))
738 {
patworkb0707982011-04-08 15:10:05 +0200739 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000740 return FALSE;
741 }
742
743 foreach ($email as $val)
744 {
745 if ( ! $this->valid_email($val))
746 {
patworkb0707982011-04-08 15:10:05 +0200747 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 return FALSE;
749 }
750 }
751
752 return TRUE;
753 }
Barry Mienydd671972010-10-04 16:33:58 +0200754
Derek Allard2067d1a2008-11-13 22:59:24 +0000755 // --------------------------------------------------------------------
756
757 /**
758 * Email Validation
759 *
760 * @access public
761 * @param string
762 * @return bool
763 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000764 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 {
766 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
767 }
Barry Mienydd671972010-10-04 16:33:58 +0200768
Derek Allard2067d1a2008-11-13 22:59:24 +0000769 // --------------------------------------------------------------------
770
771 /**
772 * Clean Extended Email Address: Joe Smith <joe@smith.com>
773 *
774 * @access public
775 * @param string
776 * @return string
777 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000778 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000779 {
780 if ( ! is_array($email))
781 {
782 if (preg_match('/\<(.*)\>/', $email, $match))
783 {
Barry Mienydd671972010-10-04 16:33:58 +0200784 return $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 {
Barry Mienydd671972010-10-04 16:33:58 +0200788 return $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 }
790 }
791
792 $clean_email = array();
793
794 foreach ($email as $addy)
795 {
796 if (preg_match( '/\<(.*)\>/', $addy, $match))
797 {
Barry Mienydd671972010-10-04 16:33:58 +0200798 $clean_email[] = $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
Barry Mienydd671972010-10-04 16:33:58 +0200800 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 {
Barry Mienydd671972010-10-04 16:33:58 +0200802 $clean_email[] = $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000803 }
804 }
805
806 return $clean_email;
807 }
Barry Mienydd671972010-10-04 16:33:58 +0200808
Derek Allard2067d1a2008-11-13 22:59:24 +0000809 // --------------------------------------------------------------------
810
811 /**
812 * Build alternative plain text message
813 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000814 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 * in plain-text headers of HTML-formatted emails.
816 * If the user hasn't specified his own alternative message
817 * it creates one by stripping the HTML
818 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600819 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000820 * @return string
821 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600822 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000823 {
824 if ($this->alt_message != "")
825 {
826 return $this->word_wrap($this->alt_message, '76');
827 }
828
829 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
830 {
831 $body = $match['1'];
832 }
833 else
834 {
835 $body = $this->_body;
836 }
837
838 $body = trim(strip_tags($body));
839 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
840 $body = str_replace("\t", "", $body);
841
842 for ($i = 20; $i >= 3; $i--)
843 {
844 $n = "";
845
846 for ($x = 1; $x <= $i; $x ++)
847 {
Barry Mienydd671972010-10-04 16:33:58 +0200848 $n .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 }
850
851 $body = str_replace($n, "\n\n", $body);
852 }
853
854 return $this->word_wrap($body, '76');
855 }
Barry Mienydd671972010-10-04 16:33:58 +0200856
Derek Allard2067d1a2008-11-13 22:59:24 +0000857 // --------------------------------------------------------------------
858
859 /**
860 * Word Wrap
861 *
862 * @access public
863 * @param string
864 * @param integer
865 * @return string
866 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000867 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000868 {
869 // Se the character limit
870 if ($charlim == '')
871 {
872 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
873 }
874
875 // Reduce multiple spaces
876 $str = preg_replace("| +|", " ", $str);
877
878 // Standardize newlines
879 if (strpos($str, "\r") !== FALSE)
880 {
881 $str = str_replace(array("\r\n", "\r"), "\n", $str);
882 }
883
884 // If the current word is surrounded by {unwrap} tags we'll
885 // strip the entire chunk and replace it with a marker.
886 $unwrap = array();
887 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
888 {
889 for ($i = 0; $i < count($matches['0']); $i++)
890 {
891 $unwrap[] = $matches['1'][$i];
892 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
893 }
894 }
895
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000896 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000897 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500898 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000899 $str = wordwrap($str, $charlim, "\n", FALSE);
900
901 // Split the string into individual lines of text and cycle through them
902 $output = "";
903 foreach (explode("\n", $str) as $line)
904 {
905 // Is the line within the allowed character count?
906 // If so we'll join it to the output and continue
907 if (strlen($line) <= $charlim)
908 {
909 $output .= $line.$this->newline;
910 continue;
911 }
912
913 $temp = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500914 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000915 {
916 // If the over-length word is a URL we won't wrap it
917 if (preg_match("!\[url.+\]|://|wwww.!", $line))
918 {
919 break;
920 }
921
922 // Trim the word down
923 $temp .= substr($line, 0, $charlim-1);
924 $line = substr($line, $charlim-1);
925 }
926
927 // If $temp contains data it means we had to split up an over-length
928 // word into smaller chunks so we'll add it back to our current line
929 if ($temp != '')
930 {
931 $output .= $temp.$this->newline.$line;
932 }
933 else
934 {
935 $output .= $line;
936 }
937
938 $output .= $this->newline;
939 }
940
941 // Put our markers back
942 if (count($unwrap) > 0)
943 {
944 foreach ($unwrap as $key => $val)
945 {
946 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
947 }
948 }
949
950 return $output;
951 }
Barry Mienydd671972010-10-04 16:33:58 +0200952
Derek Allard2067d1a2008-11-13 22:59:24 +0000953 // --------------------------------------------------------------------
954
955 /**
956 * Build final headers
957 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600958 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 * @param string
960 * @return string
961 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600962 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000963 {
964 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
965 $this->_set_header('X-Mailer', $this->useragent);
966 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
967 $this->_set_header('Message-ID', $this->_get_message_id());
968 $this->_set_header('Mime-Version', '1.0');
969 }
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 // --------------------------------------------------------------------
972
973 /**
974 * Write Headers as a string
975 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600976 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000977 * @return void
978 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600979 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000980 {
981 if ($this->protocol == 'mail')
982 {
983 $this->_subject = $this->_headers['Subject'];
984 unset($this->_headers['Subject']);
985 }
986
987 reset($this->_headers);
988 $this->_header_str = "";
989
Pascal Kriete14287f32011-02-14 13:39:34 -0500990 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 {
992 $val = trim($val);
993
994 if ($val != "")
995 {
996 $this->_header_str .= $key.": ".$val.$this->newline;
997 }
998 }
999
1000 if ($this->_get_protocol() == 'mail')
1001 {
Derek Jones1d890882009-02-10 20:32:14 +00001002 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +00001003 }
1004 }
Barry Mienydd671972010-10-04 16:33:58 +02001005
Derek Allard2067d1a2008-11-13 22:59:24 +00001006 // --------------------------------------------------------------------
1007
1008 /**
1009 * Build Final Body and attachments
1010 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001011 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001012 * @return void
1013 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001014 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001015 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001016 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001017 {
1018 $this->_body = $this->word_wrap($this->_body);
1019 }
1020
1021 $this->_set_boundaries();
1022 $this->_write_headers();
1023
1024 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001025 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001026
1027 switch ($this->_get_content_type())
1028 {
1029 case 'plain' :
1030
1031 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1032 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1033
1034 if ($this->_get_protocol() == 'mail')
1035 {
1036 $this->_header_str .= $hdr;
1037 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 }
Brandon Jones485d7412010-11-09 16:38:17 -05001039 else
1040 {
1041 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1042 }
Eric Barnes6113f542010-12-29 13:36:12 -05001043
Derek Allard2067d1a2008-11-13 22:59:24 +00001044 return;
1045
1046 break;
1047 case 'html' :
1048
1049 if ($this->send_multipart === FALSE)
1050 {
1051 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1052 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1053 }
1054 else
1055 {
Derek Jonesa45e7612009-02-10 18:33:01 +00001056 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001057
Brandon Jones485d7412010-11-09 16:38:17 -05001058 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1059 $body .= "--" . $this->_alt_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001060
Brandon Jones485d7412010-11-09 16:38:17 -05001061 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1062 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1063 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1064
1065 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1066 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001067 }
Eric Barnes6113f542010-12-29 13:36:12 -05001068
Brandon Jones485d7412010-11-09 16:38:17 -05001069 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001070
1071
Derek Allard2067d1a2008-11-13 22:59:24 +00001072 if ($this->_get_protocol() == 'mail')
1073 {
1074 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001075 }
1076 else
1077 {
1078 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001079 }
1080
Derek Allard2067d1a2008-11-13 22:59:24 +00001081
1082 if ($this->send_multipart !== FALSE)
1083 {
Brandon Jones485d7412010-11-09 16:38:17 -05001084 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001085 }
1086
Derek Allard2067d1a2008-11-13 22:59:24 +00001087 return;
1088
1089 break;
1090 case 'plain-attach' :
1091
Derek Jonesa45e7612009-02-10 18:33:01 +00001092 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001093
1094 if ($this->_get_protocol() == 'mail')
1095 {
1096 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001097 }
1098
Brandon Jones485d7412010-11-09 16:38:17 -05001099 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1100 $body .= "--" . $this->_atc_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001101
Brandon Jones485d7412010-11-09 16:38:17 -05001102 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1103 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001104
Brandon Jones485d7412010-11-09 16:38:17 -05001105 $body .= $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001106
1107 break;
1108 case 'html-attach' :
1109
Derek Jonesa45e7612009-02-10 18:33:01 +00001110 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001111
Derek Allard2067d1a2008-11-13 22:59:24 +00001112 if ($this->_get_protocol() == 'mail')
1113 {
1114 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 }
1116
Brandon Jones485d7412010-11-09 16:38:17 -05001117 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1118 $body .= "--" . $this->_atc_boundary . $this->newline;
1119
1120 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1121 $body .= "--" . $this->_alt_boundary . $this->newline;
1122
1123 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1124 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1125 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1126
1127 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1128 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
1129
1130 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1131 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001132
1133 break;
1134 }
1135
1136 $attachment = array();
1137
1138 $z = 0;
1139
1140 for ($i=0; $i < count($this->_attach_name); $i++)
1141 {
1142 $filename = $this->_attach_name[$i];
1143 $basename = basename($filename);
1144 $ctype = $this->_attach_type[$i];
1145
1146 if ( ! file_exists($filename))
1147 {
patworkb0707982011-04-08 15:10:05 +02001148 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 return FALSE;
1150 }
1151
Derek Jones37f4b9c2011-07-01 17:56:50 -05001152 $h = "--".$this->_atc_boundary.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001153 $h .= "Content-type: ".$ctype."; ";
1154 $h .= "name=\"".$basename."\"".$this->newline;
1155 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1156 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1157
1158 $attachment[$z++] = $h;
1159 $file = filesize($filename) +1;
1160
1161 if ( ! $fp = fopen($filename, FOPEN_READ))
1162 {
patworkb0707982011-04-08 15:10:05 +02001163 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001164 return FALSE;
1165 }
1166
1167 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1168 fclose($fp);
1169 }
1170
Brandon Jones485d7412010-11-09 16:38:17 -05001171 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Eric Barnes6113f542010-12-29 13:36:12 -05001172
Brandon Jones485d7412010-11-09 16:38:17 -05001173
Derek Allard2067d1a2008-11-13 22:59:24 +00001174 if ($this->_get_protocol() == 'mail')
1175 {
Brandon Jones485d7412010-11-09 16:38:17 -05001176 $this->_finalbody = $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001177 }
Brandon Jones485d7412010-11-09 16:38:17 -05001178 else
1179 {
1180 $this->_finalbody = $hdr . $body;
1181 }
Eric Barnes6113f542010-12-29 13:36:12 -05001182
Derek Allard2067d1a2008-11-13 22:59:24 +00001183 return;
1184 }
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 // --------------------------------------------------------------------
1187
1188 /**
1189 * Prep Quoted Printable
1190 *
1191 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1192 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1193 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001194 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001195 * @param string
1196 * @param integer
1197 * @return string
1198 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001199 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001200 {
1201 // Set the character limit
1202 // Don't allow over 76, as that will make servers and MUAs barf
1203 // all over quoted-printable data
1204 if ($charlim == '' OR $charlim > '76')
1205 {
1206 $charlim = '76';
1207 }
1208
1209 // Reduce multiple spaces
1210 $str = preg_replace("| +|", " ", $str);
1211
1212 // kill nulls
1213 $str = preg_replace('/\x00+/', '', $str);
1214
1215 // Standardize newlines
1216 if (strpos($str, "\r") !== FALSE)
1217 {
1218 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1219 }
1220
1221 // We are intentionally wrapping so mail servers will encode characters
1222 // properly and MUAs will behave, so {unwrap} must go!
1223 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1224
1225 // Break into an array of lines
1226 $lines = explode("\n", $str);
1227
1228 $escape = '=';
1229 $output = '';
1230
1231 foreach ($lines as $line)
1232 {
1233 $length = strlen($line);
1234 $temp = '';
1235
1236 // Loop through each character in the line to add soft-wrap
1237 // characters at the end of a line " =\r\n" and add the newly
1238 // processed line(s) to the output (see comment on $crlf class property)
1239 for ($i = 0; $i < $length; $i++)
1240 {
1241 // Grab the next character
1242 $char = substr($line, $i, 1);
1243 $ascii = ord($char);
1244
1245 // Convert spaces and tabs but only if it's the end of the line
1246 if ($i == ($length - 1))
1247 {
1248 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1249 }
1250
1251 // encode = signs
1252 if ($ascii == '61')
1253 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001254 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001255 }
1256
1257 // If we're at the character limit, add the line to the output,
1258 // reset our temp variable, and keep on chuggin'
1259 if ((strlen($temp) + strlen($char)) >= $charlim)
1260 {
1261 $output .= $temp.$escape.$this->crlf;
1262 $temp = '';
1263 }
1264
1265 // Add the character to our temporary line
1266 $temp .= $char;
1267 }
1268
1269 // Add our completed line to the output
1270 $output .= $temp.$this->crlf;
1271 }
1272
1273 // get rid of extra CRLF tacked onto the end
1274 $output = substr($output, 0, strlen($this->crlf) * -1);
1275
1276 return $output;
1277 }
1278
1279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001280
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 /**
1282 * Prep Q Encoding
1283 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001284 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001285 * but not identical to quoted-printable, so it has its own method
1286 *
1287 * @access public
1288 * @param str
1289 * @param bool // set to TRUE for processing From: headers
1290 * @return str
1291 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001292 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 {
1294 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1295
1296 // Line length must not exceed 76 characters, so we adjust for
1297 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1298 $limit = 75 - 7 - strlen($this->charset);
1299
1300 // these special characters must be converted too
1301 $convert = array('_', '=', '?');
1302
1303 if ($from === TRUE)
1304 {
1305 $convert[] = ',';
1306 $convert[] = ';';
1307 }
1308
1309 $output = '';
1310 $temp = '';
1311
1312 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1313 {
1314 // Grab the next character
1315 $char = substr($str, $i, 1);
1316 $ascii = ord($char);
1317
1318 // convert ALL non-printable ASCII characters and our specials
1319 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1320 {
1321 $char = '='.dechex($ascii);
1322 }
1323
1324 // handle regular spaces a bit more compactly than =20
1325 if ($ascii == 32)
1326 {
1327 $char = '_';
1328 }
1329
1330 // If we're at the character limit, add the line to the output,
1331 // reset our temp variable, and keep on chuggin'
1332 if ((strlen($temp) + strlen($char)) >= $limit)
1333 {
1334 $output .= $temp.$this->crlf;
1335 $temp = '';
1336 }
1337
1338 // Add the character to our temporary line
1339 $temp .= $char;
1340 }
1341
1342 $str = $output.$temp;
1343
1344 // wrap each line with the shebang, charset, and transfer encoding
1345 // the preceding space on successive lines is required for header "folding"
1346 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
1347
1348 return $str;
1349 }
1350
1351 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001352
Derek Allard2067d1a2008-11-13 22:59:24 +00001353 /**
1354 * Send Email
1355 *
1356 * @access public
1357 * @return bool
1358 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001359 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001360 {
1361 if ($this->_replyto_flag == FALSE)
1362 {
1363 $this->reply_to($this->_headers['From']);
1364 }
1365
Derek Jones37f4b9c2011-07-01 17:56:50 -05001366 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1368 ( ! isset($this->_headers['Cc'])))
1369 {
patworkb0707982011-04-08 15:10:05 +02001370 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001371 return FALSE;
1372 }
1373
1374 $this->_build_headers();
1375
Derek Jones37f4b9c2011-07-01 17:56:50 -05001376 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001377 {
1378 if (count($this->_bcc_array) > $this->bcc_batch_size)
1379 return $this->batch_bcc_send();
1380 }
1381
1382 $this->_build_message();
1383
1384 if ( ! $this->_spool_email())
1385 {
1386 return FALSE;
1387 }
1388 else
1389 {
1390 return TRUE;
1391 }
1392 }
Barry Mienydd671972010-10-04 16:33:58 +02001393
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 // --------------------------------------------------------------------
1395
1396 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001397 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001398 *
1399 * @access public
1400 * @return bool
1401 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001402 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001403 {
1404 $float = $this->bcc_batch_size -1;
1405
1406 $set = "";
1407
1408 $chunk = array();
1409
1410 for ($i = 0; $i < count($this->_bcc_array); $i++)
1411 {
1412 if (isset($this->_bcc_array[$i]))
1413 {
1414 $set .= ", ".$this->_bcc_array[$i];
1415 }
1416
1417 if ($i == $float)
1418 {
1419 $chunk[] = substr($set, 1);
1420 $float = $float + $this->bcc_batch_size;
1421 $set = "";
1422 }
1423
1424 if ($i == count($this->_bcc_array)-1)
1425 {
1426 $chunk[] = substr($set, 1);
1427 }
1428 }
1429
1430 for ($i = 0; $i < count($chunk); $i++)
1431 {
1432 unset($this->_headers['Bcc']);
1433 unset($bcc);
1434
1435 $bcc = $this->_str_to_array($chunk[$i]);
1436 $bcc = $this->clean_email($bcc);
1437
1438 if ($this->protocol != 'smtp')
1439 {
1440 $this->_set_header('Bcc', implode(", ", $bcc));
1441 }
1442 else
1443 {
1444 $this->_bcc_array = $bcc;
1445 }
1446
1447 $this->_build_message();
1448 $this->_spool_email();
1449 }
1450 }
Barry Mienydd671972010-10-04 16:33:58 +02001451
Derek Allard2067d1a2008-11-13 22:59:24 +00001452 // --------------------------------------------------------------------
1453
1454 /**
1455 * Unwrap special elements
1456 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001457 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001458 * @return void
1459 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001460 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 {
1462 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1463 }
Barry Mienydd671972010-10-04 16:33:58 +02001464
Derek Allard2067d1a2008-11-13 22:59:24 +00001465 // --------------------------------------------------------------------
1466
1467 /**
1468 * Strip line-breaks via callback
1469 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001470 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001471 * @return string
1472 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001473 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001474 {
1475 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1476 {
1477 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1478 }
1479
1480 return $matches[1];
1481 }
Barry Mienydd671972010-10-04 16:33:58 +02001482
Derek Allard2067d1a2008-11-13 22:59:24 +00001483 // --------------------------------------------------------------------
1484
1485 /**
1486 * Spool mail to the mail server
1487 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001488 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001489 * @return bool
1490 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001491 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001492 {
1493 $this->_unwrap_specials();
1494
1495 switch ($this->_get_protocol())
1496 {
1497 case 'mail' :
1498
1499 if ( ! $this->_send_with_mail())
1500 {
patworkb0707982011-04-08 15:10:05 +02001501 $this->_set_error_message('lang:email_send_failure_phpmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001502 return FALSE;
1503 }
1504 break;
1505 case 'sendmail' :
1506
1507 if ( ! $this->_send_with_sendmail())
1508 {
patworkb0707982011-04-08 15:10:05 +02001509 $this->_set_error_message('lang:email_send_failure_sendmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001510 return FALSE;
1511 }
1512 break;
1513 case 'smtp' :
1514
1515 if ( ! $this->_send_with_smtp())
1516 {
patworkb0707982011-04-08 15:10:05 +02001517 $this->_set_error_message('lang:email_send_failure_smtp');
Derek Allard2067d1a2008-11-13 22:59:24 +00001518 return FALSE;
1519 }
1520 break;
1521
1522 }
1523
patworkb0707982011-04-08 15:10:05 +02001524 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001525 return TRUE;
1526 }
Barry Mienydd671972010-10-04 16:33:58 +02001527
Derek Allard2067d1a2008-11-13 22:59:24 +00001528 // --------------------------------------------------------------------
1529
1530 /**
1531 * Send using mail()
1532 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001533 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001534 * @return bool
1535 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001536 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001537 {
1538 if ($this->_safe_mode == TRUE)
1539 {
1540 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1541 {
1542 return FALSE;
1543 }
1544 else
1545 {
1546 return TRUE;
1547 }
1548 }
1549 else
1550 {
1551 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1552 // we've encountered servers that seem to require it to be in place.
Eric Barnes6113f542010-12-29 13:36:12 -05001553
Derek Allard2067d1a2008-11-13 22:59:24 +00001554 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1555 {
1556 return FALSE;
1557 }
1558 else
1559 {
1560 return TRUE;
1561 }
1562 }
1563 }
Barry Mienydd671972010-10-04 16:33:58 +02001564
Derek Allard2067d1a2008-11-13 22:59:24 +00001565 // --------------------------------------------------------------------
1566
1567 /**
1568 * Send using Sendmail
1569 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001570 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001571 * @return bool
1572 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001573 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001574 {
1575 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1576
Derek Jones4cefaa42009-04-29 19:13:56 +00001577 if ($fp === FALSE OR $fp === NULL)
1578 {
1579 // server probably has popen disabled, so nothing we can do to get a verbose error.
1580 return FALSE;
1581 }
Derek Jones71141ce2010-03-02 16:41:20 -06001582
Derek Jonesc630bcf2008-11-17 21:09:45 +00001583 fputs($fp, $this->_header_str);
1584 fputs($fp, $this->_finalbody);
1585
Barry Mienydd671972010-10-04 16:33:58 +02001586 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001587
Derek Jonesc630bcf2008-11-17 21:09:45 +00001588 if (version_compare(PHP_VERSION, '4.2.3') == -1)
1589 {
1590 $status = $status >> 8 & 0xFF;
Barry Mienydd671972010-10-04 16:33:58 +02001591 }
Derek Jones71141ce2010-03-02 16:41:20 -06001592
Derek Jones604873f2008-11-18 15:57:24 +00001593 if ($status != 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001594 {
patworkb0707982011-04-08 15:10:05 +02001595 $this->_set_error_message('lang:email_exit_status', $status);
1596 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001597 return FALSE;
1598 }
1599
Derek Allard2067d1a2008-11-13 22:59:24 +00001600 return TRUE;
1601 }
Barry Mienydd671972010-10-04 16:33:58 +02001602
Derek Allard2067d1a2008-11-13 22:59:24 +00001603 // --------------------------------------------------------------------
1604
1605 /**
1606 * Send using SMTP
1607 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001608 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001609 * @return bool
1610 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001611 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 {
1613 if ($this->smtp_host == '')
1614 {
patworkb0707982011-04-08 15:10:05 +02001615 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001616 return FALSE;
1617 }
1618
1619 $this->_smtp_connect();
1620 $this->_smtp_authenticate();
1621
1622 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1623
Pascal Kriete14287f32011-02-14 13:39:34 -05001624 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001625 {
1626 $this->_send_command('to', $val);
1627 }
1628
1629 if (count($this->_cc_array) > 0)
1630 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001631 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001632 {
1633 if ($val != "")
1634 {
1635 $this->_send_command('to', $val);
1636 }
1637 }
1638 }
1639
1640 if (count($this->_bcc_array) > 0)
1641 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001642 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001643 {
1644 if ($val != "")
1645 {
1646 $this->_send_command('to', $val);
1647 }
1648 }
1649 }
1650
1651 $this->_send_command('data');
1652
1653 // perform dot transformation on any lines that begin with a dot
1654 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1655
1656 $this->_send_data('.');
1657
1658 $reply = $this->_get_smtp_data();
1659
1660 $this->_set_error_message($reply);
1661
1662 if (strncmp($reply, '250', 3) != 0)
1663 {
patworkb0707982011-04-08 15:10:05 +02001664 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001665 return FALSE;
1666 }
1667
1668 $this->_send_command('quit');
1669 return TRUE;
1670 }
Barry Mienydd671972010-10-04 16:33:58 +02001671
Derek Allard2067d1a2008-11-13 22:59:24 +00001672 // --------------------------------------------------------------------
1673
1674 /**
1675 * SMTP Connect
1676 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001677 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 * @param string
1679 * @return string
1680 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001681 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001682 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001683 $ssl = NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001684
Radu Potopbbf04b02011-09-28 13:57:51 +03001685 if ($this->smtp_crypto == 'ssl')
Radu Potop4c589ae2011-09-29 10:19:55 +03001686 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001687 $ssl = 'ssl://';
Radu Potop4c589ae2011-09-29 10:19:55 +03001688 }
1689
Radu Potopbbf04b02011-09-28 13:57:51 +03001690 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001691 $this->smtp_port,
1692 $errno,
1693 $errstr,
1694 $this->smtp_timeout);
1695
Pascal Kriete14287f32011-02-14 13:39:34 -05001696 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001697 {
patworkb0707982011-04-08 15:10:05 +02001698 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001699 return FALSE;
1700 }
1701
1702 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001703
1704 if ($this->smtp_crypto == 'tls')
1705 {
1706 $this->_send_command('hello');
1707 $this->_send_command('starttls');
Radu Potop4c589ae2011-09-29 10:19:55 +03001708 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1709 }
1710
1711 if ($crypto !== TRUE)
1712 {
1713 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1714 return FALSE;
Radu Potopbbf04b02011-09-28 13:57:51 +03001715 }
1716
Derek Allard2067d1a2008-11-13 22:59:24 +00001717 return $this->_send_command('hello');
1718 }
Barry Mienydd671972010-10-04 16:33:58 +02001719
Derek Allard2067d1a2008-11-13 22:59:24 +00001720 // --------------------------------------------------------------------
1721
1722 /**
1723 * Send SMTP command
1724 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001725 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001726 * @param string
1727 * @param string
1728 * @return string
1729 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001730 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001731 {
1732 switch ($cmd)
1733 {
1734 case 'hello' :
1735
1736 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1737 $this->_send_data('EHLO '.$this->_get_hostname());
1738 else
1739 $this->_send_data('HELO '.$this->_get_hostname());
1740
1741 $resp = 250;
1742 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001743 case 'starttls' :
1744
1745 $this->_send_data('STARTTLS');
1746
1747 $resp = 220;
1748 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001749 case 'from' :
1750
1751 $this->_send_data('MAIL FROM:<'.$data.'>');
1752
1753 $resp = 250;
1754 break;
1755 case 'to' :
1756
1757 $this->_send_data('RCPT TO:<'.$data.'>');
1758
1759 $resp = 250;
1760 break;
1761 case 'data' :
1762
1763 $this->_send_data('DATA');
1764
1765 $resp = 354;
1766 break;
1767 case 'quit' :
1768
1769 $this->_send_data('QUIT');
1770
1771 $resp = 221;
1772 break;
1773 }
1774
1775 $reply = $this->_get_smtp_data();
1776
1777 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1778
1779 if (substr($reply, 0, 3) != $resp)
1780 {
patworkb0707982011-04-08 15:10:05 +02001781 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001782 return FALSE;
1783 }
1784
1785 if ($cmd == 'quit')
1786 {
1787 fclose($this->_smtp_connect);
1788 }
1789
1790 return TRUE;
1791 }
Barry Mienydd671972010-10-04 16:33:58 +02001792
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 // --------------------------------------------------------------------
1794
1795 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001796 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001797 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001798 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001799 * @return bool
1800 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001801 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001802 {
1803 if ( ! $this->_smtp_auth)
1804 {
1805 return TRUE;
1806 }
1807
Derek Jones37f4b9c2011-07-01 17:56:50 -05001808 if ($this->smtp_user == "" AND $this->smtp_pass == "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001809 {
patworkb0707982011-04-08 15:10:05 +02001810 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001811 return FALSE;
1812 }
1813
1814 $this->_send_data('AUTH LOGIN');
1815
1816 $reply = $this->_get_smtp_data();
1817
1818 if (strncmp($reply, '334', 3) != 0)
1819 {
patworkb0707982011-04-08 15:10:05 +02001820 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001821 return FALSE;
1822 }
1823
1824 $this->_send_data(base64_encode($this->smtp_user));
1825
1826 $reply = $this->_get_smtp_data();
1827
1828 if (strncmp($reply, '334', 3) != 0)
1829 {
patworkb0707982011-04-08 15:10:05 +02001830 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001831 return FALSE;
1832 }
1833
1834 $this->_send_data(base64_encode($this->smtp_pass));
1835
1836 $reply = $this->_get_smtp_data();
1837
1838 if (strncmp($reply, '235', 3) != 0)
1839 {
patworkb0707982011-04-08 15:10:05 +02001840 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001841 return FALSE;
1842 }
1843
1844 return TRUE;
1845 }
Barry Mienydd671972010-10-04 16:33:58 +02001846
Derek Allard2067d1a2008-11-13 22:59:24 +00001847 // --------------------------------------------------------------------
1848
1849 /**
1850 * Send SMTP data
1851 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001852 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001853 * @return bool
1854 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001855 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 {
1857 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1858 {
patworkb0707982011-04-08 15:10:05 +02001859 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001860 return FALSE;
1861 }
1862 else
1863 {
1864 return TRUE;
1865 }
1866 }
Barry Mienydd671972010-10-04 16:33:58 +02001867
Derek Allard2067d1a2008-11-13 22:59:24 +00001868 // --------------------------------------------------------------------
1869
1870 /**
1871 * Get SMTP data
1872 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001873 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001874 * @return string
1875 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001876 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001877 {
1878 $data = "";
1879
1880 while ($str = fgets($this->_smtp_connect, 512))
1881 {
1882 $data .= $str;
1883
1884 if (substr($str, 3, 1) == " ")
1885 {
1886 break;
1887 }
1888 }
1889
1890 return $data;
1891 }
Barry Mienydd671972010-10-04 16:33:58 +02001892
Derek Allard2067d1a2008-11-13 22:59:24 +00001893 // --------------------------------------------------------------------
1894
1895 /**
1896 * Get Hostname
1897 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001898 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001899 * @return string
1900 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001901 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 {
1903 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1904 }
Barry Mienydd671972010-10-04 16:33:58 +02001905
Derek Allard2067d1a2008-11-13 22:59:24 +00001906 // --------------------------------------------------------------------
1907
1908 /**
1909 * Get IP
1910 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001911 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001912 * @return string
1913 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001914 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001915 {
1916 if ($this->_IP !== FALSE)
1917 {
1918 return $this->_IP;
1919 }
1920
1921 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1922 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1923 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1924
Barry Mienydd671972010-10-04 16:33:58 +02001925 if ($cip && $rip) $this->_IP = $cip;
Derek Allard2067d1a2008-11-13 22:59:24 +00001926 elseif ($rip) $this->_IP = $rip;
1927 elseif ($cip) $this->_IP = $cip;
1928 elseif ($fip) $this->_IP = $fip;
1929
Robin Sowell76b369e2010-03-19 11:15:28 -04001930 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001931 {
1932 $x = explode(',', $this->_IP);
1933 $this->_IP = end($x);
1934 }
1935
1936 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1937 {
1938 $this->_IP = '0.0.0.0';
1939 }
1940
1941 unset($cip);
1942 unset($rip);
1943 unset($fip);
1944
1945 return $this->_IP;
1946 }
Barry Mienydd671972010-10-04 16:33:58 +02001947
Derek Allard2067d1a2008-11-13 22:59:24 +00001948 // --------------------------------------------------------------------
1949
1950 /**
1951 * Get Debug Message
1952 *
1953 * @access public
1954 * @return string
1955 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001956 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001957 {
1958 $msg = '';
1959
1960 if (count($this->_debug_msg) > 0)
1961 {
1962 foreach ($this->_debug_msg as $val)
1963 {
1964 $msg .= $val;
1965 }
1966 }
1967
1968 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1969 return $msg;
1970 }
Barry Mienydd671972010-10-04 16:33:58 +02001971
Derek Allard2067d1a2008-11-13 22:59:24 +00001972 // --------------------------------------------------------------------
1973
1974 /**
1975 * Set Message
1976 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001977 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 * @param string
1979 * @return string
1980 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001981 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001982 {
1983 $CI =& get_instance();
1984 $CI->lang->load('email');
1985
patworkb0707982011-04-08 15:10:05 +02001986 if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001987 {
1988 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1989 }
1990 else
1991 {
1992 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1993 }
1994 }
Barry Mienydd671972010-10-04 16:33:58 +02001995
Derek Allard2067d1a2008-11-13 22:59:24 +00001996 // --------------------------------------------------------------------
1997
1998 /**
1999 * Mime Types
2000 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002001 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 * @param string
2003 * @return string
2004 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002005 protected function _mime_types($ext = "")
Derek Allard2067d1a2008-11-13 22:59:24 +00002006 {
2007 $mimes = array( 'hqx' => 'application/mac-binhex40',
2008 'cpt' => 'application/mac-compactpro',
2009 'doc' => 'application/msword',
2010 'bin' => 'application/macbinary',
2011 'dms' => 'application/octet-stream',
2012 'lha' => 'application/octet-stream',
2013 'lzh' => 'application/octet-stream',
2014 'exe' => 'application/octet-stream',
2015 'class' => 'application/octet-stream',
2016 'psd' => 'application/octet-stream',
2017 'so' => 'application/octet-stream',
2018 'sea' => 'application/octet-stream',
2019 'dll' => 'application/octet-stream',
2020 'oda' => 'application/oda',
2021 'pdf' => 'application/pdf',
2022 'ai' => 'application/postscript',
2023 'eps' => 'application/postscript',
2024 'ps' => 'application/postscript',
2025 'smi' => 'application/smil',
2026 'smil' => 'application/smil',
2027 'mif' => 'application/vnd.mif',
2028 'xls' => 'application/vnd.ms-excel',
2029 'ppt' => 'application/vnd.ms-powerpoint',
2030 'wbxml' => 'application/vnd.wap.wbxml',
2031 'wmlc' => 'application/vnd.wap.wmlc',
2032 'dcr' => 'application/x-director',
2033 'dir' => 'application/x-director',
2034 'dxr' => 'application/x-director',
2035 'dvi' => 'application/x-dvi',
2036 'gtar' => 'application/x-gtar',
2037 'php' => 'application/x-httpd-php',
2038 'php4' => 'application/x-httpd-php',
2039 'php3' => 'application/x-httpd-php',
2040 'phtml' => 'application/x-httpd-php',
2041 'phps' => 'application/x-httpd-php-source',
2042 'js' => 'application/x-javascript',
2043 'swf' => 'application/x-shockwave-flash',
2044 'sit' => 'application/x-stuffit',
2045 'tar' => 'application/x-tar',
2046 'tgz' => 'application/x-tar',
2047 'xhtml' => 'application/xhtml+xml',
2048 'xht' => 'application/xhtml+xml',
2049 'zip' => 'application/zip',
2050 'mid' => 'audio/midi',
2051 'midi' => 'audio/midi',
2052 'mpga' => 'audio/mpeg',
2053 'mp2' => 'audio/mpeg',
2054 'mp3' => 'audio/mpeg',
2055 'aif' => 'audio/x-aiff',
2056 'aiff' => 'audio/x-aiff',
2057 'aifc' => 'audio/x-aiff',
2058 'ram' => 'audio/x-pn-realaudio',
2059 'rm' => 'audio/x-pn-realaudio',
2060 'rpm' => 'audio/x-pn-realaudio-plugin',
2061 'ra' => 'audio/x-realaudio',
2062 'rv' => 'video/vnd.rn-realvideo',
2063 'wav' => 'audio/x-wav',
2064 'bmp' => 'image/bmp',
2065 'gif' => 'image/gif',
2066 'jpeg' => 'image/jpeg',
2067 'jpg' => 'image/jpeg',
2068 'jpe' => 'image/jpeg',
2069 'png' => 'image/png',
2070 'tiff' => 'image/tiff',
2071 'tif' => 'image/tiff',
2072 'css' => 'text/css',
2073 'html' => 'text/html',
2074 'htm' => 'text/html',
2075 'shtml' => 'text/html',
2076 'txt' => 'text/plain',
2077 'text' => 'text/plain',
2078 'log' => 'text/plain',
2079 'rtx' => 'text/richtext',
2080 'rtf' => 'text/rtf',
2081 'xml' => 'text/xml',
2082 'xsl' => 'text/xml',
2083 'mpeg' => 'video/mpeg',
2084 'mpg' => 'video/mpeg',
2085 'mpe' => 'video/mpeg',
2086 'qt' => 'video/quicktime',
2087 'mov' => 'video/quicktime',
2088 'avi' => 'video/x-msvideo',
2089 'movie' => 'video/x-sgi-movie',
2090 'doc' => 'application/msword',
2091 'word' => 'application/msword',
2092 'xl' => 'application/excel',
2093 'eml' => 'message/rfc822'
2094 );
2095
2096 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2097 }
2098
2099}
2100// END CI_Email class
2101
2102/* End of file Email.php */
patworkb0707982011-04-08 15:10:05 +02002103/* Location: ./system/libraries/Email.php */