Subversion-Projekte lars-tiefland.ci

Revision

Revision 68 | Revision 2049 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * CodeIgniter Email Class
42
 *
43
 * Permits email to be sent using Mail, Sendmail, or SMTP.
44
 *
45
 * @package		CodeIgniter
46
 * @subpackage	Libraries
47
 * @category	Libraries
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/libraries/email.html
50
 */
51
class CI_Email {
52
 
53
	/**
54
	 * Used as the User-Agent and X-Mailer headers' value.
55
	 *
56
	 * @var	string
57
	 */
58
	public $useragent	= 'CodeIgniter';
59
 
60
	/**
61
	 * Path to the Sendmail binary.
62
	 *
63
	 * @var	string
64
	 */
65
	public $mailpath	= '/usr/sbin/sendmail';	// Sendmail path
66
 
67
	/**
68
	 * Which method to use for sending e-mails.
69
	 *
70
	 * @var	string	'mail', 'sendmail' or 'smtp'
71
	 */
72
	public $protocol	= 'mail';		// mail/sendmail/smtp
73
 
74
	/**
75
	 * STMP Server host
76
	 *
77
	 * @var	string
78
	 */
79
	public $smtp_host	= '';
80
 
81
	/**
82
	 * SMTP Username
83
	 *
84
	 * @var	string
85
	 */
86
	public $smtp_user	= '';
87
 
88
	/**
89
	 * SMTP Password
90
	 *
91
	 * @var	string
92
	 */
93
	public $smtp_pass	= '';
94
 
95
	/**
96
	 * SMTP Server port
97
	 *
98
	 * @var	int
99
	 */
100
	public $smtp_port	= 25;
101
 
102
	/**
103
	 * SMTP connection timeout in seconds
104
	 *
105
	 * @var	int
106
	 */
107
	public $smtp_timeout	= 5;
108
 
109
	/**
110
	 * SMTP persistent connection
111
	 *
112
	 * @var	bool
113
	 */
114
	public $smtp_keepalive	= FALSE;
115
 
116
	/**
117
	 * SMTP Encryption
118
	 *
119
	 * @var	string	empty, 'tls' or 'ssl'
120
	 */
121
	public $smtp_crypto	= '';
122
 
123
	/**
124
	 * Whether to apply word-wrapping to the message body.
125
	 *
126
	 * @var	bool
127
	 */
128
	public $wordwrap	= TRUE;
129
 
130
	/**
131
	 * Number of characters to wrap at.
132
	 *
133
	 * @see	CI_Email::$wordwrap
134
	 * @var	int
135
	 */
136
	public $wrapchars	= 76;
137
 
138
	/**
139
	 * Message format.
140
	 *
141
	 * @var	string	'text' or 'html'
142
	 */
143
	public $mailtype	= 'text';
144
 
145
	/**
146
	 * Character set (default: utf-8)
147
	 *
148
	 * @var	string
149
	 */
150
	public $charset		= 'UTF-8';
151
 
152
	/**
153
	 * Alternative message (for HTML messages only)
154
	 *
155
	 * @var	string
156
	 */
157
	public $alt_message	= '';
158
 
159
	/**
160
	 * Whether to validate e-mail addresses.
161
	 *
162
	 * @var	bool
163
	 */
164
	public $validate	= FALSE;
165
 
166
	/**
167
	 * X-Priority header value.
168
	 *
169
	 * @var	int	1-5
170
	 */
171
	public $priority	= 3;			// Default priority (1 - 5)
172
 
173
	/**
174
	 * Newline character sequence.
175
	 * Use "\r\n" to comply with RFC 822.
176
	 *
177
	 * @link	http://www.ietf.org/rfc/rfc822.txt
178
	 * @var	string	"\r\n" or "\n"
179
	 */
180
	public $newline		= "\n";			// Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
181
 
182
	/**
183
	 * CRLF character sequence
184
	 *
185
	 * RFC 2045 specifies that for 'quoted-printable' encoding,
186
	 * "\r\n" must be used. However, it appears that some servers
187
	 * (even on the receiving end) don't handle it properly and
188
	 * switching to "\n", while improper, is the only solution
189
	 * that seems to work for all environments.
190
	 *
191
	 * @link	http://www.ietf.org/rfc/rfc822.txt
192
	 * @var	string
193
	 */
194
	public $crlf		= "\n";
195
 
196
	/**
197
	 * Whether to use Delivery Status Notification.
198
	 *
199
	 * @var	bool
200
	 */
201
	public $dsn		= FALSE;
202
 
203
	/**
204
	 * Whether to send multipart alternatives.
205
	 * Yahoo! doesn't seem to like these.
206
	 *
207
	 * @var	bool
208
	 */
209
	public $send_multipart	= TRUE;
210
 
211
	/**
212
	 * Whether to send messages to BCC recipients in batches.
213
	 *
214
	 * @var	bool
215
	 */
216
	public $bcc_batch_mode	= FALSE;
217
 
218
	/**
219
	 * BCC Batch max number size.
220
	 *
221
	 * @see	CI_Email::$bcc_batch_mode
222
	 * @var	int
223
	 */
224
	public $bcc_batch_size	= 200;
225
 
226
	// --------------------------------------------------------------------
227
 
228
	/**
229
	 * Whether PHP is running in safe mode. Initialized by the class constructor.
230
	 *
231
	 * @var	bool
232
	 */
233
	protected $_safe_mode		= FALSE;
234
 
235
	/**
236
	 * Subject header
237
	 *
238
	 * @var	string
239
	 */
240
	protected $_subject		= '';
241
 
242
	/**
243
	 * Message body
244
	 *
245
	 * @var	string
246
	 */
247
	protected $_body		= '';
248
 
249
	/**
250
	 * Final message body to be sent.
251
	 *
252
	 * @var	string
253
	 */
254
	protected $_finalbody		= '';
255
 
256
	/**
257
	 * Final headers to send
258
	 *
259
	 * @var	string
260
	 */
261
	protected $_header_str		= '';
262
 
263
	/**
264
	 * SMTP Connection socket placeholder
265
	 *
266
	 * @var	resource
267
	 */
268
	protected $_smtp_connect	= '';
269
 
270
	/**
271
	 * Mail encoding
272
	 *
273
	 * @var	string	'8bit' or '7bit'
274
	 */
275
	protected $_encoding		= '8bit';
276
 
277
	/**
278
	 * Whether to perform SMTP authentication
279
	 *
280
	 * @var	bool
281
	 */
282
	protected $_smtp_auth		= FALSE;
283
 
284
	/**
285
	 * Whether to send a Reply-To header
286
	 *
287
	 * @var	bool
288
	 */
289
	protected $_replyto_flag	= FALSE;
290
 
291
	/**
292
	 * Debug messages
293
	 *
294
	 * @see	CI_Email::print_debugger()
295
	 * @var	string
296
	 */
297
	protected $_debug_msg		= array();
298
 
299
	/**
300
	 * Recipients
301
	 *
302
	 * @var	string[]
303
	 */
304
	protected $_recipients		= array();
305
 
306
	/**
307
	 * CC Recipients
308
	 *
309
	 * @var	string[]
310
	 */
311
	protected $_cc_array		= array();
312
 
313
	/**
314
	 * BCC Recipients
315
	 *
316
	 * @var	string[]
317
	 */
318
	protected $_bcc_array		= array();
319
 
320
	/**
321
	 * Message headers
322
	 *
323
	 * @var	string[]
324
	 */
325
	protected $_headers		= array();
326
 
327
	/**
328
	 * Attachment data
329
	 *
330
	 * @var	array
331
	 */
332
	protected $_attachments		= array();
333
 
334
	/**
335
	 * Valid $protocol values
336
	 *
337
	 * @see	CI_Email::$protocol
338
	 * @var	string[]
339
	 */
340
	protected $_protocols		= array('mail', 'sendmail', 'smtp');
341
 
342
	/**
343
	 * Base charsets
344
	 *
345
	 * Character sets valid for 7-bit encoding,
346
	 * excluding language suffix.
347
	 *
348
	 * @var	string[]
349
	 */
350
	protected $_base_charsets	= array('us-ascii', 'iso-2022-');
351
 
352
	/**
353
	 * Bit depths
354
	 *
355
	 * Valid mail encodings
356
	 *
357
	 * @see	CI_Email::$_encoding
358
	 * @var	string[]
359
	 */
360
	protected $_bit_depths		= array('7bit', '8bit');
361
 
362
	/**
363
	 * $priority translations
364
	 *
365
	 * Actual values to send with the X-Priority header
366
	 *
367
	 * @var	string[]
368
	 */
369
	protected $_priorities = array(
370
		1 => '1 (Highest)',
371
		2 => '2 (High)',
372
		3 => '3 (Normal)',
373
		4 => '4 (Low)',
374
		5 => '5 (Lowest)'
375
	);
376
 
1257 lars 377
	/**
378
	 * mbstring.func_override flag
379
	 *
380
	 * @var	bool
381
	 */
382
	protected static $func_override;
383
 
68 lars 384
	// --------------------------------------------------------------------
385
 
386
	/**
387
	 * Constructor - Sets Email Preferences
388
	 *
389
	 * The constructor can be passed an array of config values
390
	 *
391
	 * @param	array	$config = array()
392
	 * @return	void
393
	 */
394
	public function __construct(array $config = array())
395
	{
396
		$this->charset = config_item('charset');
397
		$this->initialize($config);
398
		$this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
399
 
1257 lars 400
		isset(self::$func_override) OR self::$func_override = (extension_loaded('mbstring') && ini_get('mbstring.func_override'));
401
 
68 lars 402
		log_message('info', 'Email Class Initialized');
403
	}
404
 
405
	// --------------------------------------------------------------------
406
 
407
	/**
408
	 * Initialize preferences
409
	 *
410
	 * @param	array	$config
411
	 * @return	CI_Email
412
	 */
413
	public function initialize(array $config = array())
414
	{
415
		$this->clear();
416
 
417
		foreach ($config as $key => $val)
418
		{
419
			if (isset($this->$key))
420
			{
421
				$method = 'set_'.$key;
422
 
423
				if (method_exists($this, $method))
424
				{
425
					$this->$method($val);
426
				}
427
				else
428
				{
429
					$this->$key = $val;
430
				}
431
			}
432
		}
433
 
434
		$this->charset = strtoupper($this->charset);
435
		$this->_smtp_auth = isset($this->smtp_user[0], $this->smtp_pass[0]);
436
 
437
		return $this;
438
	}
439
 
440
	// --------------------------------------------------------------------
441
 
442
	/**
443
	 * Initialize the Email Data
444
	 *
445
	 * @param	bool
446
	 * @return	CI_Email
447
	 */
448
	public function clear($clear_attachments = FALSE)
449
	{
450
		$this->_subject		= '';
451
		$this->_body		= '';
452
		$this->_finalbody	= '';
453
		$this->_header_str	= '';
454
		$this->_replyto_flag	= FALSE;
455
		$this->_recipients	= array();
456
		$this->_cc_array	= array();
457
		$this->_bcc_array	= array();
458
		$this->_headers		= array();
459
		$this->_debug_msg	= array();
460
 
461
		$this->set_header('User-Agent', $this->useragent);
462
		$this->set_header('Date', $this->_set_date());
463
 
464
		if ($clear_attachments !== FALSE)
465
		{
466
			$this->_attachments = array();
467
		}
468
 
469
		return $this;
470
	}
471
 
472
	// --------------------------------------------------------------------
473
 
474
	/**
475
	 * Set FROM
476
	 *
477
	 * @param	string	$from
478
	 * @param	string	$name
479
	 * @param	string	$return_path = NULL	Return-Path
480
	 * @return	CI_Email
481
	 */
482
	public function from($from, $name = '', $return_path = NULL)
483
	{
484
		if (preg_match('/\<(.*)\>/', $from, $match))
485
		{
486
			$from = $match[1];
487
		}
488
 
489
		if ($this->validate)
490
		{
491
			$this->validate_email($this->_str_to_array($from));
492
			if ($return_path)
493
			{
494
				$this->validate_email($this->_str_to_array($return_path));
495
			}
496
		}
497
 
498
		// prepare the display name
499
		if ($name !== '')
500
		{
501
			// only use Q encoding if there are characters that would require it
502
			if ( ! preg_match('/[\200-\377]/', $name))
503
			{
504
				// add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
505
				$name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
506
			}
507
			else
508
			{
509
				$name = $this->_prep_q_encoding($name);
510
			}
511
		}
512
 
513
		$this->set_header('From', $name.' <'.$from.'>');
514
 
515
		isset($return_path) OR $return_path = $from;
516
		$this->set_header('Return-Path', '<'.$return_path.'>');
517
 
518
		return $this;
519
	}
520
 
521
	// --------------------------------------------------------------------
522
 
523
	/**
524
	 * Set Reply-to
525
	 *
526
	 * @param	string
527
	 * @param	string
528
	 * @return	CI_Email
529
	 */
530
	public function reply_to($replyto, $name = '')
531
	{
532
		if (preg_match('/\<(.*)\>/', $replyto, $match))
533
		{
534
			$replyto = $match[1];
535
		}
536
 
537
		if ($this->validate)
538
		{
539
			$this->validate_email($this->_str_to_array($replyto));
540
		}
541
 
542
		if ($name !== '')
543
		{
544
			// only use Q encoding if there are characters that would require it
545
			if ( ! preg_match('/[\200-\377]/', $name))
546
			{
547
				// add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
548
				$name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
549
			}
550
			else
551
			{
552
				$name = $this->_prep_q_encoding($name);
553
			}
554
		}
555
 
556
		$this->set_header('Reply-To', $name.' <'.$replyto.'>');
557
		$this->_replyto_flag = TRUE;
558
 
559
		return $this;
560
	}
561
 
562
	// --------------------------------------------------------------------
563
 
564
	/**
565
	 * Set Recipients
566
	 *
567
	 * @param	string
568
	 * @return	CI_Email
569
	 */
570
	public function to($to)
571
	{
572
		$to = $this->_str_to_array($to);
573
		$to = $this->clean_email($to);
574
 
575
		if ($this->validate)
576
		{
577
			$this->validate_email($to);
578
		}
579
 
580
		if ($this->_get_protocol() !== 'mail')
581
		{
582
			$this->set_header('To', implode(', ', $to));
583
		}
584
 
585
		$this->_recipients = $to;
586
 
587
		return $this;
588
	}
589
 
590
	// --------------------------------------------------------------------
591
 
592
	/**
593
	 * Set CC
594
	 *
595
	 * @param	string
596
	 * @return	CI_Email
597
	 */
598
	public function cc($cc)
599
	{
600
		$cc = $this->clean_email($this->_str_to_array($cc));
601
 
602
		if ($this->validate)
603
		{
604
			$this->validate_email($cc);
605
		}
606
 
607
		$this->set_header('Cc', implode(', ', $cc));
608
 
609
		if ($this->_get_protocol() === 'smtp')
610
		{
611
			$this->_cc_array = $cc;
612
		}
613
 
614
		return $this;
615
	}
616
 
617
	// --------------------------------------------------------------------
618
 
619
	/**
620
	 * Set BCC
621
	 *
622
	 * @param	string
623
	 * @param	string
624
	 * @return	CI_Email
625
	 */
626
	public function bcc($bcc, $limit = '')
627
	{
628
		if ($limit !== '' && is_numeric($limit))
629
		{
630
			$this->bcc_batch_mode = TRUE;
631
			$this->bcc_batch_size = $limit;
632
		}
633
 
634
		$bcc = $this->clean_email($this->_str_to_array($bcc));
635
 
636
		if ($this->validate)
637
		{
638
			$this->validate_email($bcc);
639
		}
640
 
641
		if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
642
		{
643
			$this->_bcc_array = $bcc;
644
		}
645
		else
646
		{
647
			$this->set_header('Bcc', implode(', ', $bcc));
648
		}
649
 
650
		return $this;
651
	}
652
 
653
	// --------------------------------------------------------------------
654
 
655
	/**
656
	 * Set Email Subject
657
	 *
658
	 * @param	string
659
	 * @return	CI_Email
660
	 */
661
	public function subject($subject)
662
	{
663
		$subject = $this->_prep_q_encoding($subject);
664
		$this->set_header('Subject', $subject);
665
		return $this;
666
	}
667
 
668
	// --------------------------------------------------------------------
669
 
670
	/**
671
	 * Set Body
672
	 *
673
	 * @param	string
674
	 * @return	CI_Email
675
	 */
676
	public function message($body)
677
	{
678
		$this->_body = rtrim(str_replace("\r", '', $body));
679
 
680
		/* strip slashes only if magic quotes is ON
681
		   if we do it with magic quotes OFF, it strips real, user-inputted chars.
682
 
683
		   NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
684
			 it will probably not exist in future versions at all.
685
		*/
686
		if ( ! is_php('5.4') && get_magic_quotes_gpc())
687
		{
688
			$this->_body = stripslashes($this->_body);
689
		}
690
 
691
		return $this;
692
	}
693
 
694
	// --------------------------------------------------------------------
695
 
696
	/**
697
	 * Assign file attachments
698
	 *
699
	 * @param	string	$file	Can be local path, URL or buffered content
700
	 * @param	string	$disposition = 'attachment'
701
	 * @param	string	$newname = NULL
702
	 * @param	string	$mime = ''
703
	 * @return	CI_Email
704
	 */
705
	public function attach($file, $disposition = '', $newname = NULL, $mime = '')
706
	{
707
		if ($mime === '')
708
		{
709
			if (strpos($file, '://') === FALSE && ! file_exists($file))
710
			{
711
				$this->_set_error_message('lang:email_attachment_missing', $file);
712
				return FALSE;
713
			}
714
 
715
			if ( ! $fp = @fopen($file, 'rb'))
716
			{
717
				$this->_set_error_message('lang:email_attachment_unreadable', $file);
718
				return FALSE;
719
			}
720
 
721
			$file_content = stream_get_contents($fp);
722
			$mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
723
			fclose($fp);
724
		}
725
		else
726
		{
727
			$file_content =& $file; // buffered file
728
		}
729
 
730
		$this->_attachments[] = array(
731
			'name'		=> array($file, $newname),
732
			'disposition'	=> empty($disposition) ? 'attachment' : $disposition,  // Can also be 'inline'  Not sure if it matters
733
			'type'		=> $mime,
734
			'content'	=> chunk_split(base64_encode($file_content)),
735
			'multipart'	=> 'mixed'
736
		);
737
 
738
		return $this;
739
	}
740
 
741
	// --------------------------------------------------------------------
742
 
743
	/**
744
	 * Set and return attachment Content-ID
745
	 *
746
	 * Useful for attached inline pictures
747
	 *
748
	 * @param	string	$filename
749
	 * @return	string
750
	 */
751
	public function attachment_cid($filename)
752
	{
753
		for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
754
		{
755
			if ($this->_attachments[$i]['name'][0] === $filename)
756
			{
757
				$this->_attachments[$i]['multipart'] = 'related';
758
				$this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
759
				return $this->_attachments[$i]['cid'];
760
			}
761
		}
762
 
763
		return FALSE;
764
	}
765
 
766
	// --------------------------------------------------------------------
767
 
768
	/**
769
	 * Add a Header Item
770
	 *
771
	 * @param	string
772
	 * @param	string
773
	 * @return	CI_Email
774
	 */
775
	public function set_header($header, $value)
776
	{
777
		$this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
778
		return $this;
779
	}
780
 
781
	// --------------------------------------------------------------------
782
 
783
	/**
784
	 * Convert a String to an Array
785
	 *
786
	 * @param	string
787
	 * @return	array
788
	 */
789
	protected function _str_to_array($email)
790
	{
791
		if ( ! is_array($email))
792
		{
793
			return (strpos($email, ',') !== FALSE)
794
				? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
795
				: (array) trim($email);
796
		}
797
 
798
		return $email;
799
	}
800
 
801
	// --------------------------------------------------------------------
802
 
803
	/**
804
	 * Set Multipart Value
805
	 *
806
	 * @param	string
807
	 * @return	CI_Email
808
	 */
809
	public function set_alt_message($str)
810
	{
811
		$this->alt_message = (string) $str;
812
		return $this;
813
	}
814
 
815
	// --------------------------------------------------------------------
816
 
817
	/**
818
	 * Set Mailtype
819
	 *
820
	 * @param	string
821
	 * @return	CI_Email
822
	 */
823
	public function set_mailtype($type = 'text')
824
	{
825
		$this->mailtype = ($type === 'html') ? 'html' : 'text';
826
		return $this;
827
	}
828
 
829
	// --------------------------------------------------------------------
830
 
831
	/**
832
	 * Set Wordwrap
833
	 *
834
	 * @param	bool
835
	 * @return	CI_Email
836
	 */
837
	public function set_wordwrap($wordwrap = TRUE)
838
	{
839
		$this->wordwrap = (bool) $wordwrap;
840
		return $this;
841
	}
842
 
843
	// --------------------------------------------------------------------
844
 
845
	/**
846
	 * Set Protocol
847
	 *
848
	 * @param	string
849
	 * @return	CI_Email
850
	 */
851
	public function set_protocol($protocol = 'mail')
852
	{
853
		$this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
854
		return $this;
855
	}
856
 
857
	// --------------------------------------------------------------------
858
 
859
	/**
860
	 * Set Priority
861
	 *
862
	 * @param	int
863
	 * @return	CI_Email
864
	 */
865
	public function set_priority($n = 3)
866
	{
867
		$this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
868
		return $this;
869
	}
870
 
871
	// --------------------------------------------------------------------
872
 
873
	/**
874
	 * Set Newline Character
875
	 *
876
	 * @param	string
877
	 * @return	CI_Email
878
	 */
879
	public function set_newline($newline = "\n")
880
	{
881
		$this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
882
		return $this;
883
	}
884
 
885
	// --------------------------------------------------------------------
886
 
887
	/**
888
	 * Set CRLF
889
	 *
890
	 * @param	string
891
	 * @return	CI_Email
892
	 */
893
	public function set_crlf($crlf = "\n")
894
	{
895
		$this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
896
		return $this;
897
	}
898
 
899
	// --------------------------------------------------------------------
900
 
901
	/**
902
	 * Get the Message ID
903
	 *
904
	 * @return	string
905
	 */
906
	protected function _get_message_id()
907
	{
908
		$from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
909
		return '<'.uniqid('').strstr($from, '@').'>';
910
	}
911
 
912
	// --------------------------------------------------------------------
913
 
914
	/**
915
	 * Get Mail Protocol
916
	 *
917
	 * @param	bool
918
	 * @return	mixed
919
	 */
920
	protected function _get_protocol($return = TRUE)
921
	{
922
		$this->protocol = strtolower($this->protocol);
923
		in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
924
 
925
		if ($return === TRUE)
926
		{
927
			return $this->protocol;
928
		}
929
	}
930
 
931
	// --------------------------------------------------------------------
932
 
933
	/**
934
	 * Get Mail Encoding
935
	 *
936
	 * @param	bool
937
	 * @return	string
938
	 */
939
	protected function _get_encoding($return = TRUE)
940
	{
941
		in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
942
 
943
		foreach ($this->_base_charsets as $charset)
944
		{
945
			if (strpos($charset, $this->charset) === 0)
946
			{
947
				$this->_encoding = '7bit';
948
			}
949
		}
950
 
951
		if ($return === TRUE)
952
		{
953
			return $this->_encoding;
954
		}
955
	}
956
 
957
	// --------------------------------------------------------------------
958
 
959
	/**
960
	 * Get content type (text/html/attachment)
961
	 *
962
	 * @return	string
963
	 */
964
	protected function _get_content_type()
965
	{
966
		if ($this->mailtype === 'html')
967
		{
968
			return empty($this->_attachments) ? 'html' : 'html-attach';
969
		}
970
		elseif	($this->mailtype === 'text' && ! empty($this->_attachments))
971
		{
972
			return 'plain-attach';
973
		}
974
		else
975
		{
976
			return 'plain';
977
		}
978
	}
979
 
980
	// --------------------------------------------------------------------
981
 
982
	/**
983
	 * Set RFC 822 Date
984
	 *
985
	 * @return	string
986
	 */
987
	protected function _set_date()
988
	{
989
		$timezone = date('Z');
990
		$operator = ($timezone[0] === '-') ? '-' : '+';
991
		$timezone = abs($timezone);
992
		$timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
993
 
994
		return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
995
	}
996
 
997
	// --------------------------------------------------------------------
998
 
999
	/**
1000
	 * Mime message
1001
	 *
1002
	 * @return	string
1003
	 */
1004
	protected function _get_mime_message()
1005
	{
1006
		return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.';
1007
	}
1008
 
1009
	// --------------------------------------------------------------------
1010
 
1011
	/**
1012
	 * Validate Email Address
1013
	 *
1014
	 * @param	string
1015
	 * @return	bool
1016
	 */
1017
	public function validate_email($email)
1018
	{
1019
		if ( ! is_array($email))
1020
		{
1021
			$this->_set_error_message('lang:email_must_be_array');
1022
			return FALSE;
1023
		}
1024
 
1025
		foreach ($email as $val)
1026
		{
1027
			if ( ! $this->valid_email($val))
1028
			{
1029
				$this->_set_error_message('lang:email_invalid_address', $val);
1030
				return FALSE;
1031
			}
1032
		}
1033
 
1034
		return TRUE;
1035
	}
1036
 
1037
	// --------------------------------------------------------------------
1038
 
1039
	/**
1040
	 * Email Validation
1041
	 *
1042
	 * @param	string
1043
	 * @return	bool
1044
	 */
1045
	public function valid_email($email)
1046
	{
1047
		if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
1048
		{
1257 lars 1049
			$email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos));
68 lars 1050
		}
