diff --git a/configure.in b/configure.in
index b53021447f2f3f77de42dd5ec6ce4c4686a17766..0f5a11a03cb4e9115b9ee6b8edfebbad907e3f8b 100644
--- a/configure.in
+++ b/configure.in
@@ -72,6 +72,7 @@ src/lib-sieve/plugins/variables/Makefile
 src/plugins/Makefile
 src/plugins/lda-sieve/Makefile
 src/sieve-bin/Makefile
+src/testsuite/Makefile
 stamp.h])
 
 AC_OUTPUT
diff --git a/src/testsuite/Makefile.am b/src/testsuite/Makefile.am
index 826cb8141ae0991c4a800687f420e6f65b8a872f..3a5a894512ed28f8296887b94cb4a2d8149a5ad7 100644
--- a/src/testsuite/Makefile.am
+++ b/src/testsuite/Makefile.am
@@ -43,6 +43,7 @@ cmds = \
 testsuite_SOURCES = \
 	namespaces.c \
 	mail-raw.c \
+	testsuite-common.c \
 	$(cmds) \
 	ext-testsuite.c \
 	testsuite.c
diff --git a/src/testsuite/cmd-test-message.c b/src/testsuite/cmd-test-message.c
index b8178407ff1fc7edb0548d2b727b67a3e0bee05d..7dd35f1690a5f1bbe62db6e04dd751ed334ced03 100644
--- a/src/testsuite/cmd-test-message.c
+++ b/src/testsuite/cmd-test-message.c
@@ -125,6 +125,8 @@ static bool cmd_test_message_operation_execute
 
 	printf(">> TEST MESSAGE \"%s\"\n", str_c(message));
 	
+	testsuite_message_set(message);
+	
 	t_pop();
 	
 	return TRUE;
diff --git a/src/testsuite/mail-raw.c b/src/testsuite/mail-raw.c
index 269e01e873fa4f712b62435f17c898aa4b16b580..beb6b8fdc48a05dc9df1edc704cd2b9afcd87dfe 100644
--- a/src/testsuite/mail-raw.c
+++ b/src/testsuite/mail-raw.c
@@ -70,8 +70,9 @@ struct mail_raw *mail_raw_open(string_t *mail_data)
 		i_fatal("Can't sync raw mail: %s",
 		mail_storage_get_last_error(raw_ns->storage, &error));
 	}
-    raw_box = (struct raw_mailbox *) mailr->box;
-    raw_box->envelope_sender = DEFAULT_ENVELOPE_SENDER;
+  
+	raw_box = (struct raw_mailbox *) mailr->box;
+	raw_box->envelope_sender = DEFAULT_ENVELOPE_SENDER;
 
 	mailr->trans = mailbox_transaction_begin(mailr->box, 0);
 	mailr->mail = mail_alloc(mailr->trans, 0, NULL);
diff --git a/src/testsuite/tests/testsuite.sieve b/src/testsuite/tests/testsuite.sieve
index 4d05ffc3a7c0f850eeee61cbde86c25d8e1b736d..2551a6bfe4ae7094d18402c33199343c4a4ce3c3 100644
--- a/src/testsuite/tests/testsuite.sieve
+++ b/src/testsuite/tests/testsuite.sieve
@@ -9,4 +9,25 @@ Frop!
 .
 ;
 
+if not header :contains "from" "rename-it.nl" {
+	discard;
+	stop;
+}
+
+test_message text:
+From: nico@vestingbar.nl
+To: stephan@zuiphol.nl
+Subject: Friep!
+
+Friep!
+.
+;
+
+if not header :is "from" "nico@vestingbar.nl" {
+    discard;
+    stop;
+} 
+
 keep;
