about summary refs log tree commit diff stats
path: root/central/trunk/class.phpmailer.php
diff options
context:
space:
mode:
Diffstat (limited to 'central/trunk/class.phpmailer.php')
-rw-r--r--central/trunk/class.phpmailer.php1897
1 files changed, 1897 insertions, 0 deletions
diff --git a/central/trunk/class.phpmailer.php b/central/trunk/class.phpmailer.php new file mode 100644 index 0000000..51a50e7 --- /dev/null +++ b/central/trunk/class.phpmailer.php
@@ -0,0 +1,1897 @@
1<?php
2/*~ class.phpmailer.php
3.---------------------------------------------------------------------------.
4| Software: PHPMailer - PHP email class |
5| Version: 2.2.1 |
6| Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
7| Info: http://phpmailer.sourceforge.net |
8| Support: http://sourceforge.net/projects/phpmailer/ |
9| ------------------------------------------------------------------------- |
10| Author: Andy Prevost (project admininistrator) |
11| Author: Brent R. Matzelle (original founder) |
12| Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
13| Copyright (c) 2001-2003, Brent R. Matzelle |
14| ------------------------------------------------------------------------- |
15| License: Distributed under the Lesser General Public License (LGPL) |
16| http://www.gnu.org/copyleft/lesser.html |
17| This program is distributed in the hope that it will be useful - WITHOUT |
18| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
19| FITNESS FOR A PARTICULAR PURPOSE. |
20| ------------------------------------------------------------------------- |
21| We offer a number of paid services (www.codeworxtech.com): |
22| - Web Hosting on highly optimized fast and secure servers |
23| - Technology Consulting |
24| - Oursourcing (highly qualified programmers and graphic designers) |
25'---------------------------------------------------------------------------'
26
27/**
28 * PHPMailer - PHP email transport class
29 * NOTE: Designed for use with PHP version 5 and up
30 * @package PHPMailer
31 * @author Andy Prevost
32 * @copyright 2004 - 2008 Andy Prevost
33 */
34
35class PHPMailer {
36
37 /////////////////////////////////////////////////
38 // PROPERTIES, PUBLIC
39 /////////////////////////////////////////////////
40
41 /**
42 * Email priority (1 = High, 3 = Normal, 5 = low).
43 * @var int
44 */
45 public $Priority = 3;
46
47 /**
48 * Sets the CharSet of the message.
49 * @var string
50 */
51 public $CharSet = 'iso-8859-1';
52
53 /**
54 * Sets the Content-type of the message.
55 * @var string
56 */
57 public $ContentType = 'text/plain';
58
59 /**
60 * Sets the Encoding of the message. Options for this are "8bit",
61 * "7bit", "binary", "base64", and "quoted-printable".
62 * @var string
63 */
64 public $Encoding = '8bit';
65
66 /**
67 * Holds the most recent mailer error message.
68 * @var string
69 */
70 public $ErrorInfo = '';
71
72 /**
73 * Sets the From email address for the message.
74 * @var string
75 */
76 public $From = 'root@localhost';
77
78 /**
79 * Sets the From name of the message.
80 * @var string
81 */
82 public $FromName = 'Root User';
83
84 /**
85 * Sets the Sender email (Return-Path) of the message. If not empty,
86 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
87 * @var string
88 */
89 public $Sender = '';
90
91 /**
92 * Sets the Subject of the message.
93 * @var string
94 */
95 public $Subject = '';
96
97 /**
98 * Sets the Body of the message. This can be either an HTML or text body.
99 * If HTML then run IsHTML(true).
100 * @var string
101 */
102 public $Body = '';
103
104 /**
105 * Sets the text-only body of the message. This automatically sets the
106 * email to multipart/alternative. This body can be read by mail
107 * clients that do not have HTML email capability such as mutt. Clients
108 * that can read HTML will view the normal Body.
109 * @var string
110 */
111 public $AltBody = '';
112
113 /**
114 * Sets word wrapping on the body of the message to a given number of
115 * characters.
116 * @var int
117 */
118 public $WordWrap = 0;
119
120 /**
121 * Method to send mail: ("mail", "sendmail", or "smtp").
122 * @var string
123 */
124 public $Mailer = 'mail';
125
126 /**
127 * Sets the path of the sendmail program.
128 * @var string
129 */
130 public $Sendmail = '/usr/sbin/sendmail';
131
132 /**
133 * Path to PHPMailer plugins. This is now only useful if the SMTP class
134 * is in a different directory than the PHP include path.
135 * @var string
136 */
137 public $PluginDir = '';
138
139 /**
140 * Holds PHPMailer version.
141 * @var string
142 */
143 public $Version = "2.2";
144
145 /**
146 * Sets the email address that a reading confirmation will be sent.
147 * @var string
148 */
149 public $ConfirmReadingTo = '';
150
151 /**
152 * Sets the hostname to use in Message-Id and Received headers
153 * and as default HELO string. If empty, the value returned
154 * by SERVER_NAME is used or 'localhost.localdomain'.
155 * @var string
156 */
157 public $Hostname = '';
158
159 /**
160 * Sets the message ID to be used in the Message-Id header.
161 * If empty, a unique id will be generated.
162 * @var string
163 */
164 public $MessageID = '';
165
166 /////////////////////////////////////////////////
167 // PROPERTIES FOR SMTP
168 /////////////////////////////////////////////////
169
170 /**
171 * Sets the SMTP hosts. All hosts must be separated by a
172 * semicolon. You can also specify a different port
173 * for each host by using this format: [hostname:port]
174 * (e.g. "smtp1.example.com:25;smtp2.example.com").
175 * Hosts will be tried in order.
176 * @var string
177 */
178 public $Host = 'localhost';
179
180 /**
181 * Sets the default SMTP server port.
182 * @var int
183 */
184 public $Port = 25;
185
186 /**
187 * Sets the SMTP HELO of the message (Default is $Hostname).
188 * @var string
189 */
190 public $Helo = '';
191
192 /**
193 * Sets connection prefix.
194 * Options are "", "ssl" or "tls"
195 * @var string
196 */
197 public $SMTPSecure = "";
198
199 /**
200 * Sets SMTP authentication. Utilizes the Username and Password variables.
201 * @var bool
202 */
203 public $SMTPAuth = false;
204
205 /**
206 * Sets SMTP username.
207 * @var string
208 */
209 public $Username = '';
210
211 /**
212 * Sets SMTP password.
213 * @var string
214 */
215 public $Password = '';
216
217 /**
218 * Sets the SMTP server timeout in seconds. This function will not
219 * work with the win32 version.
220 * @var int
221 */
222 public $Timeout = 10;
223
224 /**
225 * Sets SMTP class debugging on or off.
226 * @var bool
227 */
228 public $SMTPDebug = false;
229
230 /**
231 * Prevents the SMTP connection from being closed after each mail
232 * sending. If this is set to true then to close the connection
233 * requires an explicit call to SmtpClose().
234 * @var bool
235 */
236 public $SMTPKeepAlive = false;
237
238 /**
239 * Provides the ability to have the TO field process individual
240 * emails, instead of sending to entire TO addresses
241 * @var bool
242 */
243 public $SingleTo = false;
244
245 /////////////////////////////////////////////////
246 // PROPERTIES, PRIVATE
247 /////////////////////////////////////////////////
248
249 private $smtp = NULL;
250 private $to = array();
251 private $cc = array();
252 private $bcc = array();
253 private $ReplyTo = array();
254 private $attachment = array();
255 private $CustomHeader = array();
256 private $message_type = '';
257 private $boundary = array();
258 private $language = array();
259 private $error_count = 0;
260 private $LE = "\n";
261 private $sign_cert_file = "";
262 private $sign_key_file = "";
263 private $sign_key_pass = "";
264
265 /////////////////////////////////////////////////
266 // METHODS, VARIABLES
267 /////////////////////////////////////////////////
268
269 /**
270 * Sets message type to HTML.
271 * @param bool $bool
272 * @return void
273 */
274 public function IsHTML($bool) {
275 if($bool == true) {
276 $this->ContentType = 'text/html';
277 } else {
278 $this->ContentType = 'text/plain';
279 }
280 }
281
282 /**
283 * Sets Mailer to send message using SMTP.
284 * @return void
285 */
286 public function IsSMTP() {
287 $this->Mailer = 'smtp';
288 }
289
290 /**
291 * Sets Mailer to send message using PHP mail() function.
292 * @return void
293 */
294 public function IsMail() {
295 $this->Mailer = 'mail';
296 }
297
298 /**
299 * Sets Mailer to send message using the $Sendmail program.
300 * @return void
301 */
302 public function IsSendmail() {
303 $this->Mailer = 'sendmail';
304 }
305
306 /**
307 * Sets Mailer to send message using the qmail MTA.
308 * @return void
309 */
310 public function IsQmail() {
311 $this->Sendmail = '/var/qmail/bin/sendmail';
312 $this->Mailer = 'sendmail';
313 }
314
315 /////////////////////////////////////////////////
316 // METHODS, RECIPIENTS
317 /////////////////////////////////////////////////
318
319 /**
320 * Adds a "To" address.
321 * @param string $address
322 * @param string $name
323 * @return void
324 */
325 public function AddAddress($address, $name = '') {
326 $cur = count($this->to);
327 $this->to[$cur][0] = trim($address);
328 $this->to[$cur][1] = $name;
329 }
330
331 /**
332 * Adds a "Cc" address. Note: this function works
333 * with the SMTP mailer on win32, not with the "mail"
334 * mailer.
335 * @param string $address
336 * @param string $name
337 * @return void
338 */
339 public function AddCC($address, $name = '') {
340 $cur = count($this->cc);
341 $this->cc[$cur][0] = trim($address);
342 $this->cc[$cur][1] = $name;
343 }
344
345 /**
346 * Adds a "Bcc" address. Note: this function works
347 * with the SMTP mailer on win32, not with the "mail"
348 * mailer.
349 * @param string $address
350 * @param string $name
351 * @return void
352 */
353 public function AddBCC($address, $name = '') {
354 $cur = count($this->bcc);
355 $this->bcc[$cur][0] = trim($address);
356 $this->bcc[$cur][1] = $name;
357 }
358
359 /**
360 * Adds a "Reply-to" address.
361 * @param string $address
362 * @param string $name
363 * @return void
364 */
365 public function AddReplyTo($address, $name = '') {
366 $cur = count($this->ReplyTo);
367 $this->ReplyTo[$cur][0] = trim($address);
368 $this->ReplyTo[$cur][1] = $name;
369 }
370
371 /////////////////////////////////////////////////
372 // METHODS, MAIL SENDING
373 /////////////////////////////////////////////////
374
375 /**
376 * Creates message and assigns Mailer. If the message is
377 * not sent successfully then it returns false. Use the ErrorInfo
378 * variable to view description of the error.
379 * @return bool
380 */
381 public function Send() {
382 $header = '';
383 $body = '';
384 $result = true;
385
386 if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
387 $this->SetError($this->Lang('provide_address'));
388 return false;
389 }
390
391 /* Set whether the message is multipart/alternative */
392 if(!empty($this->AltBody)) {
393 $this->ContentType = 'multipart/alternative';
394 }
395
396 $this->error_count = 0; // reset errors
397 $this->SetMessageType();
398 $header .= $this->CreateHeader();
399 $body = $this->CreateBody();
400
401 if($body == '') {
402 return false;
403 }
404
405 /* Choose the mailer */
406 switch($this->Mailer) {
407 case 'sendmail':
408 $result = $this->SendmailSend($header, $body);
409 break;
410 case 'smtp':
411 $result = $this->SmtpSend($header, $body);
412 break;
413 case 'mail':
414 $result = $this->MailSend($header, $body);
415 break;
416 default:
417 $result = $this->MailSend($header, $body);
418 break;
419 //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
420 //$result = false;
421 //break;
422 }
423
424 return $result;
425 }
426
427 /**
428 * Sends mail using the $Sendmail program.
429 * @access public
430 * @return bool
431 */
432 public function SendmailSend($header, $body) {
433 if ($this->Sender != '') {
434 $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
435 } else {
436 $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
437 }
438
439 if(!@$mail = popen($sendmail, 'w')) {
440 $this->SetError($this->Lang('execute') . $this->Sendmail);
441 return false;
442 }
443
444 fputs($mail, $header);
445 fputs($mail, $body);
446
447 $result = pclose($mail);
448 if (version_compare(phpversion(), '4.2.3') == -1) {
449 $result = $result >> 8 & 0xFF;
450 }
451 if($result != 0) {
452 $this->SetError($this->Lang('execute') . $this->Sendmail);
453 return false;
454 }
455
456 return true;
457 }
458
459 /**
460 * Sends mail using the PHP mail() function.
461 * @access public
462 * @return bool
463 */
464 public function MailSend($header, $body) {
465
466 $to = '';
467 for($i = 0; $i < count($this->to); $i++) {
468 if($i != 0) { $to .= ', '; }
469 $to .= $this->AddrFormat($this->to[$i]);
470 }
471
472 $toArr = split(',', $to);
473
474 $params = sprintf("-oi -f %s", $this->Sender);
475 if ($this->Sender != '' && strlen(ini_get('safe_mode'))< 1) {
476 $old_from = ini_get('sendmail_from');
477 ini_set('sendmail_from', $this->Sender);
478 if ($this->SingleTo === true && count($toArr) > 1) {
479 foreach ($toArr as $key => $val) {
480 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
481 }
482 } else {
483 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
484 }
485 } else {
486 if ($this->SingleTo === true && count($toArr) > 1) {
487 foreach ($toArr as $key => $val) {
488 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
489 }
490 } else {
491 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
492 }
493 }
494
495 if (isset($old_from)) {
496 ini_set('sendmail_from', $old_from);
497 }
498
499 if(!$rt) {
500 $this->SetError($this->Lang('instantiate'));
501 return false;
502 }
503
504 return true;
505 }
506
507 /**
508 * Sends mail via SMTP using PhpSMTP (Author:
509 * Chris Ryan). Returns bool. Returns false if there is a
510 * bad MAIL FROM, RCPT, or DATA input.
511 * @access public
512 * @return bool
513 */
514 public function SmtpSend($header, $body) {
515 include_once($this->PluginDir . 'class.smtp.php');
516 $error = '';
517 $bad_rcpt = array();
518
519 if(!$this->SmtpConnect()) {
520 return false;
521 }
522
523 $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
524 if(!$this->smtp->Mail($smtp_from)) {
525 $error = $this->Lang('from_failed') . $smtp_from;
526 $this->SetError($error);
527 $this->smtp->Reset();
528 return false;
529 }
530
531 /* Attempt to send attach all recipients */
532 for($i = 0; $i < count($this->to); $i++) {
533 if(!$this->smtp->Recipient($this->to[$i][0])) {
534 $bad_rcpt[] = $this->to[$i][0];
535 }
536 }
537 for($i = 0; $i < count($this->cc); $i++) {
538 if(!$this->smtp->Recipient($this->cc[$i][0])) {
539 $bad_rcpt[] = $this->cc[$i][0];
540 }
541 }
542 for($i = 0; $i < count($this->bcc); $i++) {
543 if(!$this->smtp->Recipient($this->bcc[$i][0])) {
544 $bad_rcpt[] = $this->bcc[$i][0];
545 }
546 }
547
548 if(count($bad_rcpt) > 0) { // Create error message
549 for($i = 0; $i < count($bad_rcpt); $i++) {
550 if($i != 0) {
551 $error .= ', ';
552 }
553 $error .= $bad_rcpt[$i];
554 }
555 $error = $this->Lang('recipients_failed') . $error;
556 $this->SetError($error);
557 $this->smtp->Reset();
558 return false;
559 }
560
561 if(!$this->smtp->Data($header . $body)) {
562 $this->SetError($this->Lang('data_not_accepted'));
563 $this->smtp->Reset();
564 return false;
565 }
566 if($this->SMTPKeepAlive == true) {
567 $this->smtp->Reset();
568 } else {
569 $this->SmtpClose();
570 }
571
572 return true;
573 }
574
575 /**
576 * Initiates a connection to an SMTP server. Returns false if the
577 * operation failed.
578 * @access public
579 * @return bool
580 */
581 public function SmtpConnect() {
582 if($this->smtp == NULL) {
583 $this->smtp = new SMTP();
584 }
585
586 $this->smtp->do_debug = $this->SMTPDebug;
587 $hosts = explode(';', $this->Host);
588 $index = 0;
589 $connection = ($this->smtp->Connected());
590
591 /* Retry while there is no connection */
592 while($index < count($hosts) && $connection == false) {
593 $hostinfo = array();
594 if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {
595 $host = $hostinfo[1];
596 $port = $hostinfo[2];
597 } else {
598 $host = $hosts[$index];
599 $port = $this->Port;
600 }
601
602 $tls = ($this->SMTPSecure == 'tls');
603 $ssl = ($this->SMTPSecure == 'ssl');
604
605 if($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
606
607 $hello = ($this->Helo != '' ? $this->Hello : $this->ServerHostname());
608 $this->smtp->Hello($hello);
609
610 if($tls) {
611 if(!$this->smtp->StartTLS()) {
612 $this->SetError($this->Lang("tls"));
613 $this->smtp->Reset();
614 $connection = false;
615 }
616
617 //We must resend HELLO after tls negociation
618 $this->smtp->Hello($hello);
619 }
620
621 $connection = true;
622 if($this->SMTPAuth) {
623 if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
624 $this->SetError($this->Lang('authenticate'));
625 $this->smtp->Reset();
626 $connection = false;
627 }
628 }
629 }
630 $index++;
631 }
632 if(!$connection) {
633 $this->SetError($this->Lang('connect_host'));
634 }
635
636 return $connection;
637 }
638
639 /**
640 * Closes the active SMTP session if one exists.
641 * @return void
642 */
643 public function SmtpClose() {
644 if($this->smtp != NULL) {
645 if($this->smtp->Connected()) {
646 $this->smtp->Quit();
647 $this->smtp->Close();
648 }
649 }
650 }
651
652 /**
653 * Sets the language for all class error messages. Returns false
654 * if it cannot load the language file. The default language type
655 * is English.
656 * @param string $lang_type Type of language (e.g. Portuguese: "br")
657 * @param string $lang_path Path to the language file directory
658 * @access public
659 * @return bool
660 */
661 function SetLanguage($lang_type = 'en', $lang_path = 'language/') {
662 if( !(@include $lang_path.'phpmailer.lang-'.$lang_type.'.php') ) {
663 $this->SetError('Could not load language file');
664 return false;
665 }
666 $this->language = $PHPMAILER_LANG;
667 return true;
668 }
669
670 /////////////////////////////////////////////////
671 // METHODS, MESSAGE CREATION
672 /////////////////////////////////////////////////
673
674 /**
675 * Creates recipient headers.
676 * @access public
677 * @return string
678 */
679 public function AddrAppend($type, $addr) {
680 $addr_str = $type . ': ';
681 $addr_str .= $this->AddrFormat($addr[0]);
682 if(count($addr) > 1) {
683 for($i = 1; $i < count($addr); $i++) {
684 $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
685 }
686 }
687 $addr_str .= $this->LE;
688
689 return $addr_str;
690 }
691
692 /**
693 * Formats an address correctly.
694 * @access public
695 * @return string
696 */
697 public function AddrFormat($addr) {
698 if(empty($addr[1])) {
699 $formatted = $this->SecureHeader($addr[0]);
700 } else {
701 $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
702 }
703
704 return $formatted;
705 }
706
707 /**
708 * Wraps message for use with mailers that do not
709 * automatically perform wrapping and for quoted-printable.
710 * Original written by philippe.
711 * @access public
712 * @return string
713 */
714 public function WrapText($message, $length, $qp_mode = false) {
715 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
716 // If utf-8 encoding is used, we will need to make sure we don't
717 // split multibyte characters when we wrap
718 $is_utf8 = (strtolower($this->CharSet) == "utf-8");
719
720 $message = $this->FixEOL($message);
721 if (substr($message, -1) == $this->LE) {
722 $message = substr($message, 0, -1);
723 }
724
725 $line = explode($this->LE, $message);
726 $message = '';
727 for ($i=0 ;$i < count($line); $i++) {
728 $line_part = explode(' ', $line[$i]);
729 $buf = '';
730 for ($e = 0; $e<count($line_part); $e++) {
731 $word = $line_part[$e];
732 if ($qp_mode and (strlen($word) > $length)) {
733 $space_left = $length - strlen($buf) - 1;
734 if ($e != 0) {
735 if ($space_left > 20) {
736 $len = $space_left;
737 if ($is_utf8) {
738 $len = $this->UTF8CharBoundary($word, $len);
739 } elseif (substr($word, $len - 1, 1) == "=") {
740 $len--;
741 } elseif (substr($word, $len - 2, 1) == "=") {
742 $len -= 2;
743 }
744 $part = substr($word, 0, $len);
745 $word = substr($word, $len);
746 $buf .= ' ' . $part;
747 $message .= $buf . sprintf("=%s", $this->LE);
748 } else {
749 $message .= $buf . $soft_break;
750 }
751 $buf = '';
752 }
753 while (strlen($word) > 0) {
754 $len = $length;
755 if ($is_utf8) {
756 $len = $this->UTF8CharBoundary($word, $len);
757 } elseif (substr($word, $len - 1, 1) == "=") {
758 $len--;
759 } elseif (substr($word, $len - 2, 1) == "=") {
760 $len -= 2;
761 }
762 $part = substr($word, 0, $len);
763 $word = substr($word, $len);
764
765 if (strlen($word) > 0) {
766 $message .= $part . sprintf("=%s", $this->LE);
767 } else {
768 $buf = $part;
769 }
770 }
771 } else {
772 $buf_o = $buf;
773 $buf .= ($e == 0) ? $word : (' ' . $word);
774
775 if (strlen($buf) > $length and $buf_o != '') {
776 $message .= $buf_o . $soft_break;
777 $buf = $word;
778 }
779 }
780 }
781 $message .= $buf . $this->LE;
782 }
783
784 return $message;
785 }
786
787 /**
788 * Finds last character boundary prior to maxLength in a utf-8
789 * quoted (printable) encoded string.
790 * Original written by Colin Brown.
791 * @access public
792 * @param string $encodedText utf-8 QP text
793 * @param int $maxLength find last character boundary prior to this length
794 * @return int
795 */
796 public function UTF8CharBoundary($encodedText, $maxLength) {
797 $foundSplitPos = false;
798 $lookBack = 3;
799 while (!$foundSplitPos) {
800 $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
801 $encodedCharPos = strpos($lastChunk, "=");
802 if ($encodedCharPos !== false) {
803 // Found start of encoded character byte within $lookBack block.
804 // Check the encoded byte value (the 2 chars after the '=')
805 $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
806 $dec = hexdec($hex);
807 if ($dec < 128) { // Single byte character.
808 // If the encoded char was found at pos 0, it will fit
809 // otherwise reduce maxLength to start of the encoded char
810 $maxLength = ($encodedCharPos == 0) ? $maxLength :
811 $maxLength - ($lookBack - $encodedCharPos);
812 $foundSplitPos = true;
813 } elseif ($dec >= 192) { // First byte of a multi byte character
814 // Reduce maxLength to split at start of character
815 $maxLength = $maxLength - ($lookBack - $encodedCharPos);
816 $foundSplitPos = true;
817 } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
818 $lookBack += 3;
819 }
820 } else {
821 // No encoded character found
822 $foundSplitPos = true;
823 }
824 }
825 return $maxLength;
826 }
827
828
829 /**
830 * Set the body wrapping.
831 * @access public
832 * @return void
833 */
834 public function SetWordWrap() {
835 if($this->WordWrap < 1) {
836 return;
837 }
838
839 switch($this->message_type) {
840 case 'alt':
841 /* fall through */
842 case 'alt_attachments':
843 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
844 break;
845 default:
846 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
847 break;
848 }
849 }
850
851 /**
852 * Assembles message header.
853 * @access public
854 * @return string
855 */
856 public function CreateHeader() {
857 $result = '';
858
859 /* Set the boundaries */
860 $uniq_id = md5(uniqid(time()));
861 $this->boundary[1] = 'b1_' . $uniq_id;
862 $this->boundary[2] = 'b2_' . $uniq_id;
863
864 $result .= $this->HeaderLine('Date', $this->RFCDate());
865 if($this->Sender == '') {
866 $result .= $this->HeaderLine('Return-Path', trim($this->From));
867 } else {
868 $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
869 }
870
871 /* To be created automatically by mail() */
872 if($this->Mailer != 'mail') {
873 if(count($this->to) > 0) {
874 $result .= $this->AddrAppend('To', $this->to);
875 } elseif (count($this->cc) == 0) {
876 $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
877 }
878 if(count($this->cc) > 0) {
879 $result .= $this->AddrAppend('Cc', $this->cc);
880 }
881 }
882
883 $from = array();
884 $from[0][0] = trim($this->From);
885 $from[0][1] = $this->FromName;
886 $result .= $this->AddrAppend('From', $from);
887
888 /* sendmail and mail() extract Cc from the header before sending */
889 if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
890 $result .= $this->AddrAppend('Cc', $this->cc);
891 }
892
893 /* sendmail and mail() extract Bcc from the header before sending */
894 if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
895 $result .= $this->AddrAppend('Bcc', $this->bcc);
896 }
897
898 if(count($this->ReplyTo) > 0) {
899 $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
900 }
901
902 /* mail() sets the subject itself */
903 if($this->Mailer != 'mail') {
904 $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
905 }
906
907 if($this->MessageID != '') {
908 $result .= $this->HeaderLine('Message-ID',$this->MessageID);
909 } else {
910 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
911 }
912 $result .= $this->HeaderLine('X-Priority', $this->Priority);
913 $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.codeworxtech.com) [version ' . $this->Version . ']');
914
915 if($this->ConfirmReadingTo != '') {
916 $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
917 }
918
919 // Add custom headers
920 for($index = 0; $index < count($this->CustomHeader); $index++) {
921 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
922 }
923 if (!$this->sign_key_file) {
924 $result .= $this->HeaderLine('MIME-Version', '1.0');
925 $result .= $this->GetMailMIME();
926 }
927
928 return $result;
929 }
930
931 /**
932 * Returns the message MIME.
933 * @access public
934 * @return string
935 */
936 public function GetMailMIME() {
937 $result = '';
938 switch($this->message_type) {
939 case 'plain':
940 $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
941 $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
942 break;
943 case 'attachments':
944 /* fall through */
945 case 'alt_attachments':
946 if($this->InlineImageExists()){
947 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
948 } else {
949 $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
950 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
951 }
952 break;
953 case 'alt':
954 $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
955 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
956 break;
957 }
958
959 if($this->Mailer != 'mail') {
960 $result .= $this->LE.$this->LE;
961 }
962
963 return $result;
964 }
965
966 /**
967 * Assembles the message body. Returns an empty string on failure.
968 * @access public
969 * @return string
970 */
971 public function CreateBody() {
972 $result = '';
973
974 if ($this->sign_key_file) {
975 $result .= $this->GetMailMIME();
976 }
977
978 $this->SetWordWrap();
979
980 switch($this->message_type) {
981 case 'alt':
982 $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
983 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
984 $result .= $this->LE.$this->LE;
985 $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
986 $result .= $this->EncodeString($this->Body, $this->Encoding);
987 $result .= $this->LE.$this->LE;
988 $result .= $this->EndBoundary($this->boundary[1]);
989 break;
990 case 'plain':
991 $result .= $this->EncodeString($this->Body, $this->Encoding);
992 break;
993 case 'attachments':
994 $result .= $this->GetBoundary($this->boundary[1], '', '', '');
995 $result .= $this->EncodeString($this->Body, $this->Encoding);
996 $result .= $this->LE;
997 $result .= $this->AttachAll();
998 break;
999 case 'alt_attachments':
1000 $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
1001 $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
1002 $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
1003 $result .= $this->EncodeString($this->AltBody, $this->Encoding);
1004 $result .= $this->LE.$this->LE;
1005 $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
1006 $result .= $this->EncodeString($this->Body, $this->Encoding);
1007 $result .= $this->LE.$this->LE;
1008 $result .= $this->EndBoundary($this->boundary[2]);
1009 $result .= $this->AttachAll();
1010 break;
1011 }
1012
1013 if($this->IsError()) {
1014 $result = '';
1015 } else if ($this->sign_key_file) {
1016 $file = tempnam("", "mail");
1017 $fp = fopen($file, "w");
1018 fwrite($fp, $result);
1019 fclose($fp);
1020 $signed = tempnam("", "signed");
1021
1022 if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
1023 $fp = fopen($signed, "r");
1024 $result = '';
1025 while(!feof($fp)){
1026 $result = $result . fread($fp, 1024);
1027 }
1028 fclose($fp);
1029 } else {
1030 $this->SetError($this->Lang("signing").openssl_error_string());
1031 $result = '';
1032 }
1033
1034 unlink($file);
1035 unlink($signed);
1036 }
1037
1038 return $result;
1039 }
1040
1041 /**
1042 * Returns the start of a message boundary.
1043 * @access public
1044 */
1045 public function GetBoundary($boundary, $charSet, $contentType, $encoding) {
1046 $result = '';
1047 if($charSet == '') {
1048 $charSet = $this->CharSet;
1049 }
1050 if($contentType == '') {
1051 $contentType = $this->ContentType;
1052 }
1053 if($encoding == '') {
1054 $encoding = $this->Encoding;
1055 }
1056 $result .= $this->TextLine('--' . $boundary);
1057 $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
1058 $result .= $this->LE;
1059 $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
1060 $result .= $this->LE;
1061
1062 return $result;
1063 }
1064
1065 /**
1066 * Returns the end of a message boundary.
1067 * @access public
1068 */
1069 public function EndBoundary($boundary) {
1070 return $this->LE . '--' . $boundary . '--' . $this->LE;
1071 }
1072
1073 /**
1074 * Sets the message type.
1075 * @access public
1076 * @return void
1077 */
1078 public function SetMessageType() {
1079 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
1080 $this->message_type = 'plain';
1081 } else {
1082 if(count($this->attachment) > 0) {
1083 $this->message_type = 'attachments';
1084 }
1085 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
1086 $this->message_type = 'alt';
1087 }
1088 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
1089 $this->message_type = 'alt_attachments';
1090 }
1091 }
1092 }
1093
1094 /* Returns a formatted header line.
1095 * @access public
1096 * @return string
1097 */
1098 public function HeaderLine($name, $value) {
1099 return $name . ': ' . $value . $this->LE;
1100 }
1101
1102 /**
1103 * Returns a formatted mail line.
1104 * @access public
1105 * @return string
1106 */
1107 public function TextLine($value) {
1108 return $value . $this->LE;
1109 }
1110
1111 /////////////////////////////////////////////////
1112 // CLASS METHODS, ATTACHMENTS
1113 /////////////////////////////////////////////////
1114
1115 /**
1116 * Adds an attachment from a path on the filesystem.
1117 * Returns false if the file could not be found
1118 * or accessed.
1119 * @param string $path Path to the attachment.
1120 * @param string $name Overrides the attachment name.
1121 * @param string $encoding File encoding (see $Encoding).
1122 * @param string $type File extension (MIME) type.
1123 * @return bool
1124 */
1125 public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
1126 if(!@is_file($path)) {
1127 $this->SetError($this->Lang('file_access') . $path);
1128 return false;
1129 }
1130
1131 $filename = basename($path);
1132 if($name == '') {
1133 $name = $filename;
1134 }
1135
1136 $cur = count($this->attachment);
1137 $this->attachment[$cur][0] = $path;
1138 $this->attachment[$cur][1] = $filename;
1139 $this->attachment[$cur][2] = $name;
1140 $this->attachment[$cur][3] = $encoding;
1141 $this->attachment[$cur][4] = $type;
1142 $this->attachment[$cur][5] = false; // isStringAttachment
1143 $this->attachment[$cur][6] = 'attachment';
1144 $this->attachment[$cur][7] = 0;
1145
1146 return true;
1147 }
1148
1149 /**
1150 * Attaches all fs, string, and binary attachments to the message.
1151 * Returns an empty string on failure.
1152 * @access public
1153 * @return string
1154 */
1155 public function AttachAll() {
1156 /* Return text of body */
1157 $mime = array();
1158
1159 /* Add all attachments */
1160 for($i = 0; $i < count($this->attachment); $i++) {
1161 /* Check for string attachment */
1162 $bString = $this->attachment[$i][5];
1163 if ($bString) {
1164 $string = $this->attachment[$i][0];
1165 } else {
1166 $path = $this->attachment[$i][0];
1167 }
1168
1169 $filename = $this->attachment[$i][1];
1170 $name = $this->attachment[$i][2];
1171 $encoding = $this->attachment[$i][3];
1172 $type = $this->attachment[$i][4];
1173 $disposition = $this->attachment[$i][6];
1174 $cid = $this->attachment[$i][7];
1175
1176 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
1177 //$mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
1178 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
1179 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
1180
1181 if($disposition == 'inline') {
1182 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
1183 }
1184
1185 //$mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
1186 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
1187
1188 /* Encode as string attachment */
1189 if($bString) {
1190 $mime[] = $this->EncodeString($string, $encoding);
1191 if($this->IsError()) {
1192 return '';
1193 }
1194 $mime[] = $this->LE.$this->LE;
1195 } else {
1196 $mime[] = $this->EncodeFile($path, $encoding);
1197 if($this->IsError()) {
1198 return '';
1199 }
1200 $mime[] = $this->LE.$this->LE;
1201 }
1202 }
1203
1204 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
1205
1206 return join('', $mime);
1207 }
1208
1209 /**
1210 * Encodes attachment in requested format. Returns an
1211 * empty string on failure.
1212 * @access public
1213 * @return string
1214 */
1215 public function EncodeFile ($path, $encoding = 'base64') {
1216 if(!@$fd = fopen($path, 'rb')) {
1217 $this->SetError($this->Lang('file_open') . $path);
1218 return '';
1219 }
1220 if (function_exists('get_magic_quotes')) {
1221 function get_magic_quotes() {
1222 return false;
1223 }
1224}
1225 if (PHP_VERSION < 6) {
1226 $magic_quotes = get_magic_quotes_runtime();
1227 set_magic_quotes_runtime(0);
1228 }
1229 $file_buffer = file_get_contents($path);
1230 $file_buffer = $this->EncodeString($file_buffer, $encoding);
1231 fclose($fd);
1232 if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
1233 return $file_buffer;
1234 }
1235
1236 /**
1237 * Encodes string to requested format. Returns an
1238 * empty string on failure.
1239 * @access public
1240 * @return string
1241 */
1242 public function EncodeString ($str, $encoding = 'base64') {
1243 $encoded = '';
1244 switch(strtolower($encoding)) {
1245 case 'base64':
1246 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
1247 break;
1248 case '7bit':
1249 case '8bit':
1250 $encoded = $this->FixEOL($str);
1251 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
1252 $encoded .= $this->LE;
1253 break;
1254 case 'binary':
1255 $encoded = $str;
1256 break;
1257 case 'quoted-printable':
1258 $encoded = $this->EncodeQP($str);
1259 break;
1260 default:
1261 $this->SetError($this->Lang('encoding') . $encoding);
1262 break;
1263 }
1264 return $encoded;
1265 }
1266
1267 /**
1268 * Encode a header string to best of Q, B, quoted or none.
1269 * @access public
1270 * @return string
1271 */
1272 public function EncodeHeader ($str, $position = 'text') {
1273 $x = 0;
1274
1275 switch (strtolower($position)) {
1276 case 'phrase':
1277 if (!preg_match('/[\200-\377]/', $str)) {
1278 /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
1279 $encoded = addcslashes($str, "\0..\37\177\\\"");
1280 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
1281 return ($encoded);
1282 } else {
1283 return ("\"$encoded\"");
1284 }
1285 }
1286 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
1287 break;
1288 case 'comment':
1289 $x = preg_match_all('/[()"]/', $str, $matches);
1290 /* Fall-through */
1291 case 'text':
1292 default:
1293 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
1294 break;
1295 }
1296
1297 if ($x == 0) {
1298 return ($str);
1299 }
1300
1301 $maxlen = 75 - 7 - strlen($this->CharSet);
1302 /* Try to select the encoding which should produce the shortest output */
1303 if (strlen($str)/3 < $x) {
1304 $encoding = 'B';
1305 if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
1306 // Use a custom function which correctly encodes and wraps long
1307 // multibyte strings without breaking lines within a character
1308 $encoded = $this->Base64EncodeWrapMB($str);
1309 } else {
1310 $encoded = base64_encode($str);
1311 $maxlen -= $maxlen % 4;
1312 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
1313 }
1314 } else {
1315 $encoding = 'Q';
1316 $encoded = $this->EncodeQ($str, $position);
1317 $encoded = $this->WrapText($encoded, $maxlen, true);
1318 $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
1319 }
1320
1321 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
1322 $encoded = trim(str_replace("\n", $this->LE, $encoded));
1323
1324 return $encoded;
1325 }
1326
1327 /**
1328 * Checks if a string contains multibyte characters.
1329 * @access public
1330 * @param string $str multi-byte text to wrap encode
1331 * @return bool
1332 */
1333 public function HasMultiBytes($str) {
1334 if (function_exists('mb_strlen')) {
1335 return (strlen($str) > mb_strlen($str, $this->CharSet));
1336 } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
1337 return False;
1338 }
1339 }
1340
1341 /**
1342 * Correctly encodes and wraps long multibyte strings for mail headers
1343 * without breaking lines within a character.
1344 * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
1345 * @access public
1346 * @param string $str multi-byte text to wrap encode
1347 * @return string
1348 */
1349 public function Base64EncodeWrapMB($str) {
1350 $start = "=?".$this->CharSet."?B?";
1351 $end = "?=";
1352 $encoded = "";
1353
1354 $mb_length = mb_strlen($str, $this->CharSet);
1355 // Each line must have length <= 75, including $start and $end
1356 $length = 75 - strlen($start) - strlen($end);
1357 // Average multi-byte ratio
1358 $ratio = $mb_length / strlen($str);
1359 // Base64 has a 4:3 ratio
1360 $offset = $avgLength = floor($length * $ratio * .75);
1361
1362 for ($i = 0; $i < $mb_length; $i += $offset) {
1363 $lookBack = 0;
1364
1365 do {
1366 $offset = $avgLength - $lookBack;
1367 $chunk = mb_substr($str, $i, $offset, $this->CharSet);
1368 $chunk = base64_encode($chunk);
1369 $lookBack++;
1370 }
1371 while (strlen($chunk) > $length);
1372
1373 $encoded .= $chunk . $this->LE;
1374 }
1375
1376 // Chomp the last linefeed
1377 $encoded = substr($encoded, 0, -strlen($this->LE));
1378 return $encoded;
1379 }
1380
1381 /**
1382 * Encode string to quoted-printable.
1383 * @access public
1384 * @param string $string the text to encode
1385 * @param integer $line_max Number of chars allowed on a line before wrapping
1386 * @return string
1387 */
1388 public function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
1389 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
1390 $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
1391 $eol = "\r\n";
1392 $escape = '=';
1393 $output = '';
1394 while( list(, $line) = each($lines) ) {
1395 $linlen = strlen($line);
1396 $newline = '';
1397 for($i = 0; $i < $linlen; $i++) {
1398 $c = substr( $line, $i, 1 );
1399 $dec = ord( $c );
1400 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
1401 $c = '=2E';
1402 }
1403 if ( $dec == 32 ) {
1404 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
1405 $c = '=20';
1406 } else if ( $space_conv ) {
1407 $c = '=20';
1408 }
1409 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
1410 $h2 = floor($dec/16);
1411 $h1 = floor($dec%16);
1412 $c = $escape.$hex[$h2].$hex[$h1];
1413 }
1414 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
1415 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
1416 $newline = '';
1417 // check if newline first character will be point or not
1418 if ( $dec == 46 ) {
1419 $c = '=2E';
1420 }
1421 }
1422 $newline .= $c;
1423 } // end of for
1424 $output .= $newline.$eol;
1425 } // end of while
1426 return trim($output);
1427 }
1428
1429 /**
1430 * Encode string to q encoding.
1431 * @access public
1432 * @return string
1433 */
1434 public function EncodeQ ($str, $position = 'text') {
1435 /* There should not be any EOL in the string */
1436 $encoded = preg_replace("[\r\n]", '', $str);
1437
1438 switch (strtolower($position)) {
1439 case 'phrase':
1440 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1441 break;
1442 case 'comment':
1443 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
1444 case 'text':
1445 default:
1446 /* Replace every high ascii, control =, ? and _ characters */
1447 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
1448 "'='.sprintf('%02X', ord('\\1'))", $encoded);
1449 break;
1450 }
1451
1452 /* Replace every spaces to _ (more readable than =20) */
1453 $encoded = str_replace(' ', '_', $encoded);
1454
1455 return $encoded;
1456 }
1457
1458 /**
1459 * Adds a string or binary attachment (non-filesystem) to the list.
1460 * This method can be used to attach ascii or binary data,
1461 * such as a BLOB record from a database.
1462 * @param string $string String attachment data.
1463 * @param string $filename Name of the attachment.
1464 * @param string $encoding File encoding (see $Encoding).
1465 * @param string $type File extension (MIME) type.
1466 * @return void
1467 */
1468 public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
1469 /* Append to $attachment array */
1470 $cur = count($this->attachment);
1471 $this->attachment[$cur][0] = $string;
1472 $this->attachment[$cur][1] = $filename;
1473 $this->attachment[$cur][2] = $filename;
1474 $this->attachment[$cur][3] = $encoding;
1475 $this->attachment[$cur][4] = $type;
1476 $this->attachment[$cur][5] = true; // isString
1477 $this->attachment[$cur][6] = 'attachment';
1478 $this->attachment[$cur][7] = 0;
1479 }
1480
1481 /**
1482 * Adds an embedded attachment. This can include images, sounds, and
1483 * just about any other document. Make sure to set the $type to an
1484 * image type. For JPEG images use "image/jpeg" and for GIF images
1485 * use "image/gif".
1486 * @param string $path Path to the attachment.
1487 * @param string $cid Content ID of the attachment. Use this to identify
1488 * the Id for accessing the image in an HTML form.
1489 * @param string $name Overrides the attachment name.
1490 * @param string $encoding File encoding (see $Encoding).
1491 * @param string $type File extension (MIME) type.
1492 * @return bool
1493 */
1494 public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
1495
1496 if(!@is_file($path)) {
1497 $this->SetError($this->Lang('file_access') . $path);
1498 return false;
1499 }
1500
1501 $filename = basename($path);
1502 if($name == '') {
1503 $name = $filename;
1504 }
1505
1506 /* Append to $attachment array */
1507 $cur = count($this->attachment);
1508 $this->attachment[$cur][0] = $path;
1509 $this->attachment[$cur][1] = $filename;
1510 $this->attachment[$cur][2] = $name;
1511 $this->attachment[$cur][3] = $encoding;
1512 $this->attachment[$cur][4] = $type;
1513 $this->attachment[$cur][5] = false;
1514 $this->attachment[$cur][6] = 'inline';
1515 $this->attachment[$cur][7] = $cid;
1516
1517 return true;
1518 }
1519
1520 /**
1521 * Returns true if an inline attachment is present.
1522 * @access public
1523 * @return bool
1524 */
1525 public function InlineImageExists() {
1526 $result = false;
1527 for($i = 0; $i < count($this->attachment); $i++) {
1528 if($this->attachment[$i][6] == 'inline') {
1529 $result = true;
1530 break;
1531 }
1532 }
1533
1534 return $result;
1535 }
1536
1537 /////////////////////////////////////////////////
1538 // CLASS METHODS, MESSAGE RESET
1539 /////////////////////////////////////////////////
1540
1541 /**
1542 * Clears all recipients assigned in the TO array. Returns void.
1543 * @return void
1544 */
1545 public function ClearAddresses() {
1546 $this->to = array();
1547 }
1548
1549 /**
1550 * Clears all recipients assigned in the CC array. Returns void.
1551 * @return void
1552 */
1553 public function ClearCCs() {
1554 $this->cc = array();
1555 }
1556
1557 /**
1558 * Clears all recipients assigned in the BCC array. Returns void.
1559 * @return void
1560 */
1561 public function ClearBCCs() {
1562 $this->bcc = array();
1563 }
1564
1565 /**
1566 * Clears all recipients assigned in the ReplyTo array. Returns void.
1567 * @return void
1568 */
1569 public function ClearReplyTos() {
1570 $this->ReplyTo = array();
1571 }
1572
1573 /**
1574 * Clears all recipients assigned in the TO, CC and BCC
1575 * array. Returns void.
1576 * @return void
1577 */
1578 public function ClearAllRecipients() {
1579 $this->to = array();
1580 $this->cc = array();
1581 $this->bcc = array();
1582 }
1583
1584 /**
1585 * Clears all previously set filesystem, string, and binary
1586 * attachments. Returns void.
1587 * @return void
1588 */
1589 public function ClearAttachments() {
1590 $this->attachment = array();
1591 }
1592
1593 /**
1594 * Clears all custom headers. Returns void.
1595 * @return void
1596 */
1597 public function ClearCustomHeaders() {
1598 $this->CustomHeader = array();
1599 }
1600
1601 /////////////////////////////////////////////////
1602 // CLASS METHODS, MISCELLANEOUS
1603 /////////////////////////////////////////////////
1604
1605 /**
1606 * Adds the error message to the error container.
1607 * Returns void.
1608 * @access private
1609 * @return void
1610 */
1611 private function SetError($msg) {
1612 $this->error_count++;
1613 $this->ErrorInfo = $msg;
1614 }
1615
1616 /**
1617 * Returns the proper RFC 822 formatted date.
1618 * @access private
1619 * @return string
1620 */
1621 private static function RFCDate() {
1622 $tz = date('Z');
1623 $tzs = ($tz < 0) ? '-' : '+';
1624 $tz = abs($tz);
1625 $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
1626 $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
1627
1628 return $result;
1629 }
1630
1631 /**
1632 * Returns the server hostname or 'localhost.localdomain' if unknown.
1633 * @access private
1634 * @return string
1635 */
1636 private function ServerHostname() {
1637 if (!empty($this->Hostname)) {
1638 $result = $this->Hostname;
1639 } elseif (isset($_SERVER['SERVER_NAME'])) {
1640 $result = $_SERVER['SERVER_NAME'];
1641 } else {
1642 $result = "localhost.localdomain";
1643 }
1644
1645 return $result;
1646 }
1647
1648 /**
1649 * Returns a message in the appropriate language.
1650 * @access private
1651 * @return string
1652 */
1653 private function Lang($key) {
1654 if(count($this->language) < 1) {
1655 $this->SetLanguage('en'); // set the default language
1656 }
1657
1658 if(isset($this->language[$key])) {
1659 return $this->language[$key];
1660 } else {
1661 return 'Language string failed to load: ' . $key;
1662 }
1663 }
1664
1665 /**
1666 * Returns true if an error occurred.
1667 * @access public
1668 * @return bool
1669 */
1670 public function IsError() {
1671 return ($this->error_count > 0);
1672 }
1673
1674 /**
1675 * Changes every end of line from CR or LF to CRLF.
1676 * @access private
1677 * @return string
1678 */
1679 private function FixEOL($str) {
1680 $str = str_replace("\r\n", "\n", $str);
1681 $str = str_replace("\r", "\n", $str);
1682 $str = str_replace("\n", $this->LE, $str);
1683 return $str;
1684 }
1685
1686 /**
1687 * Adds a custom header.
1688 * @access public
1689 * @return void
1690 */
1691 public function AddCustomHeader($custom_header) {
1692 $this->CustomHeader[] = explode(':', $custom_header, 2);
1693 }
1694
1695 /**
1696 * Evaluates the message and returns modifications for inline images and backgrounds
1697 * @access public
1698 * @return $message
1699 */
1700 public function MsgHTML($message,$basedir='') {
1701 preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
1702 if(isset($images[2])) {
1703 foreach($images[2] as $i => $url) {
1704 // do not change urls for absolute images (thanks to corvuscorax)
1705 if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
1706 $filename = basename($url);
1707 $directory = dirname($url);
1708 ($directory == '.')?$directory='':'';
1709 $cid = 'cid:' . md5($filename);
1710 $fileParts = split("\.", $filename);
1711 $ext = $fileParts[1];
1712 $mimeType = $this->_mime_types($ext);
1713 if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
1714 if ( strlen($directory) > 1 && substr($basedir,-1) != '/') { $directory .= '/'; }
1715 $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType);
1716 if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
1717 $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
1718 }
1719 }
1720 }
1721 }
1722 $this->IsHTML(true);
1723 $this->Body = $message;
1724 $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
1725 if ( !empty($textMsg) && empty($this->AltBody) ) {
1726 $this->AltBody = $textMsg;
1727 }
1728 if ( empty($this->AltBody) ) {
1729 $this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";
1730 }
1731 }
1732
1733 /**
1734 * Gets the mime type of the embedded or inline image
1735 * @access public
1736 * @return mime type of ext
1737 */
1738 public function _mime_types($ext = '') {
1739 $mimes = array(
1740 'hqx' => 'application/mac-binhex40',
1741 'cpt' => 'application/mac-compactpro',
1742 'doc' => 'application/msword',
1743 'bin' => 'application/macbinary',
1744 'dms' => 'application/octet-stream',
1745 'lha' => 'application/octet-stream',
1746 'lzh' => 'application/octet-stream',
1747 'exe' => 'application/octet-stream',
1748 'class' => 'application/octet-stream',
1749 'psd' => 'application/octet-stream',
1750 'so' => 'application/octet-stream',
1751 'sea' => 'application/octet-stream',
1752 'dll' => 'application/octet-stream',
1753 'oda' => 'application/oda',
1754 'pdf' => 'application/pdf',
1755 'ai' => 'application/postscript',
1756 'eps' => 'application/postscript',
1757 'ps' => 'application/postscript',
1758 'smi' => 'application/smil',
1759 'smil' => 'application/smil',
1760 'mif' => 'application/vnd.mif',
1761 'xls' => 'application/vnd.ms-excel',
1762 'ppt' => 'application/vnd.ms-powerpoint',
1763 'wbxml' => 'application/vnd.wap.wbxml',
1764 'wmlc' => 'application/vnd.wap.wmlc',
1765 'dcr' => 'application/x-director',
1766 'dir' => 'application/x-director',
1767 'dxr' => 'application/x-director',
1768 'dvi' => 'application/x-dvi',
1769 'gtar' => 'application/x-gtar',
1770 'php' => 'application/x-httpd-php',
1771 'php4' => 'application/x-httpd-php',
1772 'php3' => 'application/x-httpd-php',
1773 'phtml' => 'application/x-httpd-php',
1774 'phps' => 'application/x-httpd-php-source',
1775 'js' => 'application/x-javascript',
1776 'swf' => 'application/x-shockwave-flash',
1777 'sit' => 'application/x-stuffit',
1778 'tar' => 'application/x-tar',
1779 'tgz' => 'application/x-tar',
1780 'xhtml' => 'application/xhtml+xml',
1781 'xht' => 'application/xhtml+xml',
1782 'zip' => 'application/zip',
1783 'mid' => 'audio/midi',
1784 'midi' => 'audio/midi',
1785 'mpga' => 'audio/mpeg',
1786 'mp2' => 'audio/mpeg',
1787 'mp3' => 'audio/mpeg',
1788 'aif' => 'audio/x-aiff',
1789 'aiff' => 'audio/x-aiff',
1790 'aifc' => 'audio/x-aiff',
1791 'ram' => 'audio/x-pn-realaudio',
1792 'rm' => 'audio/x-pn-realaudio',
1793 'rpm' => 'audio/x-pn-realaudio-plugin',
1794 'ra' => 'audio/x-realaudio',
1795 'rv' => 'video/vnd.rn-realvideo',
1796 'wav' => 'audio/x-wav',
1797 'bmp' => 'image/bmp',
1798 'gif' => 'image/gif',
1799 'jpeg' => 'image/jpeg',
1800 'jpg' => 'image/jpeg',
1801 'jpe' => 'image/jpeg',
1802 'png' => 'image/png',
1803 'tiff' => 'image/tiff',
1804 'tif' => 'image/tiff',
1805 'css' => 'text/css',
1806 'html' => 'text/html',
1807 'htm' => 'text/html',
1808 'shtml' => 'text/html',
1809 'txt' => 'text/plain',
1810 'text' => 'text/plain',
1811 'log' => 'text/plain',
1812 'rtx' => 'text/richtext',
1813 'rtf' => 'text/rtf',
1814 'xml' => 'text/xml',
1815 'xsl' => 'text/xml',
1816 'mpeg' => 'video/mpeg',
1817 'mpg' => 'video/mpeg',
1818 'mpe' => 'video/mpeg',
1819 'qt' => 'video/quicktime',
1820 'mov' => 'video/quicktime',
1821 'avi' => 'video/x-msvideo',
1822 'movie' => 'video/x-sgi-movie',
1823 'doc' => 'application/msword',
1824 'word' => 'application/msword',
1825 'xl' => 'application/excel',
1826 'eml' => 'message/rfc822'
1827 );
1828 return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
1829 }
1830
1831 /**
1832 * Set (or reset) Class Objects (variables)
1833 *
1834 * Usage Example:
1835 * $page->set('X-Priority', '3');
1836 *
1837 * @access public
1838 * @param string $name Parameter Name
1839 * @param mixed $value Parameter Value
1840 * NOTE: will not work with arrays, there are no arrays to set/reset
1841 */
1842 public function set ( $name, $value = '' ) {
1843 if ( isset($this->$name) ) {
1844 $this->$name = $value;
1845 } else {
1846 $this->SetError('Cannot set or reset variable ' . $name);
1847 return false;
1848 }
1849 }
1850
1851 /**
1852 * Read a file from a supplied filename and return it.
1853 *
1854 * @access public
1855 * @param string $filename Parameter File Name
1856 */
1857 public function getFile($filename) {
1858 $return = '';
1859 if ($fp = fopen($filename, 'rb')) {
1860 while (!feof($fp)) {
1861 $return .= fread($fp, 1024);
1862 }
1863 fclose($fp);
1864 return $return;
1865 } else {
1866 return false;
1867 }
1868 }
1869
1870 /**
1871 * Strips newlines to prevent header injection.
1872 * @access public
1873 * @param string $str String
1874 * @return string
1875 */
1876 public function SecureHeader($str) {
1877 $str = trim($str);
1878 $str = str_replace("\r", "", $str);
1879 $str = str_replace("\n", "", $str);
1880 return $str;
1881 }
1882
1883 /**
1884 * Set the private key file and password to sign the message.
1885 *
1886 * @access public
1887 * @param string $key_filename Parameter File Name
1888 * @param string $key_pass Password for private key
1889 */
1890 public function Sign($cert_filename, $key_filename, $key_pass) {
1891 $this->sign_cert_file = $cert_filename;
1892 $this->sign_key_file = $key_filename;
1893 $this->sign_key_pass = $key_pass;
1894 }
1895}
1896
1897?>