1051
 
1052
		return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
1053
	}
1054
 
1055
	// --------------------------------------------------------------------
1056
 
1057
	/**
1058
	 * Clean Extended Email Address: Joe Smith <joe@smith.com>
1059
	 *
1060
	 * @param	string
1061
	 * @return	string
1062
	 */
1063
	public function clean_email($email)
1064
	{
1065
		if ( ! is_array($email))
1066
		{
1067
			return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
1068
		}
1069
 
1070
		$clean_email = array();
1071
 
1072
		foreach ($email as $addy)
1073
		{
1074
			$clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
1075
		}
1076
 
1077
		return $clean_email;
1078
	}
1079
 
1080
	// --------------------------------------------------------------------
1081
 
1082
	/**
1083
	 * Build alternative plain text message
1084
	 *
1085
	 * Provides the raw message for use in plain-text headers of
1086
	 * HTML-formatted emails.
1087
	 * If the user hasn't specified his own alternative message
1088
	 * it creates one by stripping the HTML
1089
	 *
1090
	 * @return	string
1091
	 */
1092
	protected function _get_alt_message()
1093
	{
1094
		if ( ! empty($this->alt_message))
1095
		{
1096
			return ($this->wordwrap)
1097
				? $this->word_wrap($this->alt_message, 76)
1098
				: $this->alt_message;
1099
		}
1100
 
1101
		$body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
1102
		$body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
1103
 
1104
		for ($i = 20; $i >= 3; $i--)
1105
		{
1106
			$body = str_replace(str_repeat("\n", $i), "\n\n", $body);
1107
		}
1108
 
1109
		// Reduce multiple spaces
1110
		$body = preg_replace('| +|', ' ', $body);
1111
 
1112
		return ($this->wordwrap)
1113
			? $this->word_wrap($body, 76)
1114
			: $body;
1115
	}
