blob: c7d0bc52bf88df3f822591e51a22a1c1508d4cee [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
386 //strip slashes only if magic quotes is ON
387 //if we do it with magic quotes OFF, it strips real, user-inputted chars.
diegorivera9fca6152011-10-19 11:18:45 -0200388 if (get_magic_quotes_gpc())
389 {
diegorivera33c32802011-10-19 02:56:15 -0200390 $this->_body = stripslashes($this->_body);
diegorivera9fca6152011-10-19 11:18:45 -0200391 }
diegorivera33c32802011-10-19 02:56:15 -0200392
Greg Akera769deb2010-11-10 15:47:29 -0600393 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 // --------------------------------------------------------------------
397
398 /**
399 * Assign file attachments
400 *
401 * @access public
402 * @param string
403 * @return void
404 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000405 public function attach($filename, $disposition = 'attachment')
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 $this->_attach_name[] = $filename;
Phil Sturgeon0aaf42b2011-08-10 08:06:37 -0600408 $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
Derek Jones37f4b9c2011-07-01 17:56:50 -0500409 $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
Greg Akera769deb2010-11-10 15:47:29 -0600410 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Add a Header Item
417 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600418 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @param string
420 * @param string
421 * @return void
422 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600423 protected function _set_header($header, $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $this->_headers[$header] = $value;
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 // --------------------------------------------------------------------
429
430 /**
431 * Convert a String to an Array
432 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600433 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param string
435 * @return array
436 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600437 protected function _str_to_array($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 if ( ! is_array($email))
440 {
441 if (strpos($email, ',') !== FALSE)
442 {
443 $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
444 }
445 else
446 {
447 $email = trim($email);
448 settype($email, "array");
449 }
450 }
451 return $email;
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
455
456 /**
457 * Set Multipart Value
458 *
459 * @access public
460 * @param string
461 * @return void
462 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000463 public function set_alt_message($str = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Dan Horrigand0ddeaf2011-08-21 09:07:27 -0400465 $this->alt_message = (string) $str;
Greg Akera769deb2010-11-10 15:47:29 -0600466 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
470
471 /**
472 * Set Mailtype
473 *
474 * @access public
475 * @param string
476 * @return void
477 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000478 public function set_mailtype($type = 'text')
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 $this->mailtype = ($type == 'html') ? 'html' : 'text';
Greg Akera769deb2010-11-10 15:47:29 -0600481 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
487 * Set Wordwrap
488 *
489 * @access public
Dan Horrigan628e6602011-08-21 09:08:31 -0400490 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * @return void
492 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000493 public function set_wordwrap($wordwrap = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
Dan Horrigan628e6602011-08-21 09:08:31 -0400495 $this->wordwrap = (bool) $wordwrap;
Greg Akera769deb2010-11-10 15:47:29 -0600496 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // --------------------------------------------------------------------
500
501 /**
502 * Set Protocol
503 *
504 * @access public
505 * @param string
506 * @return void
507 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000508 public function set_protocol($protocol = 'mail')
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
Greg Akera769deb2010-11-10 15:47:29 -0600511 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
517 * Set Priority
518 *
519 * @access public
520 * @param integer
521 * @return void
522 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000523 public function set_priority($n = 3)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 if ( ! is_numeric($n))
526 {
527 $this->priority = 3;
528 return;
529 }
530
531 if ($n < 1 OR $n > 5)
532 {
533 $this->priority = 3;
534 return;
535 }
536
537 $this->priority = $n;
Greg Akera769deb2010-11-10 15:47:29 -0600538 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 // --------------------------------------------------------------------
542
543 /**
544 * Set Newline Character
545 *
546 * @access public
547 * @param string
548 * @return void
549 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000550 public function set_newline($newline = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
552 if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
553 {
554 $this->newline = "\n";
555 return;
556 }
557
558 $this->newline = $newline;
Eric Barnes6113f542010-12-29 13:36:12 -0500559
Greg Akera769deb2010-11-10 15:47:29 -0600560 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 // --------------------------------------------------------------------
564
565 /**
566 * Set CRLF
567 *
568 * @access public
569 * @param string
570 * @return void
571 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000572 public function set_crlf($crlf = "\n")
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 {
574 if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
575 {
576 $this->crlf = "\n";
577 return;
578 }
579
580 $this->crlf = $crlf;
Eric Barnes6113f542010-12-29 13:36:12 -0500581
Greg Akera769deb2010-11-10 15:47:29 -0600582 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 // --------------------------------------------------------------------
586
587 /**
588 * Set Message Boundary
589 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600590 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * @return void
592 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600593 protected function _set_boundaries()
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
596 $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 // --------------------------------------------------------------------
600
601 /**
602 * Get the Message ID
603 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600604 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @return string
606 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600607 protected function _get_message_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 $from = $this->_headers['Return-Path'];
610 $from = str_replace(">", "", $from);
611 $from = str_replace("<", "", $from);
612
Derek Jones37f4b9c2011-07-01 17:56:50 -0500613 return "<".uniqid('').strstr($from, '@').">";
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 // --------------------------------------------------------------------
617
618 /**
619 * Get Mail Protocol
620 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600621 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 * @param bool
623 * @return string
624 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600625 protected function _get_protocol($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 {
627 $this->protocol = strtolower($this->protocol);
628 $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
629
630 if ($return == TRUE)
631 {
632 return $this->protocol;
633 }
634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // --------------------------------------------------------------------
637
638 /**
639 * Get Mail Encoding
640 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600641 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @param bool
643 * @return string
644 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600645 protected function _get_encoding($return = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 {
647 $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
648
649 foreach ($this->_base_charsets as $charset)
650 {
651 if (strncmp($charset, $this->charset, strlen($charset)) == 0)
652 {
653 $this->_encoding = '7bit';
654 }
655 }
656
657 if ($return == TRUE)
658 {
659 return $this->_encoding;
660 }
661 }
662
663 // --------------------------------------------------------------------
664
665 /**
666 * Get content type (text/html/attachment)
667 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600668 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @return string
670 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600671 protected function _get_content_type()
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500673 if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 {
675 return 'html';
676 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500677 elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 {
679 return 'html-attach';
680 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500681 elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 return 'plain-attach';
684 }
685 else
686 {
687 return 'plain';
688 }
689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 // --------------------------------------------------------------------
692
693 /**
694 * Set RFC 822 Date
695 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600696 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * @return string
698 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600699 protected function _set_date()
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
701 $timezone = date("Z");
702 $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
703 $timezone = abs($timezone);
704 $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
705
706 return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
707 }
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 // --------------------------------------------------------------------
710
711 /**
712 * Mime message
713 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600714 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 * @return string
716 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600717 protected function _get_mime_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 {
719 return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
720 }
Barry Mienydd671972010-10-04 16:33:58 +0200721
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 // --------------------------------------------------------------------
723
724 /**
725 * Validate Email Address
726 *
727 * @access public
728 * @param string
729 * @return bool
730 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000731 public function validate_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 {
733 if ( ! is_array($email))
734 {
patworkb0707982011-04-08 15:10:05 +0200735 $this->_set_error_message('lang:email_must_be_array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 return FALSE;
737 }
738
739 foreach ($email as $val)
740 {
741 if ( ! $this->valid_email($val))
742 {
patworkb0707982011-04-08 15:10:05 +0200743 $this->_set_error_message('lang:email_invalid_address', $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000744 return FALSE;
745 }
746 }
747
748 return TRUE;
749 }
Barry Mienydd671972010-10-04 16:33:58 +0200750
Derek Allard2067d1a2008-11-13 22:59:24 +0000751 // --------------------------------------------------------------------
752
753 /**
754 * Email Validation
755 *
756 * @access public
757 * @param string
758 * @return bool
759 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000760 public function valid_email($address)
Derek Allard2067d1a2008-11-13 22:59:24 +0000761 {
762 return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
763 }
Barry Mienydd671972010-10-04 16:33:58 +0200764
Derek Allard2067d1a2008-11-13 22:59:24 +0000765 // --------------------------------------------------------------------
766
767 /**
768 * Clean Extended Email Address: Joe Smith <joe@smith.com>
769 *
770 * @access public
771 * @param string
772 * @return string
773 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000774 public function clean_email($email)
Derek Allard2067d1a2008-11-13 22:59:24 +0000775 {
776 if ( ! is_array($email))
777 {
778 if (preg_match('/\<(.*)\>/', $email, $match))
779 {
Barry Mienydd671972010-10-04 16:33:58 +0200780 return $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 }
Barry Mienydd671972010-10-04 16:33:58 +0200782 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000783 {
Barry Mienydd671972010-10-04 16:33:58 +0200784 return $email;
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 }
786 }
787
788 $clean_email = array();
789
790 foreach ($email as $addy)
791 {
792 if (preg_match( '/\<(.*)\>/', $addy, $match))
793 {
Barry Mienydd671972010-10-04 16:33:58 +0200794 $clean_email[] = $match['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000795 }
Barry Mienydd671972010-10-04 16:33:58 +0200796 else
Derek Allard2067d1a2008-11-13 22:59:24 +0000797 {
Barry Mienydd671972010-10-04 16:33:58 +0200798 $clean_email[] = $addy;
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 }
800 }
801
802 return $clean_email;
803 }
Barry Mienydd671972010-10-04 16:33:58 +0200804
Derek Allard2067d1a2008-11-13 22:59:24 +0000805 // --------------------------------------------------------------------
806
807 /**
808 * Build alternative plain text message
809 *
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000810 * This public function provides the raw message for use
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 * in plain-text headers of HTML-formatted emails.
812 * If the user hasn't specified his own alternative message
813 * it creates one by stripping the HTML
814 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600815 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 * @return string
817 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600818 protected function _get_alt_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000819 {
820 if ($this->alt_message != "")
821 {
822 return $this->word_wrap($this->alt_message, '76');
823 }
824
825 if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
826 {
827 $body = $match['1'];
828 }
829 else
830 {
831 $body = $this->_body;
832 }
833
834 $body = trim(strip_tags($body));
835 $body = preg_replace( '#<!--(.*)--\>#', "", $body);
836 $body = str_replace("\t", "", $body);
837
838 for ($i = 20; $i >= 3; $i--)
839 {
840 $n = "";
841
842 for ($x = 1; $x <= $i; $x ++)
843 {
Barry Mienydd671972010-10-04 16:33:58 +0200844 $n .= "\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000845 }
846
847 $body = str_replace($n, "\n\n", $body);
848 }
849
850 return $this->word_wrap($body, '76');
851 }
Barry Mienydd671972010-10-04 16:33:58 +0200852
Derek Allard2067d1a2008-11-13 22:59:24 +0000853 // --------------------------------------------------------------------
854
855 /**
856 * Word Wrap
857 *
858 * @access public
859 * @param string
860 * @param integer
861 * @return string
862 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000863 public function word_wrap($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 {
865 // Se the character limit
866 if ($charlim == '')
867 {
868 $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
869 }
870
871 // Reduce multiple spaces
872 $str = preg_replace("| +|", " ", $str);
873
874 // Standardize newlines
875 if (strpos($str, "\r") !== FALSE)
876 {
877 $str = str_replace(array("\r\n", "\r"), "\n", $str);
878 }
879
880 // If the current word is surrounded by {unwrap} tags we'll
881 // strip the entire chunk and replace it with a marker.
882 $unwrap = array();
883 if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
884 {
885 for ($i = 0; $i < count($matches['0']); $i++)
886 {
887 $unwrap[] = $matches['1'][$i];
888 $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
889 }
890 }
891
Phil Sturgeona0f980e2011-01-13 10:59:12 +0000892 // Use PHP's native public function to do the initial wordwrap.
Derek Allard2067d1a2008-11-13 22:59:24 +0000893 // We set the cut flag to FALSE so that any individual words that are
Derek Jones37f4b9c2011-07-01 17:56:50 -0500894 // too long get left alone. In the next step we'll deal with them.
Derek Allard2067d1a2008-11-13 22:59:24 +0000895 $str = wordwrap($str, $charlim, "\n", FALSE);
896
897 // Split the string into individual lines of text and cycle through them
898 $output = "";
899 foreach (explode("\n", $str) as $line)
900 {
901 // Is the line within the allowed character count?
902 // If so we'll join it to the output and continue
903 if (strlen($line) <= $charlim)
904 {
905 $output .= $line.$this->newline;
906 continue;
907 }
908
909 $temp = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500910 while ((strlen($line)) > $charlim)
Derek Allard2067d1a2008-11-13 22:59:24 +0000911 {
912 // If the over-length word is a URL we won't wrap it
913 if (preg_match("!\[url.+\]|://|wwww.!", $line))
914 {
915 break;
916 }
917
918 // Trim the word down
919 $temp .= substr($line, 0, $charlim-1);
920 $line = substr($line, $charlim-1);
921 }
922
923 // If $temp contains data it means we had to split up an over-length
924 // word into smaller chunks so we'll add it back to our current line
925 if ($temp != '')
926 {
927 $output .= $temp.$this->newline.$line;
928 }
929 else
930 {
931 $output .= $line;
932 }
933
934 $output .= $this->newline;
935 }
936
937 // Put our markers back
938 if (count($unwrap) > 0)
939 {
940 foreach ($unwrap as $key => $val)
941 {
942 $output = str_replace("{{unwrapped".$key."}}", $val, $output);
943 }
944 }
945
946 return $output;
947 }
Barry Mienydd671972010-10-04 16:33:58 +0200948
Derek Allard2067d1a2008-11-13 22:59:24 +0000949 // --------------------------------------------------------------------
950
951 /**
952 * Build final headers
953 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600954 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000955 * @param string
956 * @return string
957 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600958 protected function _build_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 {
960 $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
961 $this->_set_header('X-Mailer', $this->useragent);
962 $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
963 $this->_set_header('Message-ID', $this->_get_message_id());
964 $this->_set_header('Mime-Version', '1.0');
965 }
Barry Mienydd671972010-10-04 16:33:58 +0200966
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // --------------------------------------------------------------------
968
969 /**
970 * Write Headers as a string
971 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600972 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +0000973 * @return void
974 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -0600975 protected function _write_headers()
Derek Allard2067d1a2008-11-13 22:59:24 +0000976 {
977 if ($this->protocol == 'mail')
978 {
979 $this->_subject = $this->_headers['Subject'];
980 unset($this->_headers['Subject']);
981 }
982
983 reset($this->_headers);
984 $this->_header_str = "";
985
Pascal Kriete14287f32011-02-14 13:39:34 -0500986 foreach ($this->_headers as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000987 {
988 $val = trim($val);
989
990 if ($val != "")
991 {
992 $this->_header_str .= $key.": ".$val.$this->newline;
993 }
994 }
995
996 if ($this->_get_protocol() == 'mail')
997 {
Derek Jones1d890882009-02-10 20:32:14 +0000998 $this->_header_str = rtrim($this->_header_str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000999 }
1000 }
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 // --------------------------------------------------------------------
1003
1004 /**
1005 * Build Final Body and attachments
1006 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001007 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001008 * @return void
1009 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001010 protected function _build_message()
Derek Allard2067d1a2008-11-13 22:59:24 +00001011 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001012 if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
Derek Allard2067d1a2008-11-13 22:59:24 +00001013 {
1014 $this->_body = $this->word_wrap($this->_body);
1015 }
1016
1017 $this->_set_boundaries();
1018 $this->_write_headers();
1019
1020 $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
Brandon Jones485d7412010-11-09 16:38:17 -05001021 $body = '';
Derek Allard2067d1a2008-11-13 22:59:24 +00001022
1023 switch ($this->_get_content_type())
1024 {
1025 case 'plain' :
1026
1027 $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1028 $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
1029
1030 if ($this->_get_protocol() == 'mail')
1031 {
1032 $this->_header_str .= $hdr;
1033 $this->_finalbody = $this->_body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001034 }
Brandon Jones485d7412010-11-09 16:38:17 -05001035 else
1036 {
1037 $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
1038 }
Eric Barnes6113f542010-12-29 13:36:12 -05001039
Derek Allard2067d1a2008-11-13 22:59:24 +00001040 return;
1041
1042 break;
1043 case 'html' :
1044
1045 if ($this->send_multipart === FALSE)
1046 {
1047 $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1048 $hdr .= "Content-Transfer-Encoding: quoted-printable";
1049 }
1050 else
1051 {
Derek Jonesa45e7612009-02-10 18:33:01 +00001052 $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001053
Brandon Jones485d7412010-11-09 16:38:17 -05001054 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1055 $body .= "--" . $this->_alt_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001056
Brandon Jones485d7412010-11-09 16:38:17 -05001057 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1058 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1059 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1060
1061 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1062 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001063 }
Eric Barnes6113f542010-12-29 13:36:12 -05001064
Brandon Jones485d7412010-11-09 16:38:17 -05001065 $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001066
1067
Derek Allard2067d1a2008-11-13 22:59:24 +00001068 if ($this->_get_protocol() == 'mail')
1069 {
1070 $this->_header_str .= $hdr;
Brandon Jones485d7412010-11-09 16:38:17 -05001071 }
1072 else
1073 {
1074 $this->_finalbody = $hdr . $this->_finalbody;
Derek Allard2067d1a2008-11-13 22:59:24 +00001075 }
1076
Derek Allard2067d1a2008-11-13 22:59:24 +00001077
1078 if ($this->send_multipart !== FALSE)
1079 {
Brandon Jones485d7412010-11-09 16:38:17 -05001080 $this->_finalbody .= "--" . $this->_alt_boundary . "--";
Derek Allard2067d1a2008-11-13 22:59:24 +00001081 }
1082
Derek Allard2067d1a2008-11-13 22:59:24 +00001083 return;
1084
1085 break;
1086 case 'plain-attach' :
1087
Derek Jonesa45e7612009-02-10 18:33:01 +00001088 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001089
1090 if ($this->_get_protocol() == 'mail')
1091 {
1092 $this->_header_str .= $hdr;
Eric Barnes6113f542010-12-29 13:36:12 -05001093 }
1094
Brandon Jones485d7412010-11-09 16:38:17 -05001095 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1096 $body .= "--" . $this->_atc_boundary . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001097
Brandon Jones485d7412010-11-09 16:38:17 -05001098 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1099 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001100
Brandon Jones485d7412010-11-09 16:38:17 -05001101 $body .= $this->_body . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001102
1103 break;
1104 case 'html-attach' :
1105
Derek Jonesa45e7612009-02-10 18:33:01 +00001106 $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
Eric Barnes6113f542010-12-29 13:36:12 -05001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 if ($this->_get_protocol() == 'mail')
1109 {
1110 $this->_header_str .= $hdr;
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 }
1112
Brandon Jones485d7412010-11-09 16:38:17 -05001113 $body .= $this->_get_mime_message() . $this->newline . $this->newline;
1114 $body .= "--" . $this->_atc_boundary . $this->newline;
1115
1116 $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
1117 $body .= "--" . $this->_alt_boundary . $this->newline;
1118
1119 $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
1120 $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
1121 $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
1122
1123 $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
1124 $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
1125
1126 $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
1127 $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001128
1129 break;
1130 }
1131
1132 $attachment = array();
1133
1134 $z = 0;
1135
1136 for ($i=0; $i < count($this->_attach_name); $i++)
1137 {
1138 $filename = $this->_attach_name[$i];
1139 $basename = basename($filename);
1140 $ctype = $this->_attach_type[$i];
1141
1142 if ( ! file_exists($filename))
1143 {
patworkb0707982011-04-08 15:10:05 +02001144 $this->_set_error_message('lang:email_attachment_missing', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 return FALSE;
1146 }
1147
Derek Jones37f4b9c2011-07-01 17:56:50 -05001148 $h = "--".$this->_atc_boundary.$this->newline;
Derek Allard2067d1a2008-11-13 22:59:24 +00001149 $h .= "Content-type: ".$ctype."; ";
1150 $h .= "name=\"".$basename."\"".$this->newline;
1151 $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
1152 $h .= "Content-Transfer-Encoding: base64".$this->newline;
1153
1154 $attachment[$z++] = $h;
1155 $file = filesize($filename) +1;
1156
1157 if ( ! $fp = fopen($filename, FOPEN_READ))
1158 {
patworkb0707982011-04-08 15:10:05 +02001159 $this->_set_error_message('lang:email_attachment_unreadable', $filename);
Derek Allard2067d1a2008-11-13 22:59:24 +00001160 return FALSE;
1161 }
1162
1163 $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
1164 fclose($fp);
1165 }
1166
Brandon Jones485d7412010-11-09 16:38:17 -05001167 $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
Eric Barnes6113f542010-12-29 13:36:12 -05001168
Brandon Jones485d7412010-11-09 16:38:17 -05001169
Derek Allard2067d1a2008-11-13 22:59:24 +00001170 if ($this->_get_protocol() == 'mail')
1171 {
Brandon Jones485d7412010-11-09 16:38:17 -05001172 $this->_finalbody = $body;
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 }
Brandon Jones485d7412010-11-09 16:38:17 -05001174 else
1175 {
1176 $this->_finalbody = $hdr . $body;
1177 }
Eric Barnes6113f542010-12-29 13:36:12 -05001178
Derek Allard2067d1a2008-11-13 22:59:24 +00001179 return;
1180 }
Barry Mienydd671972010-10-04 16:33:58 +02001181
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 // --------------------------------------------------------------------
1183
1184 /**
1185 * Prep Quoted Printable
1186 *
1187 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1188 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1189 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001190 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001191 * @param string
1192 * @param integer
1193 * @return string
1194 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001195 protected function _prep_quoted_printable($str, $charlim = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 // Set the character limit
1198 // Don't allow over 76, as that will make servers and MUAs barf
1199 // all over quoted-printable data
1200 if ($charlim == '' OR $charlim > '76')
1201 {
1202 $charlim = '76';
1203 }
1204
1205 // Reduce multiple spaces
1206 $str = preg_replace("| +|", " ", $str);
1207
1208 // kill nulls
1209 $str = preg_replace('/\x00+/', '', $str);
1210
1211 // Standardize newlines
1212 if (strpos($str, "\r") !== FALSE)
1213 {
1214 $str = str_replace(array("\r\n", "\r"), "\n", $str);
1215 }
1216
1217 // We are intentionally wrapping so mail servers will encode characters
1218 // properly and MUAs will behave, so {unwrap} must go!
1219 $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1220
1221 // Break into an array of lines
1222 $lines = explode("\n", $str);
1223
1224 $escape = '=';
1225 $output = '';
1226
1227 foreach ($lines as $line)
1228 {
1229 $length = strlen($line);
1230 $temp = '';
1231
1232 // Loop through each character in the line to add soft-wrap
1233 // characters at the end of a line " =\r\n" and add the newly
1234 // processed line(s) to the output (see comment on $crlf class property)
1235 for ($i = 0; $i < $length; $i++)
1236 {
1237 // Grab the next character
1238 $char = substr($line, $i, 1);
1239 $ascii = ord($char);
1240
1241 // Convert spaces and tabs but only if it's the end of the line
1242 if ($i == ($length - 1))
1243 {
1244 $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
1245 }
1246
1247 // encode = signs
1248 if ($ascii == '61')
1249 {
Derek Jones37f4b9c2011-07-01 17:56:50 -05001250 $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
Derek Allard2067d1a2008-11-13 22:59:24 +00001251 }
1252
1253 // If we're at the character limit, add the line to the output,
1254 // reset our temp variable, and keep on chuggin'
1255 if ((strlen($temp) + strlen($char)) >= $charlim)
1256 {
1257 $output .= $temp.$escape.$this->crlf;
1258 $temp = '';
1259 }
1260
1261 // Add the character to our temporary line
1262 $temp .= $char;
1263 }
1264
1265 // Add our completed line to the output
1266 $output .= $temp.$this->crlf;
1267 }
1268
1269 // get rid of extra CRLF tacked onto the end
1270 $output = substr($output, 0, strlen($this->crlf) * -1);
1271
1272 return $output;
1273 }
1274
1275 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001276
Derek Allard2067d1a2008-11-13 22:59:24 +00001277 /**
1278 * Prep Q Encoding
1279 *
Derek Jones37f4b9c2011-07-01 17:56:50 -05001280 * Performs "Q Encoding" on a string for use in email headers. It's related
Derek Allard2067d1a2008-11-13 22:59:24 +00001281 * but not identical to quoted-printable, so it has its own method
1282 *
1283 * @access public
1284 * @param str
1285 * @param bool // set to TRUE for processing From: headers
1286 * @return str
1287 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001288 protected function _prep_q_encoding($str, $from = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001289 {
1290 $str = str_replace(array("\r", "\n"), array('', ''), $str);
1291
1292 // Line length must not exceed 76 characters, so we adjust for
1293 // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
1294 $limit = 75 - 7 - strlen($this->charset);
1295
1296 // these special characters must be converted too
1297 $convert = array('_', '=', '?');
1298
1299 if ($from === TRUE)
1300 {
1301 $convert[] = ',';
1302 $convert[] = ';';
1303 }
1304
1305 $output = '';
1306 $temp = '';
1307
1308 for ($i = 0, $length = strlen($str); $i < $length; $i++)
1309 {
1310 // Grab the next character
1311 $char = substr($str, $i, 1);
1312 $ascii = ord($char);
1313
1314 // convert ALL non-printable ASCII characters and our specials
1315 if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
1316 {
1317 $char = '='.dechex($ascii);
1318 }
1319
1320 // handle regular spaces a bit more compactly than =20
1321 if ($ascii == 32)
1322 {
1323 $char = '_';
1324 }
1325
1326 // If we're at the character limit, add the line to the output,
1327 // reset our temp variable, and keep on chuggin'
1328 if ((strlen($temp) + strlen($char)) >= $limit)
1329 {
1330 $output .= $temp.$this->crlf;
1331 $temp = '';
1332 }
1333
1334 // Add the character to our temporary line
1335 $temp .= $char;
1336 }
1337
1338 $str = $output.$temp;
1339
1340 // wrap each line with the shebang, charset, and transfer encoding
1341 // the preceding space on successive lines is required for header "folding"
1342 $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
1343
1344 return $str;
1345 }
1346
1347 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 /**
1350 * Send Email
1351 *
1352 * @access public
1353 * @return bool
1354 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001355 public function send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001356 {
1357 if ($this->_replyto_flag == FALSE)
1358 {
1359 $this->reply_to($this->_headers['From']);
1360 }
1361
Derek Jones37f4b9c2011-07-01 17:56:50 -05001362 if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
Derek Allard2067d1a2008-11-13 22:59:24 +00001363 ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
1364 ( ! isset($this->_headers['Cc'])))
1365 {
patworkb0707982011-04-08 15:10:05 +02001366 $this->_set_error_message('lang:email_no_recipients');
Derek Allard2067d1a2008-11-13 22:59:24 +00001367 return FALSE;
1368 }
1369
1370 $this->_build_headers();
1371
Derek Jones37f4b9c2011-07-01 17:56:50 -05001372 if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001373 {
1374 if (count($this->_bcc_array) > $this->bcc_batch_size)
1375 return $this->batch_bcc_send();
1376 }
1377
1378 $this->_build_message();
1379
1380 if ( ! $this->_spool_email())
1381 {
1382 return FALSE;
1383 }
1384 else
1385 {
1386 return TRUE;
1387 }
1388 }
Barry Mienydd671972010-10-04 16:33:58 +02001389
Derek Allard2067d1a2008-11-13 22:59:24 +00001390 // --------------------------------------------------------------------
1391
1392 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001393 * Batch Bcc Send. Sends groups of BCCs in batches
Derek Allard2067d1a2008-11-13 22:59:24 +00001394 *
1395 * @access public
1396 * @return bool
1397 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001398 public function batch_bcc_send()
Derek Allard2067d1a2008-11-13 22:59:24 +00001399 {
1400 $float = $this->bcc_batch_size -1;
1401
1402 $set = "";
1403
1404 $chunk = array();
1405
1406 for ($i = 0; $i < count($this->_bcc_array); $i++)
1407 {
1408 if (isset($this->_bcc_array[$i]))
1409 {
1410 $set .= ", ".$this->_bcc_array[$i];
1411 }
1412
1413 if ($i == $float)
1414 {
1415 $chunk[] = substr($set, 1);
1416 $float = $float + $this->bcc_batch_size;
1417 $set = "";
1418 }
1419
1420 if ($i == count($this->_bcc_array)-1)
1421 {
1422 $chunk[] = substr($set, 1);
1423 }
1424 }
1425
1426 for ($i = 0; $i < count($chunk); $i++)
1427 {
1428 unset($this->_headers['Bcc']);
1429 unset($bcc);
1430
1431 $bcc = $this->_str_to_array($chunk[$i]);
1432 $bcc = $this->clean_email($bcc);
1433
1434 if ($this->protocol != 'smtp')
1435 {
1436 $this->_set_header('Bcc', implode(", ", $bcc));
1437 }
1438 else
1439 {
1440 $this->_bcc_array = $bcc;
1441 }
1442
1443 $this->_build_message();
1444 $this->_spool_email();
1445 }
1446 }
Barry Mienydd671972010-10-04 16:33:58 +02001447
Derek Allard2067d1a2008-11-13 22:59:24 +00001448 // --------------------------------------------------------------------
1449
1450 /**
1451 * Unwrap special elements
1452 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001453 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001454 * @return void
1455 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001456 protected function _unwrap_specials()
Derek Allard2067d1a2008-11-13 22:59:24 +00001457 {
1458 $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
1459 }
Barry Mienydd671972010-10-04 16:33:58 +02001460
Derek Allard2067d1a2008-11-13 22:59:24 +00001461 // --------------------------------------------------------------------
1462
1463 /**
1464 * Strip line-breaks via callback
1465 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001466 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001467 * @return string
1468 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001469 protected function _remove_nl_callback($matches)
Derek Allard2067d1a2008-11-13 22:59:24 +00001470 {
1471 if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1472 {
1473 $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1474 }
1475
1476 return $matches[1];
1477 }
Barry Mienydd671972010-10-04 16:33:58 +02001478
Derek Allard2067d1a2008-11-13 22:59:24 +00001479 // --------------------------------------------------------------------
1480
1481 /**
1482 * Spool mail to the mail server
1483 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001484 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001485 * @return bool
1486 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001487 protected function _spool_email()
Derek Allard2067d1a2008-11-13 22:59:24 +00001488 {
1489 $this->_unwrap_specials();
1490
1491 switch ($this->_get_protocol())
1492 {
1493 case 'mail' :
1494
1495 if ( ! $this->_send_with_mail())
1496 {
patworkb0707982011-04-08 15:10:05 +02001497 $this->_set_error_message('lang:email_send_failure_phpmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001498 return FALSE;
1499 }
1500 break;
1501 case 'sendmail' :
1502
1503 if ( ! $this->_send_with_sendmail())
1504 {
patworkb0707982011-04-08 15:10:05 +02001505 $this->_set_error_message('lang:email_send_failure_sendmail');
Derek Allard2067d1a2008-11-13 22:59:24 +00001506 return FALSE;
1507 }
1508 break;
1509 case 'smtp' :
1510
1511 if ( ! $this->_send_with_smtp())
1512 {
patworkb0707982011-04-08 15:10:05 +02001513 $this->_set_error_message('lang:email_send_failure_smtp');
Derek Allard2067d1a2008-11-13 22:59:24 +00001514 return FALSE;
1515 }
1516 break;
1517
1518 }
1519
patworkb0707982011-04-08 15:10:05 +02001520 $this->_set_error_message('lang:email_sent', $this->_get_protocol());
Derek Allard2067d1a2008-11-13 22:59:24 +00001521 return TRUE;
1522 }
Barry Mienydd671972010-10-04 16:33:58 +02001523
Derek Allard2067d1a2008-11-13 22:59:24 +00001524 // --------------------------------------------------------------------
1525
1526 /**
1527 * Send using mail()
1528 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001529 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001530 * @return bool
1531 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001532 protected function _send_with_mail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001533 {
1534 if ($this->_safe_mode == TRUE)
1535 {
1536 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
1537 {
1538 return FALSE;
1539 }
1540 else
1541 {
1542 return TRUE;
1543 }
1544 }
1545 else
1546 {
1547 // most documentation of sendmail using the "-f" flag lacks a space after it, however
1548 // we've encountered servers that seem to require it to be in place.
Eric Barnes6113f542010-12-29 13:36:12 -05001549
Derek Allard2067d1a2008-11-13 22:59:24 +00001550 if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
1551 {
1552 return FALSE;
1553 }
1554 else
1555 {
1556 return TRUE;
1557 }
1558 }
1559 }
Barry Mienydd671972010-10-04 16:33:58 +02001560
Derek Allard2067d1a2008-11-13 22:59:24 +00001561 // --------------------------------------------------------------------
1562
1563 /**
1564 * Send using Sendmail
1565 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001566 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001567 * @return bool
1568 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001569 protected function _send_with_sendmail()
Derek Allard2067d1a2008-11-13 22:59:24 +00001570 {
1571 $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
1572
Derek Jones4cefaa42009-04-29 19:13:56 +00001573 if ($fp === FALSE OR $fp === NULL)
1574 {
1575 // server probably has popen disabled, so nothing we can do to get a verbose error.
1576 return FALSE;
1577 }
Derek Jones71141ce2010-03-02 16:41:20 -06001578
Derek Jonesc630bcf2008-11-17 21:09:45 +00001579 fputs($fp, $this->_header_str);
1580 fputs($fp, $this->_finalbody);
1581
Barry Mienydd671972010-10-04 16:33:58 +02001582 $status = pclose($fp);
Eric Barnes6113f542010-12-29 13:36:12 -05001583
Derek Jonesc630bcf2008-11-17 21:09:45 +00001584 if (version_compare(PHP_VERSION, '4.2.3') == -1)
1585 {
1586 $status = $status >> 8 & 0xFF;
Barry Mienydd671972010-10-04 16:33:58 +02001587 }
Derek Jones71141ce2010-03-02 16:41:20 -06001588
Derek Jones604873f2008-11-18 15:57:24 +00001589 if ($status != 0)
Derek Allard2067d1a2008-11-13 22:59:24 +00001590 {
patworkb0707982011-04-08 15:10:05 +02001591 $this->_set_error_message('lang:email_exit_status', $status);
1592 $this->_set_error_message('lang:email_no_socket');
Derek Allard2067d1a2008-11-13 22:59:24 +00001593 return FALSE;
1594 }
1595
Derek Allard2067d1a2008-11-13 22:59:24 +00001596 return TRUE;
1597 }
Barry Mienydd671972010-10-04 16:33:58 +02001598
Derek Allard2067d1a2008-11-13 22:59:24 +00001599 // --------------------------------------------------------------------
1600
1601 /**
1602 * Send using SMTP
1603 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001604 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001605 * @return bool
1606 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001607 protected function _send_with_smtp()
Derek Allard2067d1a2008-11-13 22:59:24 +00001608 {
1609 if ($this->smtp_host == '')
1610 {
patworkb0707982011-04-08 15:10:05 +02001611 $this->_set_error_message('lang:email_no_hostname');
Derek Allard2067d1a2008-11-13 22:59:24 +00001612 return FALSE;
1613 }
1614
1615 $this->_smtp_connect();
1616 $this->_smtp_authenticate();
1617
1618 $this->_send_command('from', $this->clean_email($this->_headers['From']));
1619
Pascal Kriete14287f32011-02-14 13:39:34 -05001620 foreach ($this->_recipients as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001621 {
1622 $this->_send_command('to', $val);
1623 }
1624
1625 if (count($this->_cc_array) > 0)
1626 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001627 foreach ($this->_cc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001628 {
1629 if ($val != "")
1630 {
1631 $this->_send_command('to', $val);
1632 }
1633 }
1634 }
1635
1636 if (count($this->_bcc_array) > 0)
1637 {
Pascal Kriete14287f32011-02-14 13:39:34 -05001638 foreach ($this->_bcc_array as $val)
Derek Allard2067d1a2008-11-13 22:59:24 +00001639 {
1640 if ($val != "")
1641 {
1642 $this->_send_command('to', $val);
1643 }
1644 }
1645 }
1646
1647 $this->_send_command('data');
1648
1649 // perform dot transformation on any lines that begin with a dot
1650 $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
1651
1652 $this->_send_data('.');
1653
1654 $reply = $this->_get_smtp_data();
1655
1656 $this->_set_error_message($reply);
1657
1658 if (strncmp($reply, '250', 3) != 0)
1659 {
patworkb0707982011-04-08 15:10:05 +02001660 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001661 return FALSE;
1662 }
1663
1664 $this->_send_command('quit');
1665 return TRUE;
1666 }
Barry Mienydd671972010-10-04 16:33:58 +02001667
Derek Allard2067d1a2008-11-13 22:59:24 +00001668 // --------------------------------------------------------------------
1669
1670 /**
1671 * SMTP Connect
1672 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001673 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001674 * @param string
1675 * @return string
1676 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001677 protected function _smtp_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +00001678 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001679 $ssl = NULL;
Radu Potop4c589ae2011-09-29 10:19:55 +03001680
Radu Potopbbf04b02011-09-28 13:57:51 +03001681 if ($this->smtp_crypto == 'ssl')
Radu Potop4c589ae2011-09-29 10:19:55 +03001682 {
Radu Potopbbf04b02011-09-28 13:57:51 +03001683 $ssl = 'ssl://';
Radu Potop4c589ae2011-09-29 10:19:55 +03001684 }
1685
Radu Potopbbf04b02011-09-28 13:57:51 +03001686 $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
Derek Allard2067d1a2008-11-13 22:59:24 +00001687 $this->smtp_port,
1688 $errno,
1689 $errstr,
1690 $this->smtp_timeout);
1691
Pascal Kriete14287f32011-02-14 13:39:34 -05001692 if ( ! is_resource($this->_smtp_connect))
Derek Allard2067d1a2008-11-13 22:59:24 +00001693 {
patworkb0707982011-04-08 15:10:05 +02001694 $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
Derek Allard2067d1a2008-11-13 22:59:24 +00001695 return FALSE;
1696 }
1697
1698 $this->_set_error_message($this->_get_smtp_data());
Radu Potopbbf04b02011-09-28 13:57:51 +03001699
1700 if ($this->smtp_crypto == 'tls')
1701 {
1702 $this->_send_command('hello');
1703 $this->_send_command('starttls');
Radu Potop4c589ae2011-09-29 10:19:55 +03001704 $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
1705 }
1706
1707 if ($crypto !== TRUE)
1708 {
1709 $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
1710 return FALSE;
Radu Potopbbf04b02011-09-28 13:57:51 +03001711 }
1712
Derek Allard2067d1a2008-11-13 22:59:24 +00001713 return $this->_send_command('hello');
1714 }
Barry Mienydd671972010-10-04 16:33:58 +02001715
Derek Allard2067d1a2008-11-13 22:59:24 +00001716 // --------------------------------------------------------------------
1717
1718 /**
1719 * Send SMTP command
1720 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001721 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001722 * @param string
1723 * @param string
1724 * @return string
1725 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001726 protected function _send_command($cmd, $data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001727 {
1728 switch ($cmd)
1729 {
1730 case 'hello' :
1731
1732 if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
1733 $this->_send_data('EHLO '.$this->_get_hostname());
1734 else
1735 $this->_send_data('HELO '.$this->_get_hostname());
1736
1737 $resp = 250;
1738 break;
Radu Potopbbf04b02011-09-28 13:57:51 +03001739 case 'starttls' :
1740
1741 $this->_send_data('STARTTLS');
1742
1743 $resp = 220;
1744 break;
Derek Allard2067d1a2008-11-13 22:59:24 +00001745 case 'from' :
1746
1747 $this->_send_data('MAIL FROM:<'.$data.'>');
1748
1749 $resp = 250;
1750 break;
1751 case 'to' :
1752
1753 $this->_send_data('RCPT TO:<'.$data.'>');
1754
1755 $resp = 250;
1756 break;
1757 case 'data' :
1758
1759 $this->_send_data('DATA');
1760
1761 $resp = 354;
1762 break;
1763 case 'quit' :
1764
1765 $this->_send_data('QUIT');
1766
1767 $resp = 221;
1768 break;
1769 }
1770
1771 $reply = $this->_get_smtp_data();
1772
1773 $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
1774
1775 if (substr($reply, 0, 3) != $resp)
1776 {
patworkb0707982011-04-08 15:10:05 +02001777 $this->_set_error_message('lang:email_smtp_error', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001778 return FALSE;
1779 }
1780
1781 if ($cmd == 'quit')
1782 {
1783 fclose($this->_smtp_connect);
1784 }
1785
1786 return TRUE;
1787 }
Barry Mienydd671972010-10-04 16:33:58 +02001788
Derek Allard2067d1a2008-11-13 22:59:24 +00001789 // --------------------------------------------------------------------
1790
1791 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -05001792 * SMTP Authenticate
Derek Allard2067d1a2008-11-13 22:59:24 +00001793 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001794 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001795 * @return bool
1796 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001797 protected function _smtp_authenticate()
Derek Allard2067d1a2008-11-13 22:59:24 +00001798 {
1799 if ( ! $this->_smtp_auth)
1800 {
1801 return TRUE;
1802 }
1803
Derek Jones37f4b9c2011-07-01 17:56:50 -05001804 if ($this->smtp_user == "" AND $this->smtp_pass == "")
Derek Allard2067d1a2008-11-13 22:59:24 +00001805 {
patworkb0707982011-04-08 15:10:05 +02001806 $this->_set_error_message('lang:email_no_smtp_unpw');
Derek Allard2067d1a2008-11-13 22:59:24 +00001807 return FALSE;
1808 }
1809
1810 $this->_send_data('AUTH LOGIN');
1811
1812 $reply = $this->_get_smtp_data();
1813
1814 if (strncmp($reply, '334', 3) != 0)
1815 {
patworkb0707982011-04-08 15:10:05 +02001816 $this->_set_error_message('lang:email_failed_smtp_login', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001817 return FALSE;
1818 }
1819
1820 $this->_send_data(base64_encode($this->smtp_user));
1821
1822 $reply = $this->_get_smtp_data();
1823
1824 if (strncmp($reply, '334', 3) != 0)
1825 {
patworkb0707982011-04-08 15:10:05 +02001826 $this->_set_error_message('lang:email_smtp_auth_un', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001827 return FALSE;
1828 }
1829
1830 $this->_send_data(base64_encode($this->smtp_pass));
1831
1832 $reply = $this->_get_smtp_data();
1833
1834 if (strncmp($reply, '235', 3) != 0)
1835 {
patworkb0707982011-04-08 15:10:05 +02001836 $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
Derek Allard2067d1a2008-11-13 22:59:24 +00001837 return FALSE;
1838 }
1839
1840 return TRUE;
1841 }
Barry Mienydd671972010-10-04 16:33:58 +02001842
Derek Allard2067d1a2008-11-13 22:59:24 +00001843 // --------------------------------------------------------------------
1844
1845 /**
1846 * Send SMTP data
1847 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001848 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001849 * @return bool
1850 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001851 protected function _send_data($data)
Derek Allard2067d1a2008-11-13 22:59:24 +00001852 {
1853 if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
1854 {
patworkb0707982011-04-08 15:10:05 +02001855 $this->_set_error_message('lang:email_smtp_data_failure', $data);
Derek Allard2067d1a2008-11-13 22:59:24 +00001856 return FALSE;
1857 }
1858 else
1859 {
1860 return TRUE;
1861 }
1862 }
Barry Mienydd671972010-10-04 16:33:58 +02001863
Derek Allard2067d1a2008-11-13 22:59:24 +00001864 // --------------------------------------------------------------------
1865
1866 /**
1867 * Get SMTP data
1868 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001869 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001870 * @return string
1871 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001872 protected function _get_smtp_data()
Derek Allard2067d1a2008-11-13 22:59:24 +00001873 {
1874 $data = "";
1875
1876 while ($str = fgets($this->_smtp_connect, 512))
1877 {
1878 $data .= $str;
1879
1880 if (substr($str, 3, 1) == " ")
1881 {
1882 break;
1883 }
1884 }
1885
1886 return $data;
1887 }
Barry Mienydd671972010-10-04 16:33:58 +02001888
Derek Allard2067d1a2008-11-13 22:59:24 +00001889 // --------------------------------------------------------------------
1890
1891 /**
1892 * Get Hostname
1893 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001894 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001895 * @return string
1896 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001897 protected function _get_hostname()
Derek Allard2067d1a2008-11-13 22:59:24 +00001898 {
1899 return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
1900 }
Barry Mienydd671972010-10-04 16:33:58 +02001901
Derek Allard2067d1a2008-11-13 22:59:24 +00001902 // --------------------------------------------------------------------
1903
1904 /**
1905 * Get IP
1906 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001907 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001908 * @return string
1909 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001910 protected function _get_ip()
Derek Allard2067d1a2008-11-13 22:59:24 +00001911 {
1912 if ($this->_IP !== FALSE)
1913 {
1914 return $this->_IP;
1915 }
1916
1917 $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
1918 $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
1919 $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
1920
Barry Mienydd671972010-10-04 16:33:58 +02001921 if ($cip && $rip) $this->_IP = $cip;
Derek Allard2067d1a2008-11-13 22:59:24 +00001922 elseif ($rip) $this->_IP = $rip;
1923 elseif ($cip) $this->_IP = $cip;
1924 elseif ($fip) $this->_IP = $fip;
1925
Robin Sowell76b369e2010-03-19 11:15:28 -04001926 if (strpos($this->_IP, ',') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +00001927 {
1928 $x = explode(',', $this->_IP);
1929 $this->_IP = end($x);
1930 }
1931
1932 if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
1933 {
1934 $this->_IP = '0.0.0.0';
1935 }
1936
1937 unset($cip);
1938 unset($rip);
1939 unset($fip);
1940
1941 return $this->_IP;
1942 }
Barry Mienydd671972010-10-04 16:33:58 +02001943
Derek Allard2067d1a2008-11-13 22:59:24 +00001944 // --------------------------------------------------------------------
1945
1946 /**
1947 * Get Debug Message
1948 *
1949 * @access public
1950 * @return string
1951 */
Phil Sturgeona0f980e2011-01-13 10:59:12 +00001952 public function print_debugger()
Derek Allard2067d1a2008-11-13 22:59:24 +00001953 {
1954 $msg = '';
1955
1956 if (count($this->_debug_msg) > 0)
1957 {
1958 foreach ($this->_debug_msg as $val)
1959 {
1960 $msg .= $val;
1961 }
1962 }
1963
1964 $msg .= "<pre>".$this->_header_str."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
1965 return $msg;
1966 }
Barry Mienydd671972010-10-04 16:33:58 +02001967
Derek Allard2067d1a2008-11-13 22:59:24 +00001968 // --------------------------------------------------------------------
1969
1970 /**
1971 * Set Message
1972 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001973 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001974 * @param string
1975 * @return string
1976 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001977 protected function _set_error_message($msg, $val = '')
Derek Allard2067d1a2008-11-13 22:59:24 +00001978 {
1979 $CI =& get_instance();
1980 $CI->lang->load('email');
1981
patworkb0707982011-04-08 15:10:05 +02001982 if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
Derek Allard2067d1a2008-11-13 22:59:24 +00001983 {
1984 $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
1985 }
1986 else
1987 {
1988 $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
1989 }
1990 }
Barry Mienydd671972010-10-04 16:33:58 +02001991
Derek Allard2067d1a2008-11-13 22:59:24 +00001992 // --------------------------------------------------------------------
1993
1994 /**
1995 * Mime Types
1996 *
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06001997 * @access protected
Derek Allard2067d1a2008-11-13 22:59:24 +00001998 * @param string
1999 * @return string
2000 */
Phil Sturgeon6d2f13a2011-07-20 10:04:52 -06002001 protected function _mime_types($ext = "")
Derek Allard2067d1a2008-11-13 22:59:24 +00002002 {
2003 $mimes = array( 'hqx' => 'application/mac-binhex40',
2004 'cpt' => 'application/mac-compactpro',
2005 'doc' => 'application/msword',
2006 'bin' => 'application/macbinary',
2007 'dms' => 'application/octet-stream',
2008 'lha' => 'application/octet-stream',
2009 'lzh' => 'application/octet-stream',
2010 'exe' => 'application/octet-stream',
2011 'class' => 'application/octet-stream',
2012 'psd' => 'application/octet-stream',
2013 'so' => 'application/octet-stream',
2014 'sea' => 'application/octet-stream',
2015 'dll' => 'application/octet-stream',
2016 'oda' => 'application/oda',
2017 'pdf' => 'application/pdf',
2018 'ai' => 'application/postscript',
2019 'eps' => 'application/postscript',
2020 'ps' => 'application/postscript',
2021 'smi' => 'application/smil',
2022 'smil' => 'application/smil',
2023 'mif' => 'application/vnd.mif',
2024 'xls' => 'application/vnd.ms-excel',
2025 'ppt' => 'application/vnd.ms-powerpoint',
2026 'wbxml' => 'application/vnd.wap.wbxml',
2027 'wmlc' => 'application/vnd.wap.wmlc',
2028 'dcr' => 'application/x-director',
2029 'dir' => 'application/x-director',
2030 'dxr' => 'application/x-director',
2031 'dvi' => 'application/x-dvi',
2032 'gtar' => 'application/x-gtar',
2033 'php' => 'application/x-httpd-php',
2034 'php4' => 'application/x-httpd-php',
2035 'php3' => 'application/x-httpd-php',
2036 'phtml' => 'application/x-httpd-php',
2037 'phps' => 'application/x-httpd-php-source',
2038 'js' => 'application/x-javascript',
2039 'swf' => 'application/x-shockwave-flash',
2040 'sit' => 'application/x-stuffit',
2041 'tar' => 'application/x-tar',
2042 'tgz' => 'application/x-tar',
2043 'xhtml' => 'application/xhtml+xml',
2044 'xht' => 'application/xhtml+xml',
2045 'zip' => 'application/zip',
2046 'mid' => 'audio/midi',
2047 'midi' => 'audio/midi',
2048 'mpga' => 'audio/mpeg',
2049 'mp2' => 'audio/mpeg',
2050 'mp3' => 'audio/mpeg',
2051 'aif' => 'audio/x-aiff',
2052 'aiff' => 'audio/x-aiff',
2053 'aifc' => 'audio/x-aiff',
2054 'ram' => 'audio/x-pn-realaudio',
2055 'rm' => 'audio/x-pn-realaudio',
2056 'rpm' => 'audio/x-pn-realaudio-plugin',
2057 'ra' => 'audio/x-realaudio',
2058 'rv' => 'video/vnd.rn-realvideo',
2059 'wav' => 'audio/x-wav',
2060 'bmp' => 'image/bmp',
2061 'gif' => 'image/gif',
2062 'jpeg' => 'image/jpeg',
2063 'jpg' => 'image/jpeg',
2064 'jpe' => 'image/jpeg',
2065 'png' => 'image/png',
2066 'tiff' => 'image/tiff',
2067 'tif' => 'image/tiff',
2068 'css' => 'text/css',
2069 'html' => 'text/html',
2070 'htm' => 'text/html',
2071 'shtml' => 'text/html',
2072 'txt' => 'text/plain',
2073 'text' => 'text/plain',
2074 'log' => 'text/plain',
2075 'rtx' => 'text/richtext',
2076 'rtf' => 'text/rtf',
2077 'xml' => 'text/xml',
2078 'xsl' => 'text/xml',
2079 'mpeg' => 'video/mpeg',
2080 'mpg' => 'video/mpeg',
2081 'mpe' => 'video/mpeg',
2082 'qt' => 'video/quicktime',
2083 'mov' => 'video/quicktime',
2084 'avi' => 'video/x-msvideo',
2085 'movie' => 'video/x-sgi-movie',
2086 'doc' => 'application/msword',
2087 'word' => 'application/msword',
2088 'xl' => 'application/excel',
2089 'eml' => 'message/rfc822'
2090 );
2091
2092 return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
2093 }
2094
2095}
2096// END CI_Email class
2097
2098/* End of file Email.php */
patworkb0707982011-04-08 15:10:05 +02002099/* Location: ./system/libraries/Email.php */