+
+
diff --git a/src/testsuite/testsuite b/src/testsuite/testsuite
index 8839e4a4592d78ab5f1d65331925e8cb2b79c8b7..e7b483718ea82d1e6db31a98b3c915c4398c8fcb 100755
Binary files a/src/testsuite/testsuite and b/src/testsuite/testsuite differ
diff --git a/src/testsuite/testsuite-common.c b/src/testsuite/testsuite-common.c
new file mode 100644
index 0000000000000000000000000000000000000000..ca1eed6152abfab121eebc541b7b87620cd72c96
--- /dev/null
+++ b/src/testsuite/testsuite-common.c
@@ -0,0 +1,90 @@
+#include "lib.h"
+#include "string.h"
+#include "ostream.h"
+#include "mail-storage.h"
+
+#include "mail-raw.h"
+#include "namespaces.h"
+#include "sieve.h"
+
+#include "testsuite-common.h"
+
+/* Message environment */
+
+struct sieve_message_data testsuite_msgdata;
+
+static const char *testsuite_user;
+static struct mail_raw *_raw_message;
+
+/* Testsuite message environment */
+
+static const char *_default_message_data = 
+"From: stephan@rename-it.nl\n"
+"To: sirius@drunksnipers.com\n"
+"Subject: Frop!\n"
+"\n"
+"Friep!\n";
+
+static void _testsuite_message_set(string_t *message)
+{
+	struct mail *mail;
+	const char *recipient = NULL, *sender = NULL;
+	
+	/*
+	 * Open message as mail struct
+	 */
+	 
+	_raw_message = mail_raw_open(message);
+	mail = _raw_message->mail;
+
+	/* 
+	 * Collect necessary message data 
+	 */
+	 
+	/* Get recipient address */ 
+	(void)mail_get_first_header(mail, "Envelope-To", &recipient);
+	if ( recipient == NULL ) 
+		(void)mail_get_first_header(mail, "To", &recipient);
+	if ( recipient == NULL ) 
+		recipient = "recipient@example.com";
+	
+	/* Get sender address */
+	(void)mail_get_first_header(mail, "Return-path", &sender);
+	if ( sender == NULL ) 
+		(void)mail_get_first_header(mail, "Sender", &sender);
+	if ( sender == NULL ) 
+		(void)mail_get_first_header(mail, "From", &sender);
+	if ( sender == NULL ) 
+		sender = "sender@example.com";
+
+	memset(&testsuite_msgdata, 0, sizeof(testsuite_msgdata));
+	testsuite_msgdata.mail = mail;
+	testsuite_msgdata.auth_user = testsuite_user;
+	testsuite_msgdata.return_path = sender;
+	testsuite_msgdata.to_address = recipient;
+
+	(void)mail_get_first_header(mail, "Message-ID", &testsuite_msgdata.id);
+}
+
+void testsuite_message_init(pool_t namespaces_pool, const char *user)
+{		
+	string_t *default_message = t_str_new(1024);
+	str_append(default_message, _default_message_data);
+
+	testsuite_user = user;
+	mail_raw_init(namespaces_pool, user);
+	_testsuite_message_set(default_message);
+}
+
+void testsuite_message_set(string_t *message)
+{
+	mail_raw_close(_raw_message);
+
+	_testsuite_message_set(message);	
+}
+
+void testsuite_message_deinit(void)
+{
+	mail_raw_close(_raw_message);
+	mail_raw_deinit();
+}
diff --git a/src/testsuite/testsuite-common.h b/src/testsuite/testsuite-common.h
index 5cc7e5a66add76e18cbf46a928afe070ffbd439b..41c148eb1ba7ad03b634fad721711b7ced57fd41 100644
--- a/src/testsuite/testsuite-common.h
+++ b/src/testsuite/testsuite-common.h
@@ -5,4 +5,13 @@ extern const struct sieve_extension testsuite_extension;
 
 extern int ext_testsuite_my_id;
 
+/* Testsuite message environment */
+
+extern struct sieve_message_data testsuite_msgdata;
+
+void testsuite_message_init(pool_t namespaces_pool, const char *user);
+void testsuite_message_deinit(void);
+
+void testsuite_message_set(string_t *message);
+
 #endif
diff --git a/src/testsuite/testsuite.c b/src/testsuite/testsuite.c
index d2ac044ad5fde5d85a22e5247e5e2a0045615192..a0c4a5ab716a63f511ac54860224a68b97e86433 100644
--- a/src/testsuite/testsuite.c
+++ b/src/testsuite/testsuite.c
@@ -1,22 +1,5 @@
 /* Copyright (c) 2005-2007 Dovecot authors, see the included COPYING file */
 
-#include "lib.h"
-#include "ostream.h"
-#include "mail-storage.h"
-
-#include "mail-raw.h"
-#include "namespaces.h"
-#include "sieve.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <pwd.h>
-
-#define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"
-#define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
-
 #include "lib.h"
 #include "lib-signals.h"
 #include "ioloop.h"
@@ -24,6 +7,8 @@
 #include "hostpid.h"
 #include "mail-storage.h"
 