1116
 
1117
	// --------------------------------------------------------------------
1118
 
1119
	/**
1120
	 * Word Wrap
1121
	 *
1122
	 * @param	string
1123
	 * @param	int	line-length limit
1124
	 * @return	string
1125
	 */
1126
	public function word_wrap($str, $charlim = NULL)
1127
	{
1128
		// Set the character limit, if not already present
1129
		if (empty($charlim))
1130
		{
1131
			$charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
1132
		}
1133
 
1134
		// Standardize newlines
1135
		if (strpos($str, "\r") !== FALSE)
1136
		{
1137
			$str = str_replace(array("\r\n", "\r"), "\n", $str);
1138
		}
1139
 
1140
		// Reduce multiple spaces at end of line
1141
		$str = preg_replace('| +\n|', "\n", $str);
1142
 
1143
		// If the current word is surrounded by {unwrap} tags we'll
1144
		// strip the entire chunk and replace it with a marker.
1145
		$unwrap = array();
1146
		if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
1147
		{
1148
			for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
1149
			{
1150
				$unwrap[] = $matches[1][$i];
1151
				$str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
1152
			}
1153
		}
1154
 
1155
		// Use PHP's native function to do the initial wordwrap.
1156
		// We set the cut flag to FALSE so that any individual words that are
1157
		// too long get left alone. In the next step we'll deal with them.
1158
		$str = wordwrap($str, $charlim, "\n", FALSE);
1159
 
1160
		// Split the string into individual lines of text and cycle through them
1161
		$output = '';
1162
		foreach (explode("\n", $str) as $line)
1163
		{
1164
			// Is the line within the allowed character count?
1165
			// If so we'll join it to the output and continue
1257 lars 1166
			if (self::strlen($line) <= $charlim)
68 lars 1167
			{
1168
				$output .= $line.$this->newline;
1169
				continue;
1170
			}
1171
 
1172
			$temp = '';
1173
			do
1174
			{
1175
				// If the over-length word is a URL we won't wrap it
1176
				if (preg_match('!\[url.+\]|://|www\.!', $line))
1177
				{
1178
					break;
1179
				}
1180
 
1181
				// Trim the word down
1257 lars 1182
				$temp .= self::substr($line, 0, $charlim - 1);
1183
				$line = self::substr($line, $charlim - 1);
68 lars 1184
			}
1257 lars 1185
			while (self::strlen($line) > $charlim);
68 lars 1186
 
1187
			// If $temp contains data it means we had to split up an over-length
1188
			// word into smaller chunks so we'll add it back to our current line
1189
			if ($temp !== '')
1190
			{
1191
				$output .= $temp.$this->newline;
1192
			}
1193
 
1194
			$output .= $line.$this->newline;
1195
		}
1196
 
1197
		// Put our markers back
1198
		if (count($unwrap) > 0)
1199
		{
1200
			foreach ($unwrap as $key => $val)
1201
			{
1202
				$output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
1203
			}
1204
		}
1205
 
1206
		return $output;
1207
	}
