From 14766bcdd0f0f678a5aa6c28c0f692a791ccf5cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=82phane=20Veyret?= <sveyret@axway.com>
Date: Thu, 10 Feb 2011 13:54:37 +0100
Subject: [PATCH] 0012382: Long emails aren't sent and make Mantis freeze

---
 config_defaults_inc.php |    6 ++++++
 core/email_api.php      |   24 +++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/config_defaults_inc.php b/config_defaults_inc.php
index 8406f9e..47f6182 100644
--- a/config_defaults_inc.php
+++ b/config_defaults_inc.php
@@ -483,6 +483,12 @@
 	$g_phpMailer_method		= PHPMAILER_METHOD_MAIL;
 
 	/**
+	 * select the e-mail max length (in x1024 characters). Length is unlimited if set to 0.
+	 * @global int $g_email_max_length
+	 */
+	$g_email_max_length = 0;
+
+	/**
 	 * This option allows you to use a remote SMTP host.  Must use the phpMailer script
 	 * One or more hosts, separated by a semicolon, can be listed.
 	 * You can also specify a different port for each host by using this
diff --git a/core/email_api.php b/core/email_api.php
index 5fa2606..69cd167 100644
--- a/core/email_api.php
+++ b/core/email_api.php
@@ -871,7 +871,7 @@ function email_send( $p_email_data ) {
 
 	$t_recipient = trim( $t_email_data->email );
 	$t_subject = string_email( trim( $t_email_data->subject ) );
-	$t_message = string_email_links( trim( $t_email_data->body ) );
+	$t_message = string_email_links( cut_long_email( trim( $t_email_data->body ) ) );
 
 	$t_debug_email = config_get( 'debug_email' );
 	$t_mailer_method = config_get( 'phpMailer_method' );
@@ -1014,6 +1014,28 @@ function email_send( $p_email_data ) {
 }
 
 /**
+ * cut overlengthed emails
+ * 
+ * @param string 
+ * @return string 
+ */
+function cut_long_email( $p_string ) {
+	$t_max_len = config_get( 'email_max_length' ) * 1024;
+	$t_string = $p_string;
+	$t_cut_end = "\n\n(...)";
+	if( $t_max_len > 0 && strlen( $t_string ) > $t_max_len ) {
+		$t_string = substr( $t_string, 0, $t_max_len - strlen( $t_cut_end ) );
+		$t_pos = strrpos( $t_string, "\n" );
+		if($t_pos !== false && $t_pos > 0) {
+			# Clean cut at end of line
+			$t_string = substr( $t_string, 0, $t_pos );
+		}
+		$t_string .= $t_cut_end;
+	}
+	return $t_string;
+}
+
+/**
  * closes opened kept alive SMTP connection (if it was opened)
  * 
  * @param string 
-- 
1.7.1