+#include "mail-raw.h"
+#include "namespaces.h"
 #include "sieve.h"
 #include "sieve-extensions.h"
 
@@ -35,7 +20,8 @@
 #include <fcntl.h>
 #include <pwd.h>
 
-/* Functionality common to all sieve test binaries */
+#define DEFAULT_SENDMAIL_PATH "/usr/lib/sendmail"
+#define DEFAULT_ENVELOPE_SENDER "MAILER-DAEMON"
 
 /* FIXME: this file is currently very messy */
 
@@ -137,28 +123,6 @@ static void _dump_sieve_binary_to(struct sieve_binary *sbin, const char *filenam
 		close(dfd);
 }
 
-static void _fill_in_envelope
-	(struct mail *mail, const char **recipient, const char **sender)
-{
-	/* Get recipient address */
-	if ( *recipient == NULL ) 
-		(void)mail_get_first_header(mail, "Envelope-To", recipient);
-	if ( *recipient == NULL ) 
-		(void)mail_get_first_header(mail, "To", recipient);
-	if ( *recipient == NULL ) 
-		*recipient = "recipient@example.com";
-	
-	/* Get sender address */
-	if ( *sender == NULL ) 
-		(void)mail_get_first_header(mail, "Return-path", sender);
-	if ( *sender == NULL ) 
-		(void)mail_get_first_header(mail, "Sender", sender);
-	if ( *sender == NULL ) 
-		(void)mail_get_first_header(mail, "From", sender);
-	if ( *sender == NULL ) 
-		*sender = "sender@example.com";
-}
-
 static void print_help(void)
 {
 	printf(
@@ -166,33 +130,20 @@ static void print_help(void)
 	);
 }
 
-static const char *message_data = 
-"From: stephan@rename-it.nl\n"
-"To: sirius@drunksnipers.com\n"
-"Subject: Frop!\n"
-"\n"
-"Friep!\n";
-
 int main(int argc, char **argv) 
 {
-	const char *scriptfile, *dumpfile, *recipient, *sender; 
+	const char *scriptfile, *dumpfile; 
 	const char *user;
 	int i;
 	pool_t namespaces_pool;
-	struct mail_raw *mailr;
 	struct sieve_binary *sbin;
-	struct sieve_message_data msgdata;
 	struct sieve_script_env scriptenv;
 	struct sieve_error_handler *ehandler;
-	string_t *mail_buffer;
 
 	testsuite_init();
 
-	mail_buffer = t_str_new(1024);
-	str_append(mail_buffer, message_data);
-
 	/* Parse arguments */
-	scriptfile = dumpfile = recipient = sender = NULL;
+	scriptfile = dumpfile =  NULL;
 	for (i = 1; i < argc; i++) {
 		if (strcmp(argv[i], "-d") == 0) {
 			/* dump file */
@@ -219,21 +170,9 @@ int main(int argc, char **argv)
 	/* Dump script */
 	_dump_sieve_binary_to(sbin, dumpfile);
 	
-	user = _get_user();
-
 	namespaces_pool = namespaces_init();
-	mail_raw_init(namespaces_pool, user);
-	mailr = mail_raw_open(mail_buffer);
-
-	_fill_in_envelope(mailr->mail, &recipient, &sender);
-
-	/* Collect necessary message data */
-	memset(&msgdata, 0, sizeof(msgdata));
-	msgdata.mail = mailr->mail;
-	msgdata.return_path = recipient;
-	msgdata.to_address = sender;
-	msgdata.auth_user = user;
-	(void)mail_get_first_header(mailr->mail, "Message-ID", &msgdata.id);
+	user = _get_user();
+	testsuite_message_init(namespaces_pool, user);
 
 	memset(&scriptenv, 0, sizeof(scriptenv));
 	scriptenv.inbox = "INBOX";
@@ -242,13 +181,12 @@ int main(int argc, char **argv)
 	ehandler = sieve_stderr_ehandler_create();	
 	
 	/* Run the test */
-	(void) sieve_test(sbin, &msgdata, &scriptenv, ehandler);
+	(void) sieve_test(sbin, &testsuite_msgdata, &scriptenv, ehandler);
 
 	sieve_close(&sbin);
 	sieve_error_handler_unref(&ehandler);
 
-	mail_raw_close(mailr);
-	mail_raw_deinit();
+	testsuite_message_deinit();
 	namespaces_deinit();
 
 	testsuite_deinit();