1208
 
1209
	// --------------------------------------------------------------------
1210
 
1211
	/**
1212
	 * Build final headers
1213
	 *
1257 lars 1214
	 * @return	void
68 lars 1215
	 */
1216
	protected function _build_headers()
1217
	{
1218
		$this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
1219
		$this->set_header('X-Mailer', $this->useragent);
1220
		$this->set_header('X-Priority', $this->_priorities[$this->priority]);
1221
		$this->set_header('Message-ID', $this->_get_message_id());
1222
		$this->set_header('Mime-Version', '1.0');
1223
	}
1224
 
1225
	// --------------------------------------------------------------------
1226
 
1227
	/**
1228
	 * Write Headers as a string
1229
	 *
1230
	 * @return	void
1231
	 */
1232
	protected function _write_headers()
1233
	{
1234
		if ($this->protocol === 'mail')
1235
		{
1236
			if (isset($this->_headers['Subject']))
1237
			{
1238
				$this->_subject = $this->_headers['Subject'];
1239
				unset($this->_headers['Subject']);
1240
			}
1241
		}
1242
 
1243
		reset($this->_headers);
1244
		$this->_header_str = '';
1245
 
1246
		foreach ($this->_headers as $key => $val)
1247
		{
1248
			$val = trim($val);
1249
 
1250
			if ($val !== '')
1251
			{
1252
				$this->_header_str .= $key.': '.$val.$this->newline;
1253
			}
1254
		}
1255
 
1256
		if ($this->_get_protocol() === 'mail')
1257
		{
1258
			$this->_header_str = rtrim($this->_header_str);
1259
		}
1260
	}
1261
 
1262
	// --------------------------------------------------------------------
1263
 
1264
	/**
1265
	 * Build Final Body and attachments
1266
	 *
1267
	 * @return	bool
1268
	 */
1269
	protected function _build_message()
