Example #1
0
 /**
  * Wrap the given text to comply with RFC 2646
  *
  * @param string $text Text to wrap
  * @param int $length Length
  *
  * @return string Wrapped text
  */
 public static function format_flowed($text, $length = 72)
 {
     $text = preg_split('/\\r?\\n/', $text);
     foreach ($text as $idx => $line) {
         if ($line != '-- ') {
             if ($line[0] == '>' && preg_match('/^(>+)/', $line, $regs)) {
                 $prefix = $regs[0];
                 $level = strlen($prefix);
                 $line = rtrim(substr($line, $level));
                 $line = $prefix . rc_wordwrap($line, $length - $level - 2, " \r\n{$prefix} ");
             } else {
                 if ($line) {
                     $line = rc_wordwrap(rtrim($line), $length - 2, " \r\n");
                     // space-stuffing
                     $line = preg_replace('/(^|\\r\\n)(From| |>)/', '\\1 \\2', $line);
                 }
             }
             $text[$idx] = $line;
         }
     }
     return implode("\r\n", $text);
 }
	private function send_html_and_text_email($to, $from, $subject, $body) {
		$rcmail = rcmail::get_instance();

		$ctb = md5(rand() . microtime());
		$headers  = "Return-Path: $from\r\n";
		$headers .= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: multipart/alternative; boundary=\"=_$ctb\"\r\n";
		$headers .= "Date: " . date('r', time()) . "\r\n";
		$headers .= "From: $from\r\n";
		$headers .= "To: $to\r\n";
		$headers .= "Subject: $subject\r\n";
		$headers .= "Reply-To: $from\r\n";

		$msg_body .= "Content-Type: multipart/alternative; boundary=\"=_$ctb\"\r\n\r\n";

		$txt_body  = "--=_$ctb";
		$txt_body .= "\r\n";
		$txt_body .= "Content-Transfer-Encoding: 7bit\r\n";
		$txt_body .= "Content-Type: text/plain; charset=" . RCMAIL_CHARSET . "\r\n";
		$LINE_LENGTH = $rcmail->config->get('line_length', 75);
		$h2t = new html2text($body, false, true, 0);
		$txt = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n");
		$txt = wordwrap($txt, 998, "\r\n", true);
		$txt_body .= "$txt\r\n";
		$txt_body .= "--=_$ctb";
		$txt_body .= "\r\n";

		$msg_body .= $txt_body;

		$msg_body .= "Content-Transfer-Encoding: quoted-printable\r\n";
		$msg_body .= "Content-Type: text/html; charset=" . RCMAIL_CHARSET . "\r\n\r\n";
		$msg_body .= str_replace("=","=3D",$body);
		$msg_body .= "\r\n\r\n";
		$msg_body .= "--=_$ctb--";
		$msg_body .= "\r\n\r\n";

		// send message
		if (!is_object($rcmail->smtp)) {
			$rcmail->smtp_init(true);
		}

		if($rcmail->config->get('smtp_pass') == "%p") {
			$rcmail->config->set('smtp_server', $rcmail->config->get('default_smtp_server'));
			$rcmail->config->set('smtp_user', $rcmail->config->get('default_smtp_user'));
			$rcmail->config->set('smtp_pass', $rcmail->config->get('default_smtp_pass'));
		}

		$rcmail->smtp->connect();
		if($rcmail->smtp->send_mail($from, $to, $headers, $msg_body)) {
			return true;
		}
		else {
			write_log('errors','response:' . print_r($rcmail->smtp->get_response(),true));
			write_log('errors','errors:' . print_r($rcmail->smtp->get_error(),true));
			return false;
		}
	}