| | |
| | | * in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it> and |
| | | * Sascha Schumann <sascha@schumann.cx> |
| | | * |
| | | * Function _encodeHeaders() changed by Thomas Bruederli <roundcube@gmail.com> |
| | | * in order to be read correctly by Google Gmail |
| | | * |
| | | * @author Richard Heyes <richard.heyes@heyes-computing.net> |
| | | * @author Tomas V.V.Cox <cox@idecnet.com> |
| | | * @package Mail |
| | |
| | | { |
| | | $this->_setEOL($crlf); |
| | | $this->_build_params = array( |
| | | 'head_encoding' => 'quoted-printable', |
| | | 'text_encoding' => '7bit', |
| | | 'html_encoding' => 'quoted-printable', |
| | | '7bit_wrap' => 998, |
| | |
| | | } |
| | | |
| | | /** |
| | | * returns the HTML body portion of the message |
| | | * @return string HTML body of the message |
| | | * @access public |
| | | */ |
| | | function getHTMLBody() |
| | | { |
| | | return $this->_htmlbody; |
| | | } |
| | | |
| | | /** |
| | | * Adds an image to the list of embedded images. |
| | | * |
| | | * @param string $file The image file name OR image data itself |
| | |
| | | * Only use if $file is the image data |
| | | * @param bool $isfilename Whether $file is a filename or not |
| | | * Defaults to true |
| | | * @param string $contentid Desired Content-ID of MIME part |
| | | * Defaults to generated unique ID |
| | | * @return mixed true on success or PEAR_Error object |
| | | * @access public |
| | | */ |
| | | function addHTMLImage($file, $c_type='application/octet-stream', |
| | | $name = '', $isfilename = true) |
| | | $name = '', $isfilename = true, $contentid = '') |
| | | { |
| | | $filedata = ($isfilename === true) ? $this->_file2str($file) |
| | | : $file; |
| | | if ($isfilename === true) { |
| | | $filename = ($name == '' ? basename($file) : basename($name)); |
| | | $filename = ($name == '' ? $file : $name); |
| | | } else { |
| | | $filename = basename($name); |
| | | $filename = $name; |
| | | } |
| | | if (PEAR::isError($filedata)) { |
| | | return $filedata; |
| | | } |
| | | if ($contentid == '') { |
| | | $contentid = md5(uniqid(time())); |
| | | } |
| | | $this->_html_images[] = array( |
| | | 'body' => $filedata, |
| | | 'name' => $filename, |
| | | 'c_type' => $c_type, |
| | | 'cid' => md5(uniqid(time())) |
| | | 'cid' => $contentid |
| | | ); |
| | | return true; |
| | | } |
| | |
| | | /** |
| | | * Adds a file to the list of attachments. |
| | | * |
| | | * @param string $file The file name of the file to attach |
| | | * OR the file data itself |
| | | * @param string $c_type The content type |
| | | * @param string $name The filename of the attachment |
| | | * Only use if $file is the file data |
| | | * @param bool $isFilename Whether $file is a filename or not |
| | | * Defaults to true |
| | | * @param string $file The file name of the file to attach |
| | | * OR the file contents itself |
| | | * @param string $c_type The content type |
| | | * @param string $name The filename of the attachment |
| | | * Only use if $file is the contents |
| | | * @param bool $isFilename Whether $file is a filename or not |
| | | * Defaults to true |
| | | * @param string $encoding The type of encoding to use. |
| | | * Defaults to base64. |
| | | * Possible values: 7bit, 8bit, base64, |
| | | * or quoted-printable. |
| | | * @param string $disposition The content-disposition of this file |
| | | * Defaults to attachment. |
| | | * Possible values: attachment, inline. |
| | | * @param string $charset The character set used in the filename |
| | | * of this attachment. |
| | | * @return mixed true on success or PEAR_Error object |
| | | * @access public |
| | | */ |
| | | function addAttachment($file, $c_type = 'application/octet-stream', |
| | | $name = '', $isfilename = true, |
| | | $encoding = 'base64') |
| | | $encoding = 'base64', |
| | | $disposition = 'attachment', $charset = '') |
| | | { |
| | | $filedata = ($isfilename === true) ? $this->_file2str($file) |
| | | : $file; |
| | |
| | | $filename = $name; |
| | | } |
| | | if (empty($filename)) { |
| | | return PEAR::raiseError( |
| | | 'The supplied filename for the attachment can\'t be empty' |
| | | $err = PEAR::raiseError( |
| | | "The supplied filename for the attachment can't be empty" |
| | | ); |
| | | return $err; |
| | | } |
| | | $filename = basename($filename); |
| | | if (PEAR::isError($filedata)) { |
| | |
| | | } |
| | | |
| | | $this->_parts[] = array( |
| | | 'body' => $filedata, |
| | | 'name' => $filename, |
| | | 'c_type' => $c_type, |
| | | 'encoding' => $encoding |
| | | 'body' => $filedata, |
| | | 'name' => $filename, |
| | | 'c_type' => $c_type, |
| | | 'encoding' => $encoding, |
| | | 'charset' => $charset, |
| | | 'disposition' => $disposition |
| | | ); |
| | | return true; |
| | | } |
| | |
| | | function &_file2str($file_name) |
| | | { |
| | | if (!is_readable($file_name)) { |
| | | return PEAR::raiseError('File is not readable ' . $file_name); |
| | | $err = PEAR::raiseError('File is not readable ' . $file_name); |
| | | return $err; |
| | | } |
| | | if (!$fd = fopen($file_name, 'rb')) { |
| | | return PEAR::raiseError('Could not open ' . $file_name); |
| | | $err = PEAR::raiseError('Could not open ' . $file_name); |
| | | return $err; |
| | | } |
| | | $cont = fread($fd, filesize($file_name)); |
| | | $filesize = filesize($file_name); |
| | | if ($filesize == 0){ |
| | | $cont = ""; |
| | | }else{ |
| | | if ($magic_quote_setting = get_magic_quotes_runtime()){ |
| | | set_magic_quotes_runtime(0); |
| | | } |
| | | $cont = fread($fd, $filesize); |
| | | if ($magic_quote_setting){ |
| | | set_magic_quotes_runtime($magic_quote_setting); |
| | | } |
| | | } |
| | | fclose($fd); |
| | | return $cont; |
| | | } |
| | |
| | | $params['encoding'] = $this->_build_params['text_encoding']; |
| | | $params['charset'] = $this->_build_params['text_charset']; |
| | | if (is_object($obj)) { |
| | | return $obj->addSubpart($text, $params); |
| | | $ret = $obj->addSubpart($text, $params); |
| | | return $ret; |
| | | } else { |
| | | return new Mail_mimePart($text, $params); |
| | | $ret = new Mail_mimePart($text, $params); |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | |
| | | $params['encoding'] = $this->_build_params['html_encoding']; |
| | | $params['charset'] = $this->_build_params['html_charset']; |
| | | if (is_object($obj)) { |
| | | return $obj->addSubpart($this->_htmlbody, $params); |
| | | $ret = $obj->addSubpart($this->_htmlbody, $params); |
| | | return $ret; |
| | | } else { |
| | | return new Mail_mimePart($this->_htmlbody, $params); |
| | | $ret = new Mail_mimePart($this->_htmlbody, $params); |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | |
| | | function &_addMixedPart() |
| | | { |
| | | $params['content_type'] = 'multipart/mixed'; |
| | | return new Mail_mimePart('', $params); |
| | | $ret = new Mail_mimePart('', $params); |
| | | return $ret; |
| | | } |
| | | |
| | | /** |
| | |
| | | if (is_object($obj)) { |
| | | return $obj->addSubpart('', $params); |
| | | } else { |
| | | return new Mail_mimePart('', $params); |
| | | $ret = new Mail_mimePart('', $params); |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | |
| | | if (is_object($obj)) { |
| | | return $obj->addSubpart('', $params); |
| | | } else { |
| | | return new Mail_mimePart('', $params); |
| | | $ret = new Mail_mimePart('', $params); |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | function &_addHtmlImagePart(&$obj, $value) |
| | | { |
| | | $params['content_type'] = $value['c_type']; |
| | | $params['content_type'] = $value['c_type'] . '; ' . |
| | | 'name="' . $value['name'] . '"'; |
| | | $params['encoding'] = 'base64'; |
| | | $params['disposition'] = 'inline'; |
| | | $params['dfilename'] = $value['name']; |
| | | $params['cid'] = $value['cid']; |
| | | $obj->addSubpart($value['body'], $params); |
| | | $ret = $obj->addSubpart($value['body'], $params); |
| | | return $ret; |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | function &_addAttachmentPart(&$obj, $value) |
| | | { |
| | | $params['content_type'] = $value['c_type']; |
| | | $params['encoding'] = $value['encoding']; |
| | | $params['disposition'] = 'attachment'; |
| | | $params['dfilename'] = $value['name']; |
| | | $obj->addSubpart($value['body'], $params); |
| | | $params['encoding'] = $value['encoding']; |
| | | if ($value['disposition'] != "inline") { |
| | | $fname = array("fname" => $value['name']); |
| | | $fname_enc = $this->_encodeHeaders($fname); |
| | | $params['dfilename'] = $fname_enc['fname']; |
| | | } |
| | | if ($value['charset']) { |
| | | $params['charset'] = $value['charset']; |
| | | } |
| | | $params['content_type'] = $value['c_type'] . '; ' . |
| | | 'name="' . $params['dfilename'] . '"'; |
| | | $params['disposition'] = isset($value['disposition']) ? |
| | | $value['disposition'] : 'attachment'; |
| | | $ret = $obj->addSubpart($value['body'], $params); |
| | | return $ret; |
| | | } |
| | | |
| | | /** |
| | | * Returns the complete e-mail, ready to send using an alternative |
| | | * mail delivery method. Note that only the mailpart that is made |
| | | * with Mail_Mime is created. This means that, |
| | | * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF |
| | | * using the $xtra_headers parameter! |
| | | * |
| | | * @param string $separation The separation etween these two parts. |
| | | * @param array $build_params The Build parameters passed to the |
| | | * &get() function. See &get for more info. |
| | | * @param array $xtra_headers The extra headers that should be passed |
| | | * to the &headers() function. |
| | | * See that function for more info. |
| | | * @param bool $overwrite Overwrite the existing headers with new. |
| | | * @return string The complete e-mail. |
| | | * @access public |
| | | */ |
| | | function getMessage($separation = null, $build_params = null, $xtra_headers = null, $overwrite = false) |
| | | { |
| | | if ($separation === null) |
| | | { |
| | | $separation = MAIL_MIME_CRLF; |
| | | } |
| | | $body = $this->get($build_params); |
| | | $head = $this->txtHeaders($xtra_headers, $overwrite); |
| | | $mail = $head . $separation . $body; |
| | | return $mail; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Builds the multipart message from the list ($this->_parts) and |
| | |
| | | * |
| | | * @param array Build parameters that change the way the email |
| | | * is built. Should be associative. Can contain: |
| | | * head_encoding - What encoding to use for the headers. |
| | | * Options: quoted-printable or base64 |
| | | * Default is quoted-printable |
| | | * text_encoding - What encoding to use for plain text |
| | | * Options: 7bit, 8bit, base64, or quoted-printable |
| | | * Default is 7bit |
| | | * html_encoding - What encoding to use for html |
| | | * Options: 7bit, 8bit, base64, or quoted-printable |
| | | * Default is quoted-printable |
| | | * 7bit_wrap - Number of characters before text is |
| | | * wrapped in 7bit encoding |
| | |
| | | } |
| | | |
| | | if (!empty($this->_html_images) AND isset($this->_htmlbody)) { |
| | | foreach ($this->_html_images as $value) { |
| | | $regex = '#src\s*=\s*(["\']?)' . preg_quote($value['name']) . |
| | | '(["\'])?#'; |
| | | $rep = 'src=\1cid:' . $value['cid'] .'\2'; |
| | | foreach ($this->_html_images as $key => $value) { |
| | | $regex = array(); |
| | | $regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . |
| | | preg_quote($value['name'], '#') . '\3#'; |
| | | $regex[] = '#(?i)url(?-i)\(\s*(["\']?)' . |
| | | preg_quote($value['name'], '#') . '\1\s*\)#'; |
| | | $rep = array(); |
| | | $rep[] = '\1\2=\3cid:' . $value['cid'] .'\3'; |
| | | $rep[] = 'url(\1cid:' . $value['cid'] . '\2)'; |
| | | $this->_htmlbody = preg_replace($regex, $rep, |
| | | $this->_htmlbody |
| | | ); |
| | | $this->_html_images[$key]['name'] = basename($this->_html_images[$key]['name']); |
| | | } |
| | | } |
| | | |
| | |
| | | $output = $message->encode(); |
| | | $this->_headers = array_merge($this->_headers, |
| | | $output['headers']); |
| | | return $output['body']; |
| | | $body = $output['body']; |
| | | return $body; |
| | | |
| | | } else { |
| | | return false; |
| | | $ret = false; |
| | | return $ret; |
| | | } |
| | | } |
| | | |
| | |
| | | * |
| | | * @param array $xtra_headers Assoc array with any extra headers. |
| | | * Optional. |
| | | * @param bool $overwrite Overwrite already existing headers. |
| | | * @return array Assoc array with the mime headers |
| | | * @access public |
| | | */ |
| | | function &headers($xtra_headers = null) |
| | | function &headers($xtra_headers = null, $overwrite = false) |
| | | { |
| | | // Content-Type header should already be present, |
| | | // So just add mime version header |
| | |
| | | if (isset($xtra_headers)) { |
| | | $headers = array_merge($headers, $xtra_headers); |
| | | } |
| | | $this->_headers = array_merge($headers, $this->_headers); |
| | | if ($overwrite){ |
| | | $this->_headers = array_merge($this->_headers, $headers); |
| | | }else{ |
| | | $this->_headers = array_merge($headers, $this->_headers); |
| | | } |
| | | |
| | | return $this->_encodeHeaders($this->_headers); |
| | | $encodedHeaders = $this->_encodeHeaders($this->_headers); |
| | | return $encodedHeaders; |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * @param array $xtra_headers Assoc array with any extra headers. |
| | | * Optional. |
| | | * @param bool $overwrite Overwrite the existing heaers with new. |
| | | * @return string Plain text headers |
| | | * @access public |
| | | */ |
| | | function txtHeaders($xtra_headers = null) |
| | | function txtHeaders($xtra_headers = null, $overwrite = false) |
| | | { |
| | | $headers = $this->headers($xtra_headers); |
| | | $headers = $this->headers($xtra_headers, $overwrite); |
| | | $ret = ''; |
| | | foreach ($headers as $key => $val) { |
| | | $ret .= "$key: $val" . MAIL_MIME_CRLF; |
| | |
| | | } |
| | | |
| | | /** |
| | | * Since the PHP send function requires you to specifiy |
| | | * recipients (To: header) separately from the other |
| | | * headers, the To: header is not properly encoded. |
| | | * To fix this, you can use this public method to |
| | | * encode your recipients before sending to the send |
| | | * function |
| | | * |
| | | * @param string $recipients A comma-delimited list of recipients |
| | | * @return string Encoded data |
| | | * @access public |
| | | */ |
| | | function encodeRecipients($recipients) |
| | | { |
| | | $input = array("To" => $recipients); |
| | | $retval = $this->_encodeHeaders($input); |
| | | return $retval["To"] ; |
| | | } |
| | | |
| | | /** |
| | | * Encodes a header as per RFC2047 |
| | | * |
| | | * @param string $input The header data to encode |
| | | * @return string Encoded data |
| | | * @param array $input The header data to encode |
| | | * @return array Encoded data |
| | | * @access private |
| | | */ |
| | | function _encodeHeaders($input) |
| | | { |
| | | $enc_prefix = '=?' . $this->_build_params['head_charset'] . '?Q?'; |
| | | $maxlen = 73; |
| | | foreach ($input as $hdr_name => $hdr_value) { |
| | | if (preg_match('/(\w*[\x80-\xFF]+\w*)/', $hdr_value)) { |
| | | $enc_value = preg_replace('/([\x80-\xFF])/e', '"=".strtoupper(dechex(ord("\1")))', $hdr_value); |
| | | // check for <email address> in string |
| | | if (preg_match('/<[a-z0-9\-\.\+\_]+@[a-z0-9]([a-z0-9\-].?)*[a-z0-9]\\.[a-z]{2,5}>/i', $enc_value) && ($p = strrpos($enc_value, '<'))) { |
| | | $hdr_value = $enc_prefix . substr($enc_value, 0, $p-1) . '?= ' . substr($enc_value, $p, strlen($enc_value)-$p); |
| | | } else { |
| | | $hdr_value = $enc_prefix . $enc_value . '?='; |
| | | // if header contains e-mail addresses |
| | | if (preg_match('/\s<.+@[a-z0-9\-\.]+\.[a-z]+>/U', $hdr_value)) |
| | | $chunks = $this->_explode_quoted_string(',', $hdr_value); |
| | | else |
| | | $chunks = array($hdr_value); |
| | | |
| | | $hdr_value = ''; |
| | | $line_len = 0; |
| | | |
| | | foreach ($chunks as $i => $value) { |
| | | $value = trim($value); |
| | | |
| | | //This header contains non ASCII chars and should be encoded. |
| | | if (preg_match('#[\x80-\xFF]{1}#', $value)) { |
| | | $suffix = ''; |
| | | // Don't encode e-mail address |
| | | if (preg_match('/(.+)\s(<.+@[a-z0-9\-\.]+>)$/Ui', $value, $matches)) { |
| | | $value = $matches[1]; |
| | | $suffix = ' '.$matches[2]; |
| | | } |
| | | |
| | | switch ($this->_build_params['head_encoding']) { |
| | | case 'base64': |
| | | // Base64 encoding has been selected. |
| | | $mode = 'B'; |
| | | $encoded = base64_encode($value); |
| | | break; |
| | | |
| | | case 'quoted-printable': |
| | | default: |
| | | // quoted-printable encoding has been selected |
| | | $mode = 'Q'; |
| | | $encoded = preg_replace('/([\x20-\x25\x2C\x80-\xFF])/e', "'='.sprintf('%02X', ord('\\1'))", $value); |
| | | // replace spaces with _ |
| | | $encoded = str_replace('=20', '_', $encoded); |
| | | } |
| | | |
| | | $value = '=?' . $this->_build_params['head_charset'] . '?' . $mode . '?' . $encoded . '?=' . $suffix; |
| | | } |
| | | |
| | | // add chunk to output string by regarding the header maxlen |
| | | $len = strlen($value); |
| | | if ($line_len + $len < $maxlen) { |
| | | $hdr_value .= ($i>0?', ':'') . $value; |
| | | $line_len += $len + ($i>0?2:0); |
| | | } |
| | | else { |
| | | $hdr_value .= ($i>0?', ':'') . "\n " . $value; |
| | | $line_len = $len; |
| | | } |
| | | } |
| | | |
| | | $input[$hdr_name] = $hdr_value; |
| | | } |
| | | |
| | | return $input; |
| | | } |
| | | |
| | | /* replaced 2005/07/08 by roundcube@gmail.com |
| | | |
| | | function _encodeHeaders_old($input) |
| | | |
| | | function _explode_quoted_string($delimiter, $string) |
| | | { |
| | | foreach ($input as $hdr_name => $hdr_value) { |
| | | preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches); |
| | | foreach ($matches[1] as $value) { |
| | | $replacement = preg_replace('/([\x80-\xFF])/e', |
| | | '"=" . |
| | | strtoupper(dechex(ord("\1")))', |
| | | $value); |
| | | $hdr_value = str_replace($value, '=?' . |
| | | $this->_build_params['head_charset'] . |
| | | '?Q?' . $replacement . '?=', |
| | | $hdr_value); |
| | | } |
| | | $input[$hdr_name] = $hdr_value; |
| | | } |
| | | $quotes = explode("\"", $string); |
| | | foreach ($quotes as $key => $val) |
| | | if (($key % 2) == 1) |
| | | $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]); |
| | | |
| | | return $input; |
| | | $string = implode("\"", $quotes); |
| | | |
| | | $result = explode($delimiter, $string); |
| | | foreach ($result as $key => $val) |
| | | $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]); |
| | | |
| | | return $result; |
| | | } |
| | | */ |
| | | |
| | | |
| | | /** |
| | | * Set the object's end-of-line and define the constant if applicable |
| | |
| | | |
| | | } // End of class |
| | | ?> |
| | | |