1270
	{
1271
		if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
1272
		{
1273
			$this->_body = $this->word_wrap($this->_body);
1274
		}
1275
 
1276
		$this->_write_headers();
1277
 
1278
		$hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
1279
		$body = '';
1280
 
1281
		switch ($this->_get_content_type())
1282
		{
1283
			case 'plain':
1284
 
1285
				$hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
1286
					.'Content-Transfer-Encoding: '.$this->_get_encoding();
1287
 
1288
				if ($this->_get_protocol() === 'mail')
1289
				{
1290
					$this->_header_str .= $hdr;
1291
					$this->_finalbody = $this->_body;
1292
				}
1293
				else
1294
				{
1295
					$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
1296
				}
1297
 
1298
				return;
1299
 
1300
			case 'html':
1301
 
1302
				if ($this->send_multipart === FALSE)
1303
				{
1304
					$hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
1305
						.'Content-Transfer-Encoding: quoted-printable';
1306
				}
1307
				else
1308
				{
1309
					$boundary = uniqid('B_ALT_');
1310
					$hdr .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
1311
 
1312
					$body .= $this->_get_mime_message().$this->newline.$this->newline
1313
						.'--'.$boundary.$this->newline
1314
 
1315
						.'Content-Type: text/plain; charset='.$this->charset.$this->newline
1316
						.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1317
						.$this->_get_alt_message().$this->newline.$this->newline
1318
						.'--'.$boundary.$this->newline
1319
 
1320
						.'Content-Type: text/html; charset='.$this->charset.$this->newline
1321
						.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
1322
				}
1323
 
1324
				$this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
1325
 
1326
				if ($this->_get_protocol() === 'mail')
1327
				{
1328
					$this->_header_str .= $hdr;
1329
				}
1330
				else
1331
				{
1332
					$this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
1333
				}
1334
 
1335
				if ($this->send_multipart !== FALSE)
1336
				{
1337
					$this->_finalbody .= '--'.$boundary.'--';
1338
				}
1339
 
1340
				return;
1341
 
1342
			case 'plain-attach':
1343
 
1344
				$boundary = uniqid('B_ATC_');
1345
				$hdr .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
1346
 
1347
				if ($this->_get_protocol() === 'mail')
1348
				{
1349
					$this->_header_str .= $hdr;
1350
				}
1351
 
1352
				$body .= $this->_get_mime_message().$this->newline
1353
					.$this->newline
1354
					.'--'.$boundary.$this->newline
1355
					.'Content-Type: text/plain; charset='.$this->charset.$this->newline
1356
					.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
1357
					.$this->newline
1358
					.$this->_body.$this->newline.$this->newline;
1359
 
1360
				$this->_append_attachments($body, $boundary);
1361
 
1362
				break;
1363
			case 'html-attach':
1364
 
1365
				$alt_boundary = uniqid('B_ALT_');
1366
				$last_boundary = NULL;
1367
 
1368
				if ($this->_attachments_have_multipart('mixed'))
1369
				{
1370
					$atc_boundary = uniqid('B_ATC_');
1371
					$hdr .= 'Content-Type: multipart/mixed; boundary="'.$atc_boundary.'"';
1372
					$last_boundary = $atc_boundary;
1373
				}
1374
 
1375
				if ($this->_attachments_have_multipart('related'))
1376
				{
1377
					$rel_boundary = uniqid('B_REL_');
1378
					$rel_boundary_header = 'Content-Type: multipart/related; boundary="'.$rel_boundary.'"';
1379
 
1380
					if (isset($last_boundary))
1381
					{
1382
						$body .= '--'.$last_boundary.$this->newline.$rel_boundary_header;
1383
					}
1384
					else
1385
					{
1386
						$hdr .= $rel_boundary_header;
1387
					}
1388
 
1389
					$last_boundary = $rel_boundary;
1390
				}
1391
 
1392
				if ($this->_get_protocol() === 'mail')
1393
				{
1394
					$this->_header_str .= $hdr;
1395
				}
1396
 
1257 lars 1397
				self::strlen($body) && $body .= $this->newline.$this->newline;
68 lars 1398
				$body .= $this->_get_mime_message().$this->newline.$this->newline
1399
					.'--'.$last_boundary.$this->newline
1400
 
1401
					.'Content-Type: multipart/alternative; boundary="'.$alt_boundary.'"'.$this->newline.$this->newline
1402
					.'--'.$alt_boundary.$this->newline
1403
 
1404
					.'Content-Type: text/plain; charset='.$this->charset.$this->newline
1405
					.'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
1406
					.$this->_get_alt_message().$this->newline.$this->newline
1407
					.'--'.$alt_boundary.$this->newline
1408
 
1409
					.'Content-Type: text/html; charset='.$this->charset.$this->newline
1410
					.'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
1411
 
1412
					.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
1413
					.'--'.$alt_boundary.'--'.$this->newline.$this->newline;
1414
 
1415
				if ( ! empty($rel_boundary))
1416
				{
1417
					$body .= $this->newline.$this->newline;
1418
					$this->_append_attachments($body, $rel_boundary, 'related');
1419
				}
1420
 
1421
				// multipart/mixed attachments
1422
				if ( ! empty($atc_boundary))
1423
				{
1424
					$body .= $this->newline.$this->newline;
1425
					$this->_append_attachments($body, $atc_boundary, 'mixed');
1426
				}
1427
 
1428
				break;
1429
		}
1430
 
1431
		$this->_finalbody = ($this->_get_protocol() === 'mail')
1432
			? $body
1433
			: $hdr.$this->newline.$this->newline.$body;
1434
 
1435
		return TRUE;
1436
	}
1437
 
1438
	// --------------------------------------------------------------------
1439
 
1440
	protected function _attachments_have_multipart($type)
1441
	{
1442
		foreach ($this->_attachments as &$attachment)
1443
		{
1444
			if ($attachment['multipart'] === $type)
1445
			{
1446
				return TRUE;
1447
			}
1448
		}
1449
 
1450
		return FALSE;
1451
	}
1452
 
1453
	// --------------------------------------------------------------------
1454
 
1455
	/**
1456
	 * Prepares attachment string
1457
	 *
1458
	 * @param	string	$body		Message body to append to
1459
	 * @param	string	$boundary	Multipart boundary
1460
	 * @param	string	$multipart	When provided, only attachments of this type will be processed
1461
	 * @return	string
1462
	 */
1463
	protected function _append_attachments(&$body, $boundary, $multipart = null)
1464
	{
1465
		for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
1466
		{
1467
			if (isset($multipart) && $this->_attachments[$i]['multipart'] !== $multipart)
1468
			{
1469
				continue;
1470
			}
1471
 
1472
			$name = isset($this->_attachments[$i]['name'][1])
1473
				? $this->_attachments[$i]['name'][1]
1474
				: basename($this->_attachments[$i]['name'][0]);
1475
 
1476
			$body .= '--'.$boundary.$this->newline
1477
				.'Content-Type: '.$this->_attachments[$i]['type'].'; name="'.$name.'"'.$this->newline
1478
				.'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
1479
				.'Content-Transfer-Encoding: base64'.$this->newline
1257 lars 1480
				.(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline)
1481
				.$this->newline
68 lars 1482
				.$this->_attachments[$i]['content'].$this->newline;
1483
		}
1484
 
1485
		// $name won't be set if no attachments were appended,
1486
		// and therefore a boundary wouldn't be necessary
1487
		empty($name) OR $body .= '--'.$boundary.'--';
1488
	}
1489
 
1490
	// --------------------------------------------------------------------
1491
 
1492
	/**
1493
	 * Prep Quoted Printable
1494
	 *
1495
	 * Prepares string for Quoted-Printable Content-Transfer-Encoding
1496
	 * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
1497
	 *
1498
	 * @param	string
1499
	 * @return	string
1500
	 */
1501
	protected function _prep_quoted_printable($str)
1502
	{
1503
		// ASCII code numbers for "safe" characters that can always be
1504
		// used literally, without encoding, as described in RFC 2049.
1505
		// http://www.ietf.org/rfc/rfc2049.txt
1506
		static $ascii_safe_chars = array(
1507
			// ' (  )   +   ,   -   .   /   :   =   ?
1508
			39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
1509
			// numbers
1510
			48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
1511
			// upper-case letters
1512
			65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
1513
			// lower-case letters
1514
			97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122
1515
		);
1516
 
1517
		// We are intentionally wrapping so mail servers will encode characters
1518
		// properly and MUAs will behave, so {unwrap} must go!
1519
		$str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
1520
 
1521
		// RFC 2045 specifies CRLF as "\r\n".
1522
		// However, many developers choose to override that and violate
1523
		// the RFC rules due to (apparently) a bug in MS Exchange,
1524
		// which only works with "\n".
1525
		if ($this->crlf === "\r\n")
1526
		{
1257 lars 1527
			return quoted_printable_encode($str);
68 lars 1528
		}
1529
 
1530
		// Reduce multiple spaces & remove nulls
1531
		$str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
1532
 
1533
		// Standardize newlines
1534
		if (strpos($str, "\r") !== FALSE)
1535
		{
1536
			$str = str_replace(array("\r\n", "\r"), "\n", $str);
1537
		}
1538
 
1539
		$escape = '=';
1540
		$output = '';
1541
 
1542
		foreach (explode("\n", $str) as $line)
1543
		{
1257 lars 1544
			$length = self::strlen($line);
68 lars 1545
			$temp = '';
1546
 
1547
			// Loop through each character in the line to add soft-wrap
1548
			// characters at the end of a line " =\r\n" and add the newly
1549
			// processed line(s) to the output (see comment on $crlf class property)
1550
			for ($i = 0; $i < $length; $i++)
1551
			{
1552
				// Grab the next character
1553
				$char = $line[$i];
1554
				$ascii = ord($char);
1555
 
1556
				// Convert spaces and tabs but only if it's the end of the line
1557
				if ($ascii === 32 OR $ascii === 9)
1558
				{
1559
					if ($i === ($length - 1))
1560
					{
1561
						$char = $escape.sprintf('%02s', dechex($ascii));
1562
					}
1563
				}
1564
				// DO NOT move this below the $ascii_safe_chars line!
1565
				//
1566
				// = (equals) signs are allowed by RFC2049, but must be encoded
1567
				// as they are the encoding delimiter!
1568
				elseif ($ascii === 61)
1569
				{
1570
					$char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));  // =3D
1571
				}
1572
				elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
1573
				{
1574
					$char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
1575
				}
1576
 
1577
				// If we're at the character limit, add the line to the output,
1578
				// reset our temp variable, and keep on chuggin'
1257 lars 1579
				if ((self::strlen($temp) + self::strlen($char)) >= 76)
68 lars 1580
				{
1581
					$output .= $temp.$escape.$this->crlf;
1582
					$temp = '';
1583
				}
1584
 
1585
				// Add the character to our temporary line
1586
				$temp .= $char;
1587
			}
