00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 include_once( 'lib/ezi18n/classes/eztextcodec.php' );
00048 include_once( 'lib/ezutils/classes/ezini.php' );
00049
00050 define( 'EZ_MAIL_REGEXP', '([0-9a-zA-Z]([-+.\w]*[0-9a-zA-Z_])*@(((([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))' );
00051
00052 class eZMail
00053 {
00054
00055
00056
00057 function eZMail()
00058 {
00059 $this->ReceiverElements = array();
00060 $this->From = false;
00061 $this->CcElements = array();
00062 $this->BccElements = array();
00063 $this->ReplyTo = false;
00064 $this->Subject = false;
00065 $this->BodyText = false;
00066 $this->ExtraHeaders = array();
00067 $this->TextCodec = false;
00068 $this->MessageID = false;
00069
00070
00071 include_once( 'lib/version.php' );
00072 $version = eZPublishSDK::version();
00073
00074 $this->MIMEVersion = '1.0';
00075 $this->ContentType = array( 'type' => 'text/plain',
00076 'charset' => eZTextCodec::internalCharset(),
00077 'transfer-encoding' => '8bit',
00078 'disposition' => 'inline',
00079 'boundary' => false );
00080 $this->UserAgent = "eZ publish, Version $version";
00081
00082 $ini =& eZINI::instance();
00083
00084 if ( $ini->hasVariable( 'MailSettings', 'ContentType' ) )
00085 $this->setContentType( $ini->variable( 'MailSettings', 'ContentType' ) );
00086
00087 if (! defined( 'EZ_MAIL_LINE_SEPARATOR' ) )
00088 {
00089 $ini =& eZINI::instance( 'site.ini' );
00090 $ending = $ini->variable( 'MailSettings', 'HeaderLineEnding' );
00091 if ( $ending == 'auto' )
00092 {
00093 $sys =& eZSys::instance();
00094
00095 if ( $sys->osType() == 'win32' )
00096 {
00097 $separator = "\r\n";
00098 }
00099 else
00100
00101 {
00102 $separator = "\n";
00103 }
00104 }
00105 else
00106 {
00107 $separator = urldecode( $ending );
00108 }
00109 define( 'EZ_MAIL_LINE_SEPARATOR', $separator );
00110 }
00111 }
00112
00113
00114
00115
00116 function receiverEmailText( $convert = true )
00117 {
00118 return $this->composeEmailItems( $this->ReceiverElements, true, 'email', $convert );
00119 }
00120
00121
00122
00123
00124 function receiverText( $convert = true )
00125 {
00126 return $this->composeEmailItems( $this->ReceiverElements, true, false, $convert );
00127 }
00128
00129
00130
00131
00132 function ccReceiverTextList( $convert = true )
00133 {
00134 return $this->composeEmailItems( $this->CcElements, false, 'email', $convert );
00135 }
00136
00137 function bccReceiverTextList( $convert = true )
00138 {
00139 return $this->composeEmailItems( $this->BccElements, false, 'email', $convert );
00140 }
00141
00142
00143
00144
00145 function receiverTextList( $convert = true )
00146 {
00147 return $this->composeEmailItems( $this->ReceiverElements, false, 'email', $convert );
00148 }
00149
00150
00151
00152
00153 function receiverElements()
00154 {
00155 return $this->ReceiverElements;
00156 }
00157
00158
00159
00160
00161 function ccElements()
00162 {
00163 return $this->CcElements;
00164 }
00165
00166
00167
00168
00169 function bccElements()
00170 {
00171 return $this->BccElements;
00172 }
00173
00174
00175
00176
00177
00178 function replyTo( $convert = true )
00179 {
00180 if ( !$convert )
00181 return $this->ReplyTo;
00182 return $this->convertHeaderText( $this->ReplyTo );
00183 }
00184
00185
00186
00187
00188 function sender( $convert = true )
00189 {
00190 if ( !$convert )
00191 return $this->From;
00192
00193 if ( is_array( $this->From ) )
00194 {
00195 $convertedSender = $this->From;
00196 if ( $this->From['name'] )
00197 {
00198 $convertedSender['name'] = $this->convertHeaderText( $this->From['name'] );
00199 }
00200 return $convertedSender;
00201 }
00202 else if ( is_string( $this->From ) )
00203 {
00204 return $this->convertHeaderText( $this->From );
00205 }
00206
00207 return $this->From;
00208 }
00209
00210
00211
00212
00213 function senderText( $convert = true )
00214 {
00215 $text = eZMail::composeEmailName( $this->From );
00216 if ( !$convert )
00217 return $text;
00218 return $this->convertHeaderText( $text );
00219 }
00220
00221
00222
00223
00224
00225 function mimeVersion()
00226 {
00227 return $this->MIMEVersion;
00228 }
00229
00230
00231
00232
00233 function contentType()
00234 {
00235 return $this->ContentType['type'];
00236 }
00237
00238
00239
00240
00241
00242 function contentCharset()
00243 {
00244 return $this->ContentType['charset'];
00245 }
00246
00247
00248
00249
00250 function contentTransferEncoding()
00251 {
00252 return $this->ContentType['transfer-encoding'];
00253 }
00254
00255
00256
00257
00258 function contentDisposition()
00259 {
00260 return $this->ContentType['disposition'];
00261 }
00262
00263
00264
00265
00266 function userAgent( $convert = true )
00267 {
00268 if ( !$convert )
00269 return $this->UserAgent;
00270 return $this->convertHeaderText( $this->UserAgent );
00271 }
00272
00273
00274
00275
00276 function setMIMEVersion( $version )
00277 {
00278 $this->MIMEVersion = $version;
00279 }
00280
00281
00282
00283
00284
00285 function setContentType( $type = false, $charset = false,
00286 $transferEncoding = false, $disposition = false, $boundary = false )
00287 {
00288 if ( $type )
00289 $this->ContentType['type'] = $type;
00290 if ( $charset )
00291 $this->ContentType['charset'] = $charset;
00292 if ( $transferEncoding )
00293 $this->ContentType['transfer-encoding'] = $transferEncoding;
00294 if ( $disposition )
00295 $this->ContentType['disposition'] = $disposition;
00296 if ( $boundary )
00297 $this->ContentType['boundary'] = $boundary;
00298 }
00299
00300
00301
00302
00303 function setUserAgent( $agent )
00304 {
00305 $this->UserAgent = $agent;
00306 }
00307
00308
00309
00310
00311 function setReceiverElements( $toElements )
00312 {
00313 $this->ReceiverElements = $toElements;
00314 }
00315
00316
00317
00318
00319
00320
00321 function setReceiver( $email, $name = false )
00322 {
00323 $this->ReceiverElements = array( array( 'name' => $name,
00324 'email' => $email ) );
00325 }
00326
00327
00328
00329
00330
00331
00332 function setReceiverText( $text )
00333 {
00334 $this->extractEmail( $text, $email, $name );
00335 $this->ReceiverElements = array( array( 'name' => $name,
00336 'email' => $email ) );
00337 }
00338
00339
00340
00341
00342 function addReceiver( $email, $name = false )
00343 {
00344 $this->ReceiverElements[] = array( 'name' => $name,
00345 'email' => $email );
00346 }
00347
00348
00349
00350
00351
00352 function setReplyTo( $email, $name = false )
00353 {
00354 $this->ReplyTo = array( 'name' => $name,
00355 'email' => $email );
00356 }
00357
00358
00359
00360
00361 function setSender( $email, $name = false )
00362 {
00363 $this->From = array( 'name' => $name,
00364 'email' => $email );
00365 }
00366
00367
00368
00369
00370 function setSenderText( $text )
00371 {
00372 $this->extractEmail( $text, $email, $name );
00373 $this->From = array( 'name' => $name,
00374 'email' => $email );
00375 }
00376
00377
00378
00379
00380 function setCcElements( $newCc )
00381 {
00382 $this->CcElements = $newCc;
00383 }
00384
00385
00386
00387
00388 function addCc( $email, $name = false )
00389 {
00390 $this->CcElements[] = array( 'name' => $name,
00391 'email' => $email );
00392 }
00393
00394
00395
00396
00397 function setBccElements( $newBcc )
00398 {
00399 $this->BccElements = $newBcc;
00400 }
00401
00402
00403
00404
00405 function addBcc( $email, $name = false )
00406 {
00407 $this->BccElements[] = array( 'name' => $name,
00408 'email' => $email );
00409 }
00410
00411
00412
00413
00414 function extraHeaders()
00415 {
00416 return $this->ExtraHeaders;
00417 }
00418
00419
00420
00421
00422 function addExtraHeader( $headerName, $headerValue )
00423 {
00424 return $this->ExtraHeaders[] = array( 'name' => $headerName,
00425 'content' => $headerValue );
00426 }
00427
00428
00429
00430
00431 function setExtraHeader( $headerName, $headerValue )
00432 {
00433 for ( $i = 0; $i < count( $this->ExtraHeaders ); ++$i )
00434 {
00435 $extraHeader =& $this->ExtraHeaders[$i];
00436 if ( isset( $extraHeader['name'] ) and
00437 $extraHeader['name'] == $headerName )
00438 {
00439 $extraHeader = array( 'name' => $headerName,
00440 'content' => $headerValue );
00441 return true;
00442 }
00443 }
00444 $this->addExtraHeader( $headerName, $headerValue );
00445 }
00446
00447
00448
00449
00450 function setExtraHeaders( $headers )
00451 {
00452 return $this->ExtraHeaders = $headers;
00453 }
00454
00455
00456
00457
00458
00459 function messageID()
00460 {
00461 return $this->MessageID;
00462 }
00463
00464
00465
00466
00467 function setMessageID( $newMessageID )
00468 {
00469 $this->MessageID = $newMessageID;
00470 }
00471
00472
00473
00474
00475 function references()
00476 {
00477 return $this->References;
00478 }
00479
00480
00481
00482
00483 function setReferences( $newReference )
00484 {
00485 $this->References = $newReference;
00486 }
00487
00488
00489
00490
00491 function subject( $convert = true )
00492 {
00493 if ( !$convert )
00494 return $this->Subject;
00495 return $this->convertHeaderText( $this->Subject );
00496 }
00497
00498
00499
00500
00501 function setSubject( $newSubject )
00502 {
00503 $this->Subject = trim( $newSubject );
00504 }
00505
00506
00507
00508
00509 function body( $convert = true )
00510 {
00511 if ( !$convert )
00512 return $this->BodyText;
00513 return $this->convertText( $this->BodyText );
00514 }
00515
00516
00517
00518
00519 function setBody( $newBody )
00520 {
00521 $newBody = preg_replace( "/\r\n|\r|\n/", EZ_MAIL_LINE_SEPARATOR, $newBody );
00522 $this->BodyText = $newBody;
00523 }
00524
00525
00526
00527
00528
00529 function &splitList( $emails )
00530 {
00531 $emails = preg_split( "/[,;]/", $emails );
00532 return $emails;
00533 }
00534
00535
00536
00537
00538
00539
00540
00541 function validate( $address )
00542 {
00543 $pos = ( ereg( '^' . EZ_MAIL_REGEXP . '$', $address) );
00544 return $pos;
00545 }
00546
00547 function extractEmail( $text, &$email, &$name )
00548 {
00549 if ( preg_match( "/([^<]+)<" . EZ_MAIL_REGEXP . ">/", $text, $matches ) )
00550 {
00551 $email = $matches[2];
00552 $name = $matches[1];
00553 }
00554 else
00555 {
00556 $email = $text;
00557 $name = false;
00558 }
00559 }
00560
00561
00562
00563
00564
00565
00566
00567 function stripEmail( $address )
00568 {
00569 $res = ereg( EZ_MAIL_REGEXP, $address, $email );
00570 if ( $res )
00571 return $email[0];
00572 else
00573 return 0;
00574 }
00575
00576
00577
00578
00579
00580 function blankNewlines( $text )
00581 {
00582 return preg_replace( "/\r\n|\r|\n/", ' ', $text );
00583 }
00584
00585
00586
00587
00588
00589
00590 function contentString( $content )
00591 {
00592 if ( is_array( $content ) )
00593 return implode( '; ', $content );
00594 else
00595 return (string)$content;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604 function composeEmailName( $item, $key = false, $convert = true )
00605 {
00606 if ( $key !== false and
00607 isset( $item[$key] ) )
00608 return $item[$key];
00609 if ( $item['name'] )
00610 {
00611 if ( $convert )
00612 $item['name'] = $this->convertHeaderText( $item['name'] );
00613 $text = $item['name'] . ' <' . $item['email'] . '>';
00614 }
00615 else
00616 $text = $item['email'];
00617 return $text;
00618 }
00619
00620
00621
00622
00623
00624
00625 function composeEmailItems( $items, $join = true, $key = false, $convert = true )
00626 {
00627 $textElements = array();
00628 foreach ( $items as $item )
00629 {
00630 $textElements[] = eZMail::composeEmailName( $item, $key, $convert );
00631 }
00632
00633 if ( $join )
00634 $text = implode( ', ', $textElements );
00635 else
00636 $text = $textElements;
00637
00638 return $text;
00639 }
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 function headers( $parameters = array() )
00650 {
00651 $parameters = array_merge( array( 'exclude-headers' => false ),
00652 $parameters );
00653 $excludeHeaders = array();
00654 if ( $parameters['exclude-headers'] )
00655 {
00656 foreach ( $parameters['exclude-headers'] as $excludeHeader )
00657 {
00658 $excludeHeaders[] = strtolower( $excludeHeader );
00659 }
00660 }
00661 $headers = array();
00662 $headerNames = array();
00663 if ( !in_array( 'to', $excludeHeaders ) )
00664 {
00665 $toHeaderContent = count( $this->ReceiverElements ) > 0 ? $this->composeEmailItems( $this->ReceiverElements ) : 'undisclosed-recipients:;';
00666 $headers[] = array( 'name' => 'To',
00667 'content' => $toHeaderContent );
00668 $headerNames[] = 'to';
00669 }
00670 if ( !in_array( 'date', $excludeHeaders ) )
00671 {
00672 $headers[] = array( 'name' => 'Date',
00673 'content' => date( 'r' ) );
00674 $headerNames[] = 'date';
00675 }
00676 if ( $this->Subject !== false and
00677 !in_array( 'subject', $excludeHeaders ) )
00678 {
00679 $headers[] = array( 'name' => 'Subject',
00680 'content' => $this->subject() );
00681 $headerNames[] = 'subject';
00682 }
00683 if ( $this->From !== false and
00684 !in_array( 'from', $excludeHeaders ) )
00685 {
00686 $headers[] = array( 'name' => 'From',
00687 'content' => $this->composeEmailName( $this->From ) );
00688 $headerNames[] = 'from';
00689 }
00690 if ( count( $this->CcElements ) > 0 and
00691 !in_array( 'cc', $excludeHeaders ) )
00692 {
00693 $headers[] = array( 'name' => 'Cc',
00694 'content' => $this->composeEmailItems( $this->CcElements ) );
00695 $headerNames[] = 'cc';
00696 }
00697 if ( count( $this->BccElements ) > 0 and
00698 !in_array( 'bcc', $excludeHeaders ) )
00699 {
00700 $headers[] = array( 'name' => 'Bcc',
00701 'content' => $this->composeEmailItems( $this->BccElements ) );
00702 $headerNames[] = 'bcc';
00703 }
00704 if ( $this->ReplyTo !== false and
00705 !in_array( 'reply-to', $excludeHeaders ) )
00706 {
00707 $headers[] = array( 'name' => 'Reply-To',
00708 'content' => $this->composeEmailName( $this->ReplyTo ) );
00709 $headerNames[] = 'reply-to';
00710 }
00711 if ( !in_array( 'mime-version', $excludeHeaders ) )
00712 {
00713 $headers[] = array( 'name' => 'MIME-Version',
00714 'content' => $this->MIMEVersion );
00715 $headerNames[] = 'mime-version';
00716 }
00717 if ( !in_array( 'content-type', $excludeHeaders ) )
00718 {
00719 $charset = $this->usedCharset();
00720 if ( $this->ContentType['boundary'] )
00721 {
00722 $headers[] = array( 'name' => 'Content-Type',
00723 'content' => array( $this->ContentType['type'], 'charset='. $charset, 'boundary="'. $this->ContentType['boundary'] . '"' ) );
00724 }
00725 else
00726 {
00727 $headers[] = array( 'name' => 'Content-Type',
00728 'content' => array( $this->ContentType['type'], 'charset='. $charset ) );
00729 }
00730 $headerNames[] = 'content-type';
00731 }
00732 if ( !in_array( 'content-transfer-encoding', $excludeHeaders ) )
00733 {
00734 $headers[] = array( 'name' => 'Content-Transfer-Encoding',
00735 'content' => $this->ContentType['transfer-encoding'] );
00736 $headerNames[] = 'content-transfer-encoding';
00737 }
00738 if ( !in_array( 'content-disposition', $excludeHeaders ) )
00739 {
00740 $headers[] = array( 'name' => 'Content-Disposition',
00741 'content' => $this->ContentType['disposition'] );
00742 $headerNames[] = 'content-disposition';
00743 }
00744 if ( !in_array( 'user-agent', $excludeHeaders ) )
00745 {
00746 $headers[] = array( 'name' => 'User-Agent',
00747 'content' => $this->UserAgent );
00748 $headerNames[] = 'user-agent';
00749 }
00750 if ( !in_array( 'message-id', $excludeHeaders ) )
00751 {
00752 if ( $this->MessageID )
00753 {
00754 $headers[] = array( 'name' => 'Message-Id',
00755 'content' => $this->MessageID );
00756 $headerNames[] = 'message-id';
00757 }
00758 }
00759
00760 $extraHeaders = $this->ExtraHeaders;
00761 foreach ( $extraHeaders as $extraHeader )
00762 {
00763 if ( !in_array( strtolower( $extraHeader['name'] ), $excludeHeaders ) and
00764 !in_array( strtolower( $extraHeader['name'] ), $headerNames ) )
00765 $headers[] = $extraHeader;
00766 }
00767 return $headers;
00768 }
00769
00770
00771
00772
00773
00774 function headerTextList( $parameters = array() )
00775 {
00776 $convert = true;
00777 if ( isset( $parameters['convert'] ) )
00778 $convert = $parameters['convert'];
00779 $textElements = array();
00780 $headers = $this->headers( $parameters );
00781 foreach ( $headers as $header )
00782 {
00783 $headerText = $this->blankNewlines( $header['name'] ) . ': ';
00784 $contentText = $this->blankNewlines( $this->contentString( $header['content'] ) );
00785 $headerText .= $contentText;
00786 $textElements[] = $headerText;
00787 }
00788 return $textElements;
00789 }
00790
00791
00792
00793
00794
00795 function headerText( $parameters = array() )
00796 {
00797 $convert = true;
00798 if ( isset( $parameters['convert'] ) )
00799 $convert = $parameters['convert'];
00800 $text = '';
00801 $headers = $this->headers( $parameters );
00802 $headerCount = 0;
00803 foreach ( $headers as $header )
00804 {
00805 if ( $headerCount++ > 0 )
00806 $text .= EZ_MAIL_LINE_SEPARATOR;
00807 $text .= $this->blankNewlines( $header['name'] ) . ': ';
00808 $contentText = $this->blankNewlines( $this->contentString( $header['content'] ) );
00809 $text .= $contentText;
00810 }
00811 return $text;
00812 }
00813
00814
00815
00816
00817 function convertHeaderText( $text )
00818 {
00819 $charset = $this->contentCharset();
00820 if ( $charset != 'us-ascii' )
00821 {
00822 $newText = $this->encodeMimeHeader( $text );
00823 return $newText;
00824 }
00825 return $text;
00826 }
00827
00828
00829
00830
00831 function encodeMimeHeader( $str )
00832 {
00833 if ( !$this->TextCodec )
00834 {
00835 $this->TextCodec =& eZTextCodec::instance( $this->contentCharset(), $this->outputCharset() );
00836 }
00837
00838 if ( function_exists( "mb_encode_mimeheader" ) )
00839 {
00840 $encoded = mb_encode_mimeheader( $str, $this->TextCodec->InputCharsetCode, "B", EZ_MAIL_LINE_SEPARATOR );
00841 }
00842 else
00843 {
00844 if ( 0 == preg_match_all( '/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches ) )
00845 return $str;
00846
00847 $maxlen = 75 - 7 - strlen( $this->TextCodec->InputCharsetCode );
00848
00849 $encoding = 'B';
00850 $encoded = base64_encode( $str );
00851 $maxlen -= $maxlen % 4;
00852 $encoded = trim( chunk_split( $encoded, $maxlen, "\n" ) );
00853
00854 $encoded = preg_replace( '/^(.*)$/m', " =?".$this->TextCodec->InputCharsetCode."?$encoding?\\1?=", $encoded );
00855
00856 $encoded = trim( str_replace( "\n", EZ_MAIL_LINE_SEPARATOR, $encoded ) );
00857 }
00858
00859 return $encoded;
00860 }
00861
00862
00863
00864
00865
00866
00867 function convertText( $text, $isHeader = false )
00868 {
00869
00870 $charset = $this->contentCharset();
00871 if ( $this->isAllowedCharset( $charset ) )
00872 return $text;
00873 $outputCharset = $this->outputCharset();
00874 if ( !$this->TextCodec )
00875 {
00876 $this->TextCodec =& eZTextCodec::instance( $charset, $outputCharset );
00877 }
00878 $newText = $this->TextCodec->convertString( $text );
00879 return $newText;
00880 }
00881
00882
00883
00884
00885
00886 function isAllowedCharset( $charset )
00887 {
00888 include_once( 'lib/ezi18n/classes/ezcharsetinfo.php' );
00889 $realCharset = eZCharsetInfo::realCharsetCode( $charset );
00890 $charsets = $this->allowedCharsets();
00891 foreach ( $charsets as $charsetName )
00892 {
00893 $realName = eZCharsetInfo::realCharsetCode( $charsetName );
00894 if ( $realName == $realCharset )
00895 return true;
00896 }
00897 return false;
00898 }
00899
00900
00901
00902
00903 function allowedCharsets()
00904 {
00905 $ini =& eZINI::instance();
00906 $charsets = $ini->variable( 'MailSettings', 'AllowedCharsets' );
00907 return $charsets;
00908 }
00909
00910
00911
00912
00913 function usedCharset()
00914 {
00915 $charset = $this->contentCharset();
00916 if ( $this->isAllowedCharset( $charset ) )
00917 return $charset;
00918 return $this->outputCharset();
00919 }
00920
00921
00922
00923
00924 function outputCharset()
00925 {
00926 $ini =& eZINI::instance();
00927 $outputCharset = $ini->variable( 'MailSettings', 'OutputCharset' );
00928 return $outputCharset;
00929 }
00930
00931
00932
00933
00934
00935
00936 var $ReceiverElements;
00937 var $From;
00938 var $CcElements;
00939 var $BccElements;
00940 var $ContentType;
00941 var $UserAgent;
00942 var $ReplyTo;
00943 var $Subject;
00944 var $BodyText;
00945 var $ExtraHeaders;
00946 var $TextCodec;
00947 var $MessageID;
00948 }
00949
00950 ?>