class.t3lib_htmlmail.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2008 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * HTML mail class
00029  *
00030  * $Id: class.t3lib_htmlmail.php 4488 2008-11-25 09:18:06Z steffenk $
00031  *
00032  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00033  */
00034 /**
00035  * [CLASS/FUNCTION INDEX of SCRIPT]
00036  *
00037  *
00038  *
00039  *  193: class t3lib_htmlmail
00040  *  261:     function t3lib_htmlmail ()
00041  *  268:     function start ()
00042  *  305:     function useQuotedPrintable()
00043  *  315:     function useBase64()
00044  *  326:     function use8Bit()
00045  *  338:     function encodeMsg($content)
00046  *  348:     function addPlain ($content)
00047  *  360:     function addAttachment($file)
00048  *  378:     function addHTML ($file)
00049  *  401:     function extractHtmlInit($html,$url)
00050  *  412:     function send($recipient)
00051  *
00052  *              SECTION: Main functions
00053  *  441:     function setHeaders()
00054  *  500:     function setRecipient ($recip)
00055  *  518:     function getHTMLContentType()
00056  *  527:     function setContent()
00057  *  554:     function constructMixed ($boundary)
00058  *  593:     function constructHTML ($boundary)
00059  *  617:     function constructAlternative($boundary)
00060  *  638:     function constructHTML_media ($boundary)
00061  *  691:     function sendTheMail ()
00062  *  757:     function getBoundary()
00063  *  769:     function setPlain ($content)
00064  *  780:     function setHtml ($content)
00065  *  791:     function add_header($header)
00066  *  812:     function add_message($string)
00067  *  823:     function getContent($type)
00068  *  832:     function preview()
00069  *
00070  *              SECTION: Functions for acquiring attachments, HTML, analyzing and so on  **
00071  *  860:     function fetchHTML($file)
00072  *  878:     function fetchHTMLMedia()
00073  *  899:     function extractMediaLinks()
00074  *  976:     function extractHyperLinks()
00075  * 1025:     function extractFramesInfo()
00076  * 1051:     function substMediaNamesInHTML($absolute)
00077  * 1078:     function substHREFsInHTML()
00078  * 1106:     function substHTTPurlsInPlainText($content)
00079  * 1142:     function fixRollOvers()
00080  *
00081  *              SECTION: File and URL-functions
00082  * 1189:     function makeBase64($inputstr)
00083  * 1200:     function getExtendedURL($url)
00084  * 1222:     function addUserPass($url)
00085  * 1238:     function getURL($url)
00086  * 1250:     function getStrippedURL($url)
00087  * 1271:     function getMimeType($url)
00088  * 1300:     function absRef($ref)
00089  * 1320:     function split_fileref($fileref)
00090  * 1347:     function extParseUrl($path)
00091  * 1362:     function tag_regex($tagArray)
00092  * 1384:     function get_tag_attributes($tag)
00093  * 1426:     function quoted_printable($string)
00094  * 1437:     function convertName($name)
00095  *
00096  * TOTAL FUNCTIONS: 49
00097  * (This index is automatically created/updated by the extension "extdeveval")
00098  *
00099  */
00100 /**
00101  * NOTES on MIME mail structures:
00102  *
00103  * Plain + HTML
00104  *  multipart/alternative   (text, html)
00105  *  multipart/alternative   (text, html)
00106  *
00107  * Plain + HTML + image
00108  *  multipart/related (m/a, cids)
00109  *      multipart/alternative (text, html)
00110  *
00111  *  multipart/related  (m/a, cids)
00112  *      multipart/alternative   (text, html)
00113  *
00114  * plain + attachment
00115  *  multipart/mixed
00116  *
00117  * HTML + Attachment:
00118  *  multipart/mixed     (text/html , attachments)
00119  *
00120  * Plain + HTML + Attachments:
00121  *  multipart/mixed     (m/a, attachments)
00122  *      multipart/alternative   (text, html)
00123  *
00124  * Plain + HTML + image + attachment
00125  *
00126  *      Outlook expr.
00127  *  multipart/mixed (m/r, attachments)
00128  *      multipart/related  (m/a, cids)
00129  *          multipart/alternative   (text, html)
00130  *
00131  *
00132  *
00133  * FROM RFC 1521:
00134  *
00135  * 5.1 Quoted-Printable Content-Transfer-Encoding
00136  * The Quoted-Printable encoding is intended to represent data that largely consists of octets that correspond to printable characters in the ASCII character set. It encodes the data in such a way that the resulting octets are unlikely to be modified by mail transport. If the data being encoded are mostly ASCII text, the encoded form of the data remains largely recognizable by humans. A body which is entirely ASCII may also be encoded in Quoted-Printable to ensure the integrity of the data should the message pass through a character- translating, and/or line-wrapping gateway.
00137  *
00138  * In this encoding, octets are to be represented as determined by the following rules:
00139  * Rule #1: (General 8-bit representation) Any octet, except those indicating a line break according to the newline convention of the canonical (standard) form of the data being encoded, may be represented by an "=" followed by a two digit hexadecimal representation of the octet's value. The digits of the hexadecimal alphabet, for this purpose, are "0123456789ABCDEF". Uppercase letters must be used when sending hexadecimal data, though a robust implementation may choose to recognize lowercase letters on receipt. Thus, for example, the value 12 (ASCII form feed) can be represented by "=0C", and the value 61 (ASCII EQUAL SIGN) can be represented by "=3D". Except when the following rules allow an alternative encoding, this rule is mandatory.
00140  * Rule #2: (Literal representation) Octets with decimal values of 33 through 60 inclusive, and 62 through 126, inclusive, MAY be represented as the ASCII characters which correspond to those octets (EXCLAMATION POINT through LESS THAN, and GREATER THAN through TILDE, respectively).
00141  * Rule #3: (White Space): Octets with values of 9 and 32 MAY be represented as ASCII TAB (HT) and SPACE characters, respectively, but MUST NOT be so represented at the end of an encoded line. Any TAB (HT) or SPACE characters on an encoded line MUST thus be followed on that line by a printable character. In particular, an
00142  * "=" at the end of an encoded line, indicating a soft line break (see rule #5) may follow one or more TAB (HT) or SPACE characters. It follows that an octet with value 9 or 32 appearing at the end of an encoded line must be represented according to Rule #1. This rule is necessary because some MTAs (Message Transport Agents, programs which transport messages from one user to another, or perform a part of such transfers) are known to pad lines of text with SPACEs, and others are known to remove "white space" characters from the end of a line. Therefore, when decoding a Quoted-Printable body, any trailing white space on a line must be deleted, as it will necessarily have been added by intermediate transport agents.
00143  * Rule #4 (Line Breaks): A line break in a text body, independent of what its representation is following the canonical representation of the data being encoded, must be represented by a (RFC 822) line break, which is a CRLF sequence, in the Quoted-Printable encoding. Since the canonical representation of types other than text do not generally include the representation of line breaks, no hard line breaks (i.e. line breaks that are intended to be meaningful and to be displayed to the user) should occur in the quoted-printable encoding of such types. Of course, occurrences of "=0D", "=0A", "0A=0D" and "=0D=0A" will eventually be encountered. In general, however, base64 is preferred over quoted-printable for binary data.
00144  * Note that many implementations may elect to encode the local representation of various content types directly, as described in Appendix G. In particular, this may apply to plain text material on systems that use newline conventions other than CRLF delimiters. Such an implementation is permissible, but the generation of line breaks must be generalized to account for the case where alternate representations of newline sequences are used.
00145  * Rule #5 (Soft Line Breaks): The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76 characters long. If longer lines are to be encoded with the Quoted-Printable encoding, 'soft' line breaks must be used. An equal sign as the last character on a encoded line indicates such a non-significant ('soft') line break in the encoded text. Thus if the "raw" form of the line is a single unencoded line that says:
00146  * Now's the time for all folk to come to the aid of their country.
00147  *
00148  * This can be represented, in the Quoted-Printable encoding, as
00149  *
00150  * Now's the time =
00151  * for all folk to come=
00152  * to the aid of their country.
00153  *
00154  * This provides a mechanism with which long lines are encoded in such a way as to be restored by the user agent. The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.
00155  * Since the hyphen character ("-") is represented as itself in the Quoted-Printable encoding, care must be taken, when encapsulating a quoted-printable encoded body in a multipart entity, to ensure that the encapsulation boundary does not appear anywhere in the encoded body. (A good strategy is to choose a boundary that includes a character sequence such as "=_" which can never appear in a quoted- printable body. See the definition of multipart messages later in this document.)
00156  * NOTE: The quoted-printable encoding represents something of a compromise between readability and reliability in transport. Bodies encoded with the quoted-printable encoding will work reliably over most mail gateways, but may not work perfectly over a few gateways, notably those involving translation into EBCDIC. (In theory, an EBCDIC gateway could decode a quoted-printable body and re-encode it using base64, but such gateways do not yet exist.) A higher level of confidence is offered by the base64 Content-Transfer-Encoding. A way to get reasonably reliable transport through EBCDIC gateways is to also quote the ASCII characters
00157  * !"#$@[\]^`{|}~
00158  * according to rule #1. See Appendix B for more information.
00159  * Because quoted-printable data is generally assumed to be line- oriented, it is to be expected that the representation of the breaks between the lines of quoted printable data may be altered in transport, in the same manner that plain text mail has always been altered in Internet mail when passing between systems with differing newline conventions. If such alterations are likely to constitute a corruption of the data, it is probably more sensible to use the base64 encoding rather than the quoted-printable encoding.
00160  * WARNING TO IMPLEMENTORS: If binary data are encoded in quoted- printable, care must be taken to encode CR and LF characters as "=0D" and "=0A", respectively. In particular, a CRLF sequence in binary data should be encoded as "=0D=0A". Otherwise, if CRLF were represented as a hard line break, it might be incorrectly decoded on
00161  * platforms with different line break conventions.
00162  * For formalists, the syntax of quoted-printable data is described by the following grammar:
00163  *
00164  *    quoted-printable := ([*(ptext / SPACE / TAB) ptext] ["="] CRLF)
00165  *         ; Maximum line length of 76 characters excluding CRLF
00166  *
00167  *    ptext := octet /<any ASCII character except "=", SPACE, or TAB>
00168  *         ; characters not listed as "mail-safe" in Appendix B
00169  *         ; are also not recommended.
00170  *
00171  *    octet := "=" 2(DIGIT / "A" / "B" / "C" / "D" / "E" / "F")
00172  *         ; octet must be used for characters > 127, =, SPACE, or TAB,
00173  *         ; and is recommended for any characters not listed in
00174  *         ; Appendix B as "mail-safe".
00175  */
00176 /**
00177  * HTML mail class
00178  *
00179  * @author  Kasper Skaarhoj <kasperYYYY@typo3.com>
00180  * @package TYPO3
00181  * @subpackage  t3lib
00182  */
00183 class t3lib_htmlmail {
00184         // Headerinfo:
00185     var $recipient      = '';
00186     var $recipient_copy = '';   // This recipient (or list of...) will also receive the mail. Regard it as a copy.
00187     var $subject        = '';
00188     var $from_email     = '';
00189     var $from_name      = '';
00190     var $replyto_email  = '';
00191     var $replyto_name   = '';
00192     var $organisation   = '';
00193     var $priority       = 3;    // 1 = highest, 5 = lowest, 3 = normal
00194     var $mailer         = '';   // X-mailer, set to TYPO3 Major.Minor in constructor
00195     var $alt_base64     = 0;
00196     var $alt_8bit       = 0;
00197     var $jumperURL_prefix   = '';       // This is a prefix that will be added to all links in the mail. Example: 'http://www.mydomain.com/jump?userid=###FIELD_uid###&url='. if used, anything after url= is urlencoded.
00198     var $jumperURL_useId    = 0;            // If set, then the array-key of the urls are inserted instead of the url itself. Smart in order to reduce link-length
00199     var $mediaList      = '';               // If set, this is a list of the media-files (index-keys to the array) that should be represented in the html-mail
00200     var $http_password  = '';
00201     var $http_username  = '';
00202     var $postfix_version1   = false;
00203 
00204     // Internal
00205     /*
00206     This is how the $theParts-array is normally looking
00207     var $theParts = array(
00208         'plain' => array(
00209             'content' => ''
00210         ),
00211         'html' => array(
00212             'content' => '',
00213             'path' => '',
00214             'media' => array(),
00215             'hrefs' => array()
00216         ),
00217         'attach' => array()
00218     );
00219     */
00220     var $theParts = array();
00221 
00222     var $messageid = '';
00223     var $returnPath = '';
00224     var $Xid = '';
00225     var $dontEncodeHeader = false;      // If set, the header will not be encoded
00226 
00227     var $headers = '';
00228     var $message = '';
00229     var $part = 0;
00230     var $image_fullpath_list = '';
00231     var $href_fullpath_list = '';
00232 
00233     var $plain_text_header = '';
00234     var $html_text_header = '';
00235     var $charset = '';
00236     var $defaultCharset = 'iso-8859-1';
00237 
00238 
00239 
00240     /**
00241      * Constructor. If the configuration variable forceReturnPath is set,
00242      * calls to mail will be called with a 5th parameter.
00243      * See function sendTheMail for more info
00244      *
00245      * @return  void
00246      */
00247     public function t3lib_htmlmail() {
00248         $this->forceReturnPath = $GLOBALS['TYPO3_CONF_VARS']['SYS']['forceReturnPath'];
00249 
00250         $this->mailer = 'TYPO3 '.TYPO3_version;
00251     }
00252 
00253 
00254     /**
00255      * start action that sets the message ID and the charset
00256      *
00257      * @return  void
00258      */
00259     public function start() {
00260         global $TYPO3_CONF_VARS;
00261 
00262             // Sets the message id
00263         $host = t3lib_div::getHostname();
00264         if (!$host || $host == '127.0.0.1' || $host == 'localhost' || $host == 'localhost.localdomain') {
00265             $host = ($TYPO3_CONF_VARS['SYS']['sitename'] ? preg_replace('/[^A-Za-z0-9_\-]/', '_', $TYPO3_CONF_VARS['SYS']['sitename']) : 'localhost') . '.TYPO3';
00266         }
00267         $this->messageid = md5(microtime()) . '@' . $host;
00268 
00269 
00270             // Default line break for Unix systems.
00271         $this->linebreak = chr(10);
00272             // Line break for Windows. This is needed because PHP on Windows systems
00273             // send mails via SMTP instead of using sendmail, and thus the linebreak needs to be \r\n.
00274         if (TYPO3_OS == 'WIN') {
00275             $this->linebreak = chr(13).chr(10);
00276         }
00277 
00278             // Sets the Charset
00279         if (!$this->charset) {
00280             if (is_object($GLOBALS['TSFE']) && $GLOBALS['TSFE']->renderCharset) {
00281                 $this->charset = $GLOBALS['TSFE']->renderCharset;
00282             } elseif (is_object($GLOBALS['LANG']) && $GLOBALS['LANG']->charSet) {
00283                 $this->charset = $GLOBALS['LANG']->charSet;
00284             } elseif ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) {
00285                 $this->charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
00286             } else  {
00287                 $this->charset = $this->defaultCharset;
00288             }
00289         }
00290 
00291             // Use quoted-printable headers by default
00292         $this->useQuotedPrintable();
00293     }
00294 
00295 
00296     /**
00297      * sets the header of both Plain Text and HTML mails to quoted printable
00298      *
00299      * @return  void
00300      */
00301     public function useQuotedPrintable() {
00302         $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: quoted-printable';
00303         $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: quoted-printable';
00304     }
00305 
00306     /**
00307      * sets the encoding headers to base64 for both the Plain Text and HTML mail
00308      *
00309      * @return  void
00310      */
00311     public function useBase64() {
00312         $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: base64';
00313         $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: base64';
00314         $this->alt_base64 = 1;
00315     }
00316 
00317 
00318     /**
00319      * sets the encoding to 8bit and the current charset of both the Plain Text and the HTML mail
00320      *
00321      * @return  void
00322      */
00323     public function use8Bit() {
00324         $this->plain_text_header = 'Content-Type: text/plain; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: 8bit';
00325         $this->html_text_header = 'Content-Type: text/html; charset='.$this->charset.$this->linebreak.'Content-Transfer-Encoding: 8bit';
00326         $this->alt_8bit = 1;
00327     }
00328 
00329 
00330     /**
00331      * Encodes the message content according to the options "alt_base64" and "alt_8bit" (no need to encode here)
00332      * or to "quoted_printable" if no option is set.
00333      *
00334      * @param   string      $content the content that will be encoded
00335      * @return  string      the encoded content
00336      */
00337     public function encodeMsg($content) {
00338         if ($this->alt_base64) {
00339             return $this->makeBase64($content);
00340         } elseif ($this->alt_8bit) {
00341             return $content;
00342         } else  {
00343             return t3lib_div::quoted_printable($content);
00344         }
00345     }
00346 
00347 
00348     /**
00349      * Adds plain-text, replaces the HTTP urls in the plain text and then encodes it
00350      *
00351      * @param   string      $content that will be added
00352      * @return  void
00353      */
00354     public function addPlain($content) {
00355         $content = $this->substHTTPurlsInPlainText($content);
00356         $this->setPlain($this->encodeMsg($content));
00357     }
00358 
00359 
00360     /**
00361      * Adds an attachment to the mail
00362      *
00363      * @param   string      $file: the filename to add
00364      * @return  boolean     whether the attachment was added or not
00365      */
00366     public function addAttachment($file) {
00367             // Fetching the content and the mime-type
00368         $fileInfo = $this->getExtendedURL($file);
00369         if ($fileInfo) {
00370             if (!$fileInfo['content_type']) {
00371                 $fileInfo['content_type'] = 'application/octet-stream';
00372             }
00373             $temp = $this->split_fileref($file);
00374             if ($temp['file']) {
00375                 $fileInfo['filename'] = $temp['file'];
00376             } elseif (strpos(' '.$fileInfo['content_type'], 'htm')) {
00377                 $fileInfo['filename'] = 'index.html';
00378             } else  {
00379                 $fileInfo['filename'] = 'unknown';
00380             }
00381             $this->theParts['attach'][] = $fileInfo;
00382             return true;
00383         }
00384         return false;
00385     }
00386 
00387 
00388     /**
00389      * Adds HTML and media, encodes it from a URL or file
00390      *
00391      * @param   string      $file: the filename to add
00392      * @return  boolean     whether the attachment was added or not
00393      */
00394     public function addHTML($file) {
00395         $status = $this->fetchHTML($file);
00396         if (!$status) {
00397             return false;
00398         }
00399         if ($this->extractFramesInfo()) {
00400             return 'Document was a frameset. Stopped';
00401         }
00402         $this->extractMediaLinks();
00403         $this->extractHyperLinks();
00404         $this->fetchHTMLMedia();
00405         $this->substMediaNamesInHTML(0);    // 0 = relative
00406         $this->substHREFsInHTML();
00407         $this->setHtml($this->encodeMsg($this->theParts['html']['content']));
00408     }
00409 
00410 
00411     /**
00412      * Extract HTML-parts, used externally
00413      *
00414      * @param   string      $html: will be added to the html "content" part
00415      * @param   string      $url: will be added to the html "path" part
00416      * @return  void
00417      */
00418     public function extractHtmlInit($html,$url) {
00419         $this->theParts['html']['content'] = $html;
00420         $this->theParts['html']['path'] = $url;
00421     }
00422 
00423 
00424     /**
00425      * Assembles the message by headers and content and finally send it to the provided recipient.
00426      *
00427      * @param   string      $recipient: The recipient the message should be delivered to (if blank, $this->recipient will be used instead)
00428      * @return  boolean     Returns whether the mail was sent (successfully accepted for delivery)
00429      */
00430     public function send($recipient) {
00431         if ($recipient) {
00432             $this->recipient = $recipient;
00433         }
00434         $this->setHeaders();
00435         $this->setContent();
00436         $mailWasSent = $this->sendTheMail();
00437         return $mailWasSent;
00438     }
00439 
00440 
00441 
00442 
00443 
00444 
00445 
00446 
00447 
00448 
00449 
00450 
00451 
00452     /*****************************************
00453      *
00454      * Main functions
00455      *
00456      *****************************************/
00457 
00458     /**
00459      * Clears the header-string and sets the headers based on object-vars.
00460      *
00461      * @return  void
00462      */
00463     public function setHeaders() {
00464         $this->headers = '';
00465             // Message_id
00466         $this->add_header('Message-ID: <'.$this->messageid.'>');
00467             // Return path
00468         if ($this->returnPath) {
00469             $this->add_header('Return-Path: '.$this->returnPath);
00470             $this->add_header('Errors-To: '.$this->returnPath);
00471         }
00472             // X-id
00473         if ($this->Xid) {
00474             $this->add_header('X-Typo3MID: '.$this->Xid);
00475         }
00476 
00477             // From
00478         if ($this->from_email) {
00479             if ($this->from_name && !t3lib_div::isBrokenEmailEnvironment()) {
00480                 $this->add_header('From: '.$this->from_name.' <'.$this->from_email.'>');
00481             } else {
00482                 $this->add_header('From: '.$this->from_email);
00483             }
00484         }
00485             // Reply
00486         if ($this->replyto_email) {
00487             if ($this->replyto_name) {
00488                 $this->add_header('Reply-To: '.$this->replyto_name.' <'.$this->replyto_email.'>');
00489             } else {
00490                 $this->add_header('Reply-To: '.$this->replyto_email);
00491             }
00492         }
00493             // Organisation
00494         if ($this->organisation) {
00495             $this->add_header('Organisation: '.$this->organisation);
00496         }
00497             // mailer
00498         if ($this->mailer) {
00499             $this->add_header('X-Mailer: '.$this->mailer);
00500         }
00501             // priority
00502         if ($this->priority) {
00503             $this->add_header('X-Priority: '.$this->priority);
00504         }
00505         $this->add_header('Mime-Version: 1.0');
00506 
00507         if (!$this->dontEncodeHeader) {
00508             $enc = $this->alt_base64 ? 'base64' : 'quoted_printable';   // Header must be ASCII, therefore only base64 or quoted_printable are allowed!
00509                 // Quote recipient and subject
00510             $this->recipient = t3lib_div::encodeHeader($this->recipient,$enc,$this->charset);
00511             $this->subject = t3lib_div::encodeHeader($this->subject,$enc,$this->charset);
00512         }
00513     }
00514 
00515 
00516     /**
00517      * Sets the recipient(s). If you supply a string, you set one recipient.
00518      * If you supply an array, every value is added as a recipient.
00519      *
00520      * @param   mixed       $recipient: the recipient(s) to set
00521      * @return  void
00522      */
00523     public function setRecipient($recipient) {
00524         $this->recipient = (is_array($recipient) ? implode(',', $recipient) : $recipient);
00525     }
00526 
00527 
00528     /**
00529      * Returns the content type based on whether the mail has media / attachments or no
00530      *
00531      * @return  string      the content type
00532      */
00533     public function getHTMLContentType() {
00534         return (count($this->theParts['html']['media']) ? 'multipart/related' : 'multipart/alternative');
00535     }
00536 
00537 
00538     /**
00539      * Begins building the message-body
00540      *
00541      * @return  void
00542      */
00543     public function setContent() {
00544         $this->message = '';
00545         $boundary = $this->getBoundary();
00546 
00547             // Setting up headers
00548         if (count($this->theParts['attach'])) {
00549             // Generate (plain/HTML) / attachments
00550             $this->add_header('Content-Type: multipart/mixed;');
00551             $this->add_header(' boundary="' . $boundary . '"');
00552             $this->add_message('This is a multi-part message in MIME format.' . "\n");
00553             $this->constructMixed($boundary);
00554         } elseif ($this->theParts['html']['content']) {
00555             // Generate plain/HTML mail
00556             $this->add_header('Content-Type: ' . $this->getHTMLContentType() . ';');
00557             $this->add_header(' boundary="' . $boundary . '"');
00558             $this->add_message('This is a multi-part message in MIME format.' . "\n");
00559             $this->constructHTML($boundary);
00560         } else {
00561             // Generate plain only
00562             $this->add_header($this->plain_text_header);
00563             $this->add_message($this->getContent('plain'));
00564         }
00565     }
00566 
00567 
00568     /**
00569      * This functions combines the plain / HTML content with the attachments
00570      *
00571      * @param   string      $boundary: the mail boundary
00572      * @return  void
00573      */
00574     public function constructMixed($boundary) {
00575         $this->add_message('--' . $boundary);
00576 
00577         if ($this->theParts['html']['content']) {
00578             // HTML and plain is added
00579             $newBoundary = $this->getBoundary();
00580             $this->add_message('Content-Type: '.$this->getHTMLContentType() . ';');
00581             $this->add_message(' boundary="' . $newBoundary . '"');
00582             $this->add_message('');
00583             $this->constructHTML($newBoundary);
00584         } else {
00585             // Purely plain
00586             $this->add_message($this->plain_text_header);
00587             $this->add_message('');
00588             $this->add_message($this->getContent('plain'));
00589         }
00590         // attachments are added
00591         if (is_array($this->theParts['attach'])) {
00592             foreach ($this->theParts['attach'] as $media) {
00593                 $this->add_message('--' . $boundary);
00594                 $this->add_message('Content-Type: ' . $media['content_type'] . ';');
00595                 $this->add_message(' name="' . $media['filename'] . '"');
00596                 $this->add_message('Content-Transfer-Encoding: base64');
00597                 $this->add_message('Content-Disposition: attachment;');
00598                 $this->add_message(' filename="' . $media['filename'] . '"');
00599                 $this->add_message('');
00600                 $this->add_message($this->makeBase64($media['content']));
00601             }
00602         }
00603         $this->add_message('--' . $boundary . '--' . "\n");
00604     }
00605 
00606 
00607     /**
00608      * this function creates the HTML part of the mail
00609      *
00610      * @param   string      $boundary: the boundary to use
00611      * @return  void
00612      */
00613     public function constructHTML($boundary) {
00614         // If media, then we know, the multipart/related content-type has been set before this function call
00615         if (count($this->theParts['html']['media'])) {
00616             $this->add_message('--' . $boundary);
00617             // HTML has media
00618             $newBoundary = $this->getBoundary();
00619             $this->add_message('Content-Type: multipart/alternative;');
00620             $this->add_message(' boundary="' . $newBoundary . '"');
00621             $this->add_message('Content-Transfer-Encoding: 7bit');
00622             $this->add_message('');
00623 
00624                 // Adding the plaintext/html mix, and use $newBoundary
00625             $this->constructAlternative($newBoundary);
00626             $this->constructHTML_media($boundary);
00627         } else  {
00628             // if no media, just use the $boundary for adding plaintext/html mix
00629             $this->constructAlternative($boundary);
00630         }
00631     }
00632 
00633 
00634     /**
00635      * Here plain is combined with HTML
00636      *
00637      * @param   string      $boundary: the boundary to use
00638      * @return  void
00639      */
00640     public function constructAlternative($boundary) {
00641         $this->add_message('--'.$boundary);
00642 
00643             // plain is added
00644         $this->add_message($this->plain_text_header);
00645         $this->add_message('');
00646         $this->add_message($this->getContent('plain'));
00647         $this->add_message('--' . $boundary);
00648 
00649             // html is added
00650         $this->add_message($this->html_text_header);
00651         $this->add_message('');
00652         $this->add_message($this->getContent('html'));
00653         $this->add_message('--' . $boundary . '--' . "\n");
00654     }
00655 
00656 
00657     /**
00658      * Constructs the HTML-part of message if the HTML contains media
00659      *
00660      * @param   string      $boundary: the boundary to use
00661      * @return  void
00662      */
00663     public function constructHTML_media($boundary) {
00664         // media is added
00665         if (is_array($this->theParts['html']['media'])) {
00666             foreach($this->theParts['html']['media'] as $key => $media) {
00667                 if (!$this->mediaList || t3lib_div::inList($this->mediaList, $key)) {
00668                     $this->add_message('--' . $boundary);
00669                     $this->add_message('Content-Type: ' . $media['ctype']);
00670                     $this->add_message('Content-ID: <part' . $key . '.' . $this->messageid . '>');
00671                     $this->add_message('Content-Transfer-Encoding: base64');
00672                     $this->add_message('');
00673                     $this->add_message($this->makeBase64($media['content']));
00674                 }
00675             }
00676         }
00677         $this->add_message('--' . $boundary . '--' . "\n");
00678     }
00679 
00680 
00681     /**
00682      * Sends the mail by calling the mail() function in php. On Linux systems this will invoke the MTA
00683      * defined in php.ini (sendmail -t -i by default), on Windows a SMTP must be specified in the sys.ini.
00684      * Most common MTA's on Linux has a Sendmail interface, including Postfix and Exim.
00685      * For setting the return-path correctly, the parameter -f has to be added to the system call to sendmail.
00686      * This obviously does not have any effect on Windows, but on Sendmail compliant systems this works. If safe mode
00687      * is enabled, then extra parameters is not allowed, so a safe mode check is made before the mail() command is
00688      * invoked. When using the -f parameter, some MTA's will put an X-AUTHENTICATION-WARNING saying that
00689      * the return path was modified manually with the -f flag. To disable this warning make sure that the user running
00690      * Apache is in the /etc/mail/trusted-users table.
00691      *
00692      * POSTFIX: With postfix version below 2.0 there is a problem that the -f parameter can not be used in conjunction
00693      * with -t. Postfix will give an error in the maillog:
00694      *
00695      *  cannot handle command-line recipients with -t
00696      *
00697      * The -f parameter is only enabled if the parameter forceReturnPath is enabled in the install tool.
00698      *
00699      * This whole problem of return-path turns out to be quite tricky. If you have a solution that works better, on all
00700      * standard MTA's then we are very open for suggestions.
00701      *
00702      * With time this function should be made such that several ways of sending the mail is possible (local MTA, smtp other).
00703      *
00704      * @return  boolean     Returns whether the mail was sent (successfully accepted for delivery)
00705      */
00706     public function sendTheMail() {
00707         $mailWasSent = false;
00708 
00709             // Sending the mail requires the recipient and message to be set.
00710         if (!trim($this->recipient) || !trim($this->message)) {
00711             return false;
00712         }
00713 
00714             // On windows the -f flag is not used (specific for Sendmail and Postfix),
00715             // but instead the php.ini parameter sendmail_from is used.
00716         $returnPath = (strlen($this->returnPath) > 0) ? '-f ' . escapeshellarg($this->returnPath) : '';
00717         if($this->returnPath) {
00718             @ini_set('sendmail_from', t3lib_div::normalizeMailAddress($this->returnPath));
00719         }
00720         $recipient = t3lib_div::normalizeMailAddress($this->recipient);
00721         $recipient_copy = t3lib_div::normalizeMailAddress($this->recipient_copy);
00722 
00723         // If safe mode is on, the fifth parameter to mail is not allowed, so the fix wont work on unix with safe_mode=On
00724         $returnPathPossible = (!ini_get('safe_mode') && $this->forceReturnPath);
00725         if ($returnPathPossible) {
00726             $mailWasSent = mail($recipient,
00727                   $this->subject,
00728                   $this->message,
00729                   $this->headers,
00730                   $returnPath);
00731         } else {
00732             $mailWasSent = mail($recipient,
00733                   $this->subject,
00734                   $this->message,
00735                   $this->headers);
00736         }
00737 
00738             // Sending a copy
00739         if ($recipient_copy) {
00740             if ($returnPathPossible) {
00741                 $mailWasSent = mail($recipient_copy,
00742                     $this->subject,
00743                     $this->message,
00744                     $this->headers,
00745                     $returnPath);
00746             } else {
00747                 $mailWasSent = mail($recipient_copy,
00748                     $this->subject,
00749                     $this->message,
00750                     $this->headers);
00751             }
00752         }
00753             // Auto response
00754         if ($this->auto_respond_msg) {
00755             $theParts = explode('/',$this->auto_respond_msg,2);
00756             $theParts[1] = str_replace("/",chr(10),$theParts[1]);
00757             if ($returnPathPossible) {
00758                 $mailWasSent = mail($this->from_email,
00759                     $theParts[0],
00760                     $theParts[1],
00761                     'From: ' . $recipient,
00762                     $returnPath);
00763             } else {
00764                 $mailWasSent = mail($this->from_email,
00765                     $theParts[0],
00766                     $theParts[1],
00767                     'From: ' . $recipient);
00768             }
00769         }
00770         if ($this->returnPath) {
00771             ini_restore('sendmail_from');
00772         }
00773         return $mailWasSent;
00774     }
00775 
00776 
00777     /**
00778      * Returns boundaries
00779      *
00780      * @return  string  the boundary
00781      */
00782     public function getBoundary() {
00783         $this->part++;
00784         return  "----------".uniqid("part_".$this->part."_");
00785     }
00786 
00787 
00788     /**
00789      * Sets the plain-text part. No processing done.
00790      *
00791      * @param   string      $content: the plain content
00792      * @return  void
00793      */
00794     public function setPlain($content) {
00795         $this->theParts['plain']['content'] = $content;
00796     }
00797 
00798 
00799     /**
00800      * Sets the HTML-part. No processing done.
00801      *
00802      * @param   string      $content: the HTML content
00803      * @return  void
00804      */
00805     public function setHtml($content) {
00806         $this->theParts['html']['content'] = $content;
00807     }
00808 
00809 
00810     /**
00811      * Adds a header to the mail. Use this AFTER the setHeaders()-function
00812      *
00813      * @param   string      $header: the header in form of "key: value"
00814      * @return  void
00815      */
00816     public function add_header($header) {
00817             // Mail headers must be ASCII, therefore we convert the whole header to either base64 or quoted_printable
00818         if (!$this->dontEncodeHeader && !stristr($header,'Content-Type') && !stristr($header,'Content-Transfer-Encoding')) {
00819                 // Field tags must not be encoded
00820             $parts = explode(': ',$header,2);
00821             if (count($parts) == 2) {
00822                 $enc = $this->alt_base64 ? 'base64' : 'quoted_printable';
00823                 $parts[1] = t3lib_div::encodeHeader($parts[1], $enc, $this->charset);
00824                 $header = implode(': ', $parts);
00825             }
00826         }
00827 
00828         $this->headers .= $header."\n";
00829     }
00830 
00831 
00832     /**
00833      * Adds a line of text to the mail-body. Is normally used internally
00834      *
00835      * @param   string      $msg: the message to add
00836      * @return  void
00837      */
00838     public function add_message($msg) {
00839         $this->message .= $msg."\n";
00840     }
00841 
00842 
00843     /**
00844      * returns the content specified by the type (plain, html etc.)
00845      *
00846      * @param   string      $type: the content type, can either plain or html
00847      * @return  void
00848      */
00849     public function getContent($type) {
00850         return $this->theParts[$type]['content'];
00851     }
00852 
00853 
00854     /**
00855      * shows a preview of the email of the headers and the message
00856      *
00857      * @return  void
00858      */
00859     public function preview() {
00860         echo nl2br(htmlspecialchars($this->headers));
00861         echo "<BR>";
00862         echo nl2br(htmlspecialchars($this->message));
00863     }
00864 
00865 
00866 
00867 
00868 
00869 
00870 
00871 
00872     /****************************************************
00873      *
00874      * Functions for acquiring attachments, HTML, analyzing and so on  **
00875      *
00876      ***************************************************/
00877 
00878     /**
00879      * Fetches the HTML-content from either url og local serverfile
00880      *
00881      * @param   string      $file: the file to load
00882      * @return  boolean     whether the data was fetched or not
00883      */
00884     public function fetchHTML($file) {
00885             // Fetches the content of the page
00886         $this->theParts['html']['content'] = $this->getURL($file);
00887         if ($this->theParts['html']['content']) {
00888             $addr = $this->extParseUrl($file);
00889             $path = ($addr['scheme']) ? $addr['scheme'].'://'.$addr['host'].(($addr['port'])?':'.$addr['port']:'').(($addr['filepath'])?$addr['filepath']:'/') : $addr['filepath'];
00890             $this->theParts['html']['path'] = $path;
00891             return true;
00892         } else  {
00893             return false;
00894         }
00895     }
00896 
00897 
00898     /**
00899      * Fetches the mediafiles which are found by extractMediaLinks()
00900      *
00901      * @return  void
00902      */
00903     public function fetchHTMLMedia() {
00904         if (!is_array($this->theParts['html']['media']) || !count($this->theParts['html']['media'])) return;
00905         foreach ($this->theParts['html']['media'] as $key => $media) {
00906                 // fetching the content and the mime-type
00907             $picdata = $this->getExtendedURL($this->theParts['html']['media'][$key]['absRef']);
00908             if (is_array($picdata)) {
00909                 $this->theParts['html']['media'][$key]['content'] = $picdata['content'];
00910                 $this->theParts['html']['media'][$key]['ctype']   = $picdata['content_type'];
00911             }
00912         }
00913     }
00914 
00915 
00916     /**
00917      * extracts all media-links from $this->theParts['html']['content']
00918      *
00919      * @return  void
00920      */
00921     public function extractMediaLinks() {
00922         $html_code = $this->theParts['html']['content'];
00923         $attribRegex = $this->tag_regex(array('img','table','td','tr','body','iframe','script','input','embed'));
00924 
00925             // split the document by the beginning of the above tags
00926         $codepieces = split($attribRegex, $html_code);
00927         $len = strlen($codepieces[0]);
00928         $pieces = count($codepieces);
00929         $reg = array();
00930         for ($i = 1; $i < $pieces; $i++) {
00931             $tag = strtolower(strtok(substr($html_code,$len+1,10),' '));
00932             $len += strlen($tag)+strlen($codepieces[$i])+2;
00933             $dummy = eregi("[^>]*", $codepieces[$i], $reg);
00934             $attributes = $this->get_tag_attributes($reg[0]);   // Fetches the attributes for the tag
00935             $imageData = array();
00936 
00937                 // Finds the src or background attribute
00938             $imageData['ref'] = ($attributes['src'] ? $attributes['src'] : $attributes['background']);
00939             if ($imageData['ref']) {
00940                     // find out if the value had quotes around it
00941                 $imageData['quotes'] = (substr($codepieces[$i], strpos($codepieces[$i], $imageData['ref'])-1,1) == '"') ? '"' : '';
00942                     // subst_str is the string to look for, when substituting lateron
00943                 $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
00944                 if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
00945                     $this->image_fullpath_list .= "|".$imageData['subst_str']."|";
00946                     $imageData['absRef'] = $this->absRef($imageData['ref']);
00947                     $imageData['tag'] = $tag;
00948                     $imageData['use_jumpurl'] = $attributes['dmailerping']?1:0;
00949                     $this->theParts['html']['media'][] = $imageData;
00950                 }
00951             }
00952         }
00953 
00954             // Extracting stylesheets
00955         $attribRegex = $this->tag_regex(array('link'));
00956             // Split the document by the beginning of the above tags
00957         $codepieces = split($attribRegex, $html_code);
00958         $pieces = count($codepieces);
00959         for ($i = 1; $i < $pieces; $i++) {
00960             $dummy = eregi("[^>]*", $codepieces[$i], $reg);
00961                 // fetches the attributes for the tag
00962             $attributes = $this->get_tag_attributes($reg[0]);
00963             $imageData = array();
00964             if (strtolower($attributes['rel']) == 'stylesheet' && $attributes['href']) {
00965                     // Finds the src or background attribute
00966                 $imageData['ref'] = $attributes['href'];
00967                     // Finds out if the value had quotes around it
00968                 $imageData['quotes'] = (substr($codepieces[$i],strpos($codepieces[$i], $imageData['ref'])-1,1) == '"') ? '"' : '';
00969                     // subst_str is the string to look for, when substituting lateron
00970                 $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
00971                 if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
00972                     $this->image_fullpath_list .= "|".$imageData["subst_str"]."|";
00973                     $imageData['absRef'] = $this->absRef($imageData["ref"]);
00974                     $this->theParts['html']['media'][] = $imageData;
00975                 }
00976             }
00977         }
00978 
00979             // fixes javascript rollovers
00980         $codepieces = split(quotemeta(".src"), $html_code);
00981         $pieces = count($codepieces);
00982         $expr = "^[^".quotemeta("\"").quotemeta("'")."]*";
00983         for($i = 1; $i < $pieces; $i++) {
00984             $temp = $codepieces[$i];
00985             $temp = trim(ereg_replace("=","",trim($temp)));
00986             ereg($expr,substr($temp,1,strlen($temp)),$reg);
00987             $imageData['ref'] = $reg[0];
00988             $imageData['quotes'] = substr($temp,0,1);
00989                 // subst_str is the string to look for, when substituting lateron
00990             $imageData['subst_str'] = $imageData['quotes'].$imageData['ref'].$imageData['quotes'];
00991             $theInfo = $this->split_fileref($imageData['ref']);
00992 
00993             switch ($theInfo['fileext']) {
00994                 case 'gif':
00995                 case 'jpeg':
00996                 case 'jpg':
00997                     if ($imageData['ref'] && !strstr($this->image_fullpath_list,"|".$imageData["subst_str"]."|")) {
00998                         $this->image_fullpath_list .= "|".$imageData['subst_str']."|";
00999                         $imageData['absRef'] = $this->absRef($imageData['ref']);
01000                         $this->theParts['html']['media'][] = $imageData;
01001                     }
01002                 break;
01003             }
01004         }
01005     }
01006 
01007 
01008     /**
01009      * extracts all hyper-links from $this->theParts["html"]["content"]
01010      *
01011      * @return  void
01012      */
01013     public function extractHyperLinks() {
01014         $html_code = $this->theParts['html']['content'];
01015         $attribRegex = $this->tag_regex(array('a','form','area'));
01016         $codepieces = split($attribRegex, $html_code);  // Splits the document by the beginning of the above tags
01017         $len = strlen($codepieces[0]);
01018         $pieces = count($codepieces);
01019         for($i = 1; $i < $pieces; $i++) {
01020             $tag = strtolower(strtok(substr($html_code,$len+1,10)," "));
01021             $len += strlen($tag) + strlen($codepieces[$i]) + 2;
01022 
01023             $dummy = eregi("[^>]*", $codepieces[$i], $reg);
01024                 // Fetches the attributes for the tag
01025             $attributes = $this->get_tag_attributes($reg[0]);
01026             $hrefData = array();
01027             $hrefData['ref'] = ($attributes['href'] ? $attributes['href'] : $hrefData['ref'] = $attributes['action']);
01028             if ($hrefData['ref']) {
01029                     // Finds out if the value had quotes around it
01030                 $hrefData['quotes'] = (substr($codepieces[$i],strpos($codepieces[$i], $hrefData["ref"])-1,1) == '"') ? '"' : '';
01031                     // subst_str is the string to look for, when substituting lateron
01032                 $hrefData['subst_str'] = $hrefData['quotes'].$hrefData['ref'].$hrefData['quotes'];
01033                 if ($hrefData['ref'] && substr(trim($hrefData['ref']),0,1) != "#" && !strstr($this->href_fullpath_list,"|".$hrefData['subst_str']."|")) {
01034                     $this->href_fullpath_list .= "|".$hrefData['subst_str']."|";
01035                     $hrefData['absRef'] = $this->absRef($hrefData['ref']);
01036                     $hrefData['tag'] = $tag;
01037                     $this->theParts['html']['hrefs'][] = $hrefData;
01038                 }
01039             }
01040         }
01041             // Extracts TYPO3 specific links made by the openPic() JS function
01042         $codepieces = explode("onClick=\"openPic('", $html_code);
01043         $pieces = count($codepieces);
01044         for($i = 1; $i < $pieces; $i++) {
01045             $showpic_linkArr = explode("'",$codepieces[$i]);
01046             $hrefData['ref'] = $showpic_linkArr[0];
01047             if ($hrefData['ref']) {
01048                 $hrefData['quotes'] = "'";
01049                     // subst_str is the string to look for, when substituting lateron
01050                 $hrefData['subst_str'] = $hrefData['quotes'].$hrefData['ref'].$hrefData['quotes'];
01051                 if ($hrefData['ref'] && !strstr($this->href_fullpath_list,"|".$hrefData['subst_str']."|")) {
01052                     $this->href_fullpath_list .= "|".$hrefData['subst_str']."|";
01053                     $hrefData['absRef'] = $this->absRef($hrefData['ref']);
01054                     $this->theParts['html']['hrefs'][] = $hrefData;
01055                 }
01056             }
01057         }
01058     }
01059 
01060 
01061     /**
01062      * extracts all media-links from $this->theParts["html"]["content"]
01063      *
01064      * @return  array   two-dimensional array with information about each frame
01065      */
0