1588
 
1589
			// Add our completed line to the output
1590
			$output .= $temp.$this->crlf;
1591
		}
1592
 
1593
		// get rid of extra CRLF tacked onto the end
1257 lars 1594
		return self::substr($output, 0, self::strlen($this->crlf) * -1);
68 lars 1595
	}
1596
 
1597
	// --------------------------------------------------------------------
1598
 
1599
	/**
1600
	 * Prep Q Encoding
1601
	 *
1602
	 * Performs "Q Encoding" on a string for use in email headers.
1603
	 * It's related but not identical to quoted-printable, so it has its
1604
	 * own method.
1605
	 *
1606
	 * @param	string
1607
	 * @return	string
1608
	 */
1609
	protected function _prep_q_encoding($str)
1610
	{
1611
		$str = str_replace(array("\r", "\n"), '', $str);
1612
 
1613
		if ($this->charset === 'UTF-8')
1614
		{
1615
			// Note: We used to have mb_encode_mimeheader() as the first choice
1616
			//       here, but it turned out to be buggy and unreliable. DO NOT
1617
			//       re-add it! -- Narf
1618
			if (ICONV_ENABLED === TRUE)
1619
			{
1620
				$output = @iconv_mime_encode('', $str,
1621
					array(
1622
						'scheme' => 'Q',
1623
						'line-length' => 76,
1624
						'input-charset' => $this->charset,
1625
						'output-charset' => $this->charset,
1626
						'line-break-chars' => $this->crlf
1627
					)
1628
				);
1629
 
1630
				// There are reports that iconv_mime_encode() might fail and return FALSE
1631
				if ($output !== FALSE)
1632
				{
1633
					// iconv_mime_encode() will always put a header field name.
1634
					// We've passed it an empty one, but it still prepends our
1635
					// encoded string with ': ', so we need to strip it.
1257 lars 1636
					return self::substr($output, 2);
68 lars 1637
				}
1638
 
1639
				$chars = iconv_strlen($str, 'UTF-8');
1640
			}
1641
			elseif (MB_ENABLED === TRUE)
1642
			{
1643
				$chars = mb_strlen($str, 'UTF-8');
1644
			}
1645
		}
1646
 
1647
		// We might already have this set for UTF-8
1257 lars 1648
		isset($chars) OR $chars = self::strlen($str);
68 lars 1649
 
1650
		$output = '=?'.$this->charset.'?Q?';
1257 lars 1651
		for ($i = 0, $length = self::strlen($output); $i < $chars; $i++)
68 lars 1652
		{
1653
			$chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
1654
				? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
1655
				: '='.strtoupper(bin2hex($str[$i]));
1656
 
1657
			// RFC 2045 sets a limit of 76 characters per line.
1658
			// We'll append ?= to the end of each line though.
1257 lars 1659
			if ($length + ($l = self::strlen($chr)) > 74)
68 lars 1660
			{
1661
				$output .= '?='.$this->crlf // EOL
1662
					.' =?'.$this->charset.'?Q?'.$chr; // New line
1257 lars 1663
				$length = 6 + self::strlen($this->charset) + $l; // Reset the length for the new line
68 lars 1664
			}
1665
			else
1666
			{
1667
				$output .= $chr;
1668
				$length += $l;
1669
			}
1670
		}
1671
 
1672
		// End the header
1673
		return $output.'?=';
1674
	}
1675
 
1676
	// --------------------------------------------------------------------
1677
 
1678
	/**
1679
	 * Send Email
1680
	 *
1681
	 * @param	bool	$auto_clear = TRUE
1682
	 * @return	bool
1683
	 */
1684
	public function send($auto_clear = TRUE)
1685
	{
1686
		if ( ! isset($this->_headers['From']))
1687
		{
1688
			$this->_set_error_message('lang:email_no_from');
1689
			return FALSE;
1690
		}
1691
 
1692
		if ($this->_replyto_flag === FALSE)
1693
		{
1694
			$this->reply_to($this->_headers['From']);
1695
		}
1696
 
1697
		if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
1698
			&& ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
1699
			&& ! isset($this->_headers['Cc']))
1700
		{
1701
			$this->_set_error_message('lang:email_no_recipients');
1702
			return FALSE;
1703
		}
1704
 
1705
		$this->_build_headers();
1706
 
1707
		if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
1708
		{
1709
			$result = $this->batch_bcc_send();
1710
 
1711
			if ($result && $auto_clear)
1712
			{
1713
				$this->clear();
1714
			}
1715
 
1716
			return $result;
1717
		}
1718
 
1719
		if ($this->_build_message() === FALSE)
1720
		{
1721
			return FALSE;
1722
		}
1723
 
1724
		$result = $this->_spool_email();
1725
 
1726
		if ($result && $auto_clear)
1727
		{
1728
			$this->clear();
1729
		}
1730
 
1731
		return $result;
1732
	}
1733
 
1734
	// --------------------------------------------------------------------
1735
 
1736
	/**
1737
	 * Batch Bcc Send. Sends groups of BCCs in batches
1738
	 *
1739
	 * @return	void
1740
	 */
1741
	public function batch_bcc_send()
1742
	{
1743
		$float = $this->bcc_batch_size - 1;
1744
		$set = '';
1745
		$chunk = array();
1746
 
1747
		for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
1748
		{
1749
			if (isset($this->_bcc_array[$i]))
1750
			{
1751
				$set .= ', '.$this->_bcc_array[$i];
1752
			}
1753
 
1754
			if ($i === $float)
1755
			{
1257 lars 1756
				$chunk[] = self::substr($set, 1);
68 lars 1757
				$float += $this->bcc_batch_size;
1758
				$set = '';
1759
			}
1760
 
1761
			if ($i === $c-1)
1762
			{
1257 lars 1763
				$chunk[] = self::substr($set, 1);
68 lars 1764
			}
1765
		}
1766
 
1767
		for ($i = 0, $c = count($chunk); $i < $c; $i++)
1768
		{
1769
			unset($this->_headers['Bcc']);
1770
 
1771
			$bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
1772
 
1773
			if ($this->protocol !== 'smtp')
1774
			{
1775
				$this->set_header('Bcc', implode(', ', $bcc));
1776
			}
1777
			else
1778
			{
1779
				$this->_bcc_array = $bcc;
1780
			}
1781
 
1782
			if ($this->_build_message() === FALSE)
1783
			{
1784
				return FALSE;
1785
			}
1786
 
1787
			$this->_spool_email();
1788
		}
1789
	}
1790
 
1791
	// --------------------------------------------------------------------
1792
 
1793
	/**
1794
	 * Unwrap special elements
1795
	 *
1796
	 * @return	void
1797
	 */
1798
	protected function _unwrap_specials()
1799
	{
1800
		$this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
1801
	}
1802
 
1803
	// --------------------------------------------------------------------
1804
 
1805
	/**
1806
	 * Strip line-breaks via callback
1807
	 *
1808
	 * @param	string	$matches
1809
	 * @return	string
1810
	 */
1811
	protected function _remove_nl_callback($matches)
1812
	{
1813
		if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
1814
		{
1815
			$matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
1816
		}
1817
 
1818
		return $matches[1];
1819
	}
1820
 
1821
	// --------------------------------------------------------------------
1822
 
1823
	/**
1824
	 * Spool mail to the mail server
1825
	 *
1826
	 * @return	bool
1827
	 */
1828
	protected function _spool_email()
1829
	{
1830
		$this->_unwrap_specials();
1831
 
1832
		$method = '_send_with_'.$this->_get_protocol();
1833
		if ( ! $this->$method())
1834
		{
1835
			$this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
1836
			return FALSE;
1837
		}
1838
 
1839
		$this->_set_error_message('lang:email_sent', $this->_get_protocol());
1840
		return TRUE;
1841
	}
1842
 
1843
	// --------------------------------------------------------------------
1844
 
1845
	/**
1846
	 * Send using mail()
1847
	 *
1848
	 * @return	bool
1849
	 */
1850
	protected function _send_with_mail()
1851
	{
1852
		if (is_array($this->_recipients))
1853
		{
1854
			$this->_recipients = implode(', ', $this->_recipients);
1855
		}
1856
 
1857
		if ($this->_safe_mode === TRUE)
1858
		{
1859
			return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
1860
		}
1861
		else
1862
		{
1863
			// most documentation of sendmail using the "-f" flag lacks a space after it, however
1864
			// we've encountered servers that seem to require it to be in place.
1865
			return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$this->clean_email($this->_headers['Return-Path']));
1866
		}
1867
	}
1868
 
1869
	// --------------------------------------------------------------------
1870
 
1871
	/**
1872
	 * Send using Sendmail
1873
	 *
1874
	 * @return	bool
1875
	 */
1876
	protected function _send_with_sendmail()
1877
	{
1878
		// is popen() enabled?
1879
		if ( ! function_usable('popen')
1880
			OR FALSE === ($fp = @popen(
1881
						$this->mailpath.' -oi -f '.$this->clean_email($this->_headers['From']).' -t'
1882
						, 'w'))
1883
		) // server probably has popen disabled, so nothing we can do to get a verbose error.
1884
		{
1885
			return FALSE;
1886
		}
1887
 
1888
		fputs($fp, $this->_header_str);
1889
		fputs($fp, $this->_finalbody);
1890
 
1891
		$status = pclose($fp);
1892
 
1893
		if ($status !== 0)
1894
		{
1895
			$this->_set_error_message('lang:email_exit_status', $status);
1896
			$this->_set_error_message('lang:email_no_socket');
1897
			return FALSE;
1898
		}
1899
 
1900
		return TRUE;
1901
	}
1902
 
1903
	// --------------------------------------------------------------------
1904
 
1905
	/**
1906
	 * Send using SMTP
1907
	 *
1908
	 * @return	bool
1909
	 */
1910
	protected function _send_with_smtp()
1911
	{
1912
		if ($this->smtp_host === '')
1913
		{
1914
			$this->_set_error_message('lang:email_no_hostname');
1915
			return FALSE;
1916
		}
1917
 
1918
		if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
1919
		{
1920
			return FALSE;
1921
		}
1922
 
1923
		if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
1924
		{
1925
			$this->_smtp_end();
1926
			return FALSE;
1927
		}
1928
 
1929
		foreach ($this->_recipients as $val)
1930
		{
1931
			if ( ! $this->_send_command('to', $val))
1932
			{
1933
				$this->_smtp_end();
1934
				return FALSE;
1935
			}
1936
		}
1937
 
1938
		if (count($this->_cc_array) > 0)
1939
		{
1940
			foreach ($this->_cc_array as $val)
1941
			{
1942
				if ($val !== '' && ! $this->_send_command('to', $val))
1943
				{
1944
					$this->_smtp_end();
1945
					return FALSE;
1946
				}
1947
			}
1948
		}
1949
 
1950
		if (count($this->_bcc_array) > 0)
1951
		{
1952
			foreach ($this->_bcc_array as $val)
1953
			{
1954
				if ($val !== '' && ! $this->_send_command('to', $val))
1955
				{
1956
					$this->_smtp_end();
1957
					return FALSE;
1958
				}
1959
			}
1960
		}
1961
 
1962
		if ( ! $this->_send_command('data'))
1963
		{
1964
			$this->_smtp_end();
1965
			return FALSE;
1966
		}
1967
 
1968
		// perform dot transformation on any lines that begin with a dot
1969
		$this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
1970
 
1971
		$this->_send_data('.');
1972
 
1973
		$reply = $this->_get_smtp_data();
1974
		$this->_set_error_message($reply);
1975
 
1976
		$this->_smtp_end();
1977
 
1978
		if (strpos($reply, '250') !== 0)
1979
		{
1980
			$this->_set_error_message('lang:email_smtp_error', $reply);
1981
			return FALSE;
1982
		}
1983
 
1984
		return TRUE;
1985
	}
1986
 
1987
	// --------------------------------------------------------------------
1988
 
1989
	/**
1990
	 * SMTP End
1991
	 *
1992
	 * Shortcut to send RSET or QUIT depending on keep-alive
1993
	 *
1994
	 * @return	void
1995
	 */
1996
	protected function _smtp_end()
1997
	{
1998
		($this->smtp_keepalive)
1999
			? $this->_send_command('reset')
2000
			: $this->_send_command('quit');
2001
	}
2002
 
2003
	// --------------------------------------------------------------------
2004
 
2005
	/**
2006
	 * SMTP Connect
2007
	 *
2008
	 * @return	string
2009
	 */
2010
	protected function _smtp_connect()
2011
	{
2012
		if (is_resource($this->_smtp_connect))
2013
		{
2014
			return TRUE;
2015
		}
2016
 
2017
		$ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
2018
 
2019
		$this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
2020
							$this->smtp_port,
2021
							$errno,
2022
							$errstr,
2023
							$this->smtp_timeout);
2024
 
2025
		if ( ! is_resource($this->_smtp_connect))
2026
		{
2027
			$this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
2028
			return FALSE;
2029
		}
2030
 
2031
		stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
2032
		$this->_set_error_message($this->_get_smtp_data());
2033
 
2034
		if ($this->smtp_crypto === 'tls')
2035
		{
2036
			$this->_send_command('hello');
2037
			$this->_send_command('starttls');
2038
 
2039
			$crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
2040
 
2041
			if ($crypto !== TRUE)
2042
			{
2043
				$this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
2044
				return FALSE;
2045
			}
2046
		}
2047
 
2048
		return $this->_send_command('hello');
2049
	}
2050
 
2051
	// --------------------------------------------------------------------
2052
 
2053
	/**
2054
	 * Send SMTP command
2055
	 *
2056
	 * @param	string
2057
	 * @param	string
1257 lars 2058
	 * @return	bool
68 lars 2059
	 */
2060
	protected function _send_command($cmd, $data = '')
2061
	{
2062
		switch ($cmd)
2063
		{
2064
			case 'hello' :
2065
 
2066
						if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
2067
						{
2068
							$this->_send_data('EHLO '.$this->_get_hostname());
2069
						}
2070
						else
2071
						{
2072
							$this->_send_data('HELO '.$this->_get_hostname());
2073
						}
2074
 
2075
						$resp = 250;
2076
			break;
2077
			case 'starttls'	:
2078
 
2079
						$this->_send_data('STARTTLS');
2080
						$resp = 220;
2081
			break;
2082
			case 'from' :
2083
 
2084
						$this->_send_data('MAIL FROM:<'.$data.'>');
2085
						$resp = 250;
2086
			break;
2087
			case 'to' :
2088
 
2089
						if ($this->dsn)
2090
						{
2091
							$this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
2092
						}
2093
						else
2094
						{
2095
							$this->_send_data('RCPT TO:<'.$data.'>');
2096
						}
2097
 
2098
						$resp = 250;
2099
			break;
2100
			case 'data'	:
2101
 
2102
						$this->_send_data('DATA');
2103
						$resp = 354;
2104
			break;
2105
			case 'reset':
2106
 
2107
						$this->_send_data('RSET');
2108
						$resp = 250;
2109
			break;
2110
			case 'quit'	:
2111
 
2112
						$this->_send_data('QUIT');
2113
						$resp = 221;
2114
			break;
2115
		}
2116
 
2117
		$reply = $this->_get_smtp_data();
2118
 
2119
		$this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
2120
 
1257 lars 2121
		if ((int) self::substr($reply, 0, 3) !== $resp)
68 lars 2122
		{
2123
			$this->_set_error_message('lang:email_smtp_error', $reply);
2124
			return FALSE;
2125
		}
2126
 
2127
		if ($cmd === 'quit')
2128
		{
2129
			fclose($this->_smtp_connect);
2130
		}
2131
 
2132
		return TRUE;
2133
	}
2134
 
2135
	// --------------------------------------------------------------------
2136
 
2137
	/**
2138
	 * SMTP Authenticate
2139
	 *
2140
	 * @return	bool
2141
	 */
2142
	protected function _smtp_authenticate()
2143
	{
2144
		if ( ! $this->_smtp_auth)
2145
		{
2146
			return TRUE;
2147
		}
2148
 
2149
		if ($this->smtp_user === '' && $this->smtp_pass === '')
2150
		{
2151
			$this->_set_error_message('lang:email_no_smtp_unpw');
2152
			return FALSE;
2153
		}
2154
 
2155
		$this->_send_data('AUTH LOGIN');
2156
 
2157
		$reply = $this->_get_smtp_data();
2158
 
2159
		if (strpos($reply, '503') === 0)	// Already authenticated
2160
		{
2161
			return TRUE;
2162
		}
2163
		elseif (strpos($reply, '334') !== 0)
2164
		{
2165
			$this->_set_error_message('lang:email_failed_smtp_login', $reply);
2166
			return FALSE;
2167
		}
2168
 
2169
		$this->_send_data(base64_encode($this->smtp_user));
2170
 
2171
		$reply = $this->_get_smtp_data();
2172
 
2173
		if (strpos($reply, '334') !== 0)
2174
		{
2175
			$this->_set_error_message('lang:email_smtp_auth_un', $reply);
2176
			return FALSE;
2177
		}
2178
 
2179
		$this->_send_data(base64_encode($this->smtp_pass));
2180
 
2181
		$reply = $this->_get_smtp_data();
2182
 
2183
		if (strpos($reply, '235') !== 0)
2184
		{
2185
			$this->_set_error_message('lang:email_smtp_auth_pw', $reply);
2186
			return FALSE;
2187
		}
2188
 
2189
		if ($this->smtp_keepalive)
2190
		{
2191
			$this->_smtp_auth = FALSE;
2192
		}
2193
 
2194
		return TRUE;
2195
	}
2196
 
2197
	// --------------------------------------------------------------------
2198
 
2199
	/**
2200
	 * Send SMTP data
2201
	 *
2202
	 * @param	string	$data
2203
	 * @return	bool
2204
	 */
2205
	protected function _send_data($data)
2206
	{
2207
		$data .= $this->newline;
1257 lars 2208
		for ($written = $timestamp = 0, $length = self::strlen($data); $written < $length; $written += $result)
68 lars 2209
		{
1257 lars 2210
			if (($result = fwrite($this->_smtp_connect, self::substr($data, $written))) === FALSE)
68 lars 2211
			{
2212
				break;
2213
			}
2214
			// See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
2215
			elseif ($result === 0)
2216
			{
2217
				if ($timestamp === 0)
2218
				{
2219
					$timestamp = time();
2220
				}
2221
				elseif ($timestamp < (time() - $this->smtp_timeout))
2222
				{
2223
					$result = FALSE;
2224
					break;
2225
				}
2226
 
2227
				usleep(250000);
2228
				continue;
2229
			}
2230
			else
2231
			{
2232
				$timestamp = 0;
2233
			}
2234
		}
2235
 
2236
		if ($result === FALSE)
2237
		{
2238
			$this->_set_error_message('lang:email_smtp_data_failure', $data);
2239
			return FALSE;
2240
		}
2241
 
2242
		return TRUE;
2243
	}
2244
 
2245
	// --------------------------------------------------------------------
2246
 
2247
	/**
2248
	 * Get SMTP data
2249
	 *
2250
	 * @return	string
2251
	 */
2252
	protected function _get_smtp_data()
2253
	{
2254
		$data = '';
2255
 
2256
		while ($str = fgets($this->_smtp_connect, 512))
2257
		{
2258
			$data .= $str;
2259
 
2260
			if ($str[3] === ' ')
2261
			{
2262
				break;
2263
			}
2264
		}
2265
 
2266
		return $data;
2267
	}
2268
 
2269
	// --------------------------------------------------------------------
2270
 
2271
	/**
2272
	 * Get Hostname
2273
	 *
2274
	 * There are only two legal types of hostname - either a fully
2275
	 * qualified domain name (eg: "mail.example.com") or an IP literal
2276
	 * (eg: "[1.2.3.4]").
2277
	 *
2278
	 * @link	https://tools.ietf.org/html/rfc5321#section-2.3.5
2279
	 * @link	http://cbl.abuseat.org/namingproblems.html
2280
	 * @return	string
2281
	 */
2282
	protected function _get_hostname()
2283
	{
2284
		if (isset($_SERVER['SERVER_NAME']))
2285
		{
2286
			return $_SERVER['SERVER_NAME'];
2287
		}
2288
 
2289
		return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
2290
	}
2291
 
2292
	// --------------------------------------------------------------------
2293
 
2294
	/**
2295
	 * Get Debug Message
2296
	 *
2297
	 * @param	array	$include	List of raw data chunks to include in the output
2298
	 *					Valid options are: 'headers', 'subject', 'body'
2299
	 * @return	string
2300
	 */
2301
	public function print_debugger($include = array('headers', 'subject', 'body'))
2302
	{
2303
		$msg = '';
2304
 
2305
		if (count($this->_debug_msg) > 0)
2306
		{
2307
			foreach ($this->_debug_msg as $val)
2308
			{
2309
				$msg .= $val;
2310
			}
2311
		}
2312
 
2313
		// Determine which parts of our raw data needs to be printed
2314
		$raw_data = '';
2315
		is_array($include) OR $include = array($include);
2316
 
2317
		if (in_array('headers', $include, TRUE))
2318
		{
2319
			$raw_data = htmlspecialchars($this->_header_str)."\n";
2320
		}
2321
 
2322
		if (in_array('subject', $include, TRUE))
2323
		{
2324
			$raw_data .= htmlspecialchars($this->_subject)."\n";
2325
		}
2326
 
2327
		if (in_array('body', $include, TRUE))
2328
		{
2329
			$raw_data .= htmlspecialchars($this->_finalbody);
2330
		}
2331
 
2332
		return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
2333
	}
2334
 
2335
	// --------------------------------------------------------------------
2336
 
2337
	/**
2338
	 * Set Message
2339
	 *
2340
	 * @param	string	$msg
2341
	 * @param	string	$val = ''
2342
	 * @return	void
2343
	 */
2344
	protected function _set_error_message($msg, $val = '')
2345
	{
2346
		$CI =& get_instance();
2347
		$CI->lang->load('email');
2348
 
2349
		if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
2350
		{
2351
			$this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
2352
		}
2353
		else
2354
		{
2355
			$this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
2356
		}
2357
	}
2358
 
2359
	// --------------------------------------------------------------------
2360
 
2361
	/**
2362
	 * Mime Types
2363
	 *
2364
	 * @param	string
2365
	 * @return	string
2366
	 */
2367
	protected function _mime_types($ext = '')
2368
	{
2369
		$ext = strtolower($ext);
2370
 
2371
		$mimes =& get_mimes();
2372
 
2373
		if (isset($mimes[$ext]))
2374
		{
2375
			return is_array($mimes[$ext])
2376
				? current($mimes[$ext])
2377
				: $mimes[$ext];
2378
		}
2379
 
2380
		return 'application/x-unknown-content-type';
2381
	}
2382
 
2383
	// --------------------------------------------------------------------
2384
 
2385
	/**
2386
	 * Destructor
2387
	 *
2388
	 * @return	void
2389
	 */
2390
	public function __destruct()
2391
	{
2392
		is_resource($this->_smtp_connect) && $this->_send_command('quit');
2393
	}
1257 lars 2394
 
2395
	// --------------------------------------------------------------------
2396
 
2397
	/**
2398
	 * Byte-safe strlen()
2399
	 *
2400
	 * @param	string	$str
2401
	 * @return	int
2402
	 */
2403
	protected static function strlen($str)
2404
	{
2405
		return (self::$func_override)
2406
			? mb_strlen($str, '8bit')
2407
			: strlen($str);
2408
	}
2409
 
2410
	// --------------------------------------------------------------------
2411
 
2412
	/**
2413
	 * Byte-safe substr()
2414
	 *
2415
	 * @param	string	$str
2416
	 * @param	int	$start
2417
	 * @param	int	$length
2418
	 * @return	string
2419
	 */
2420
	protected static function substr($str, $start, $length = NULL)
2421
	{
2422
		if (self::$func_override)
2423
		{
2424
			// mb_substr($str, $start, null, '8bit') returns an empty
2425
			// string on PHP 5.3
2426
			isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
2427
			return mb_substr($str, $start, $length, '8bit');
2428
		}
2429
 
2430
		return isset($length)
2431
			? substr($str, $start, $length)
2432
			: substr($str, $start);
2433
	}
68 lars 2434
}