--- dovecot-sieve-1.0.1-org/src/sieve-cmu.c	2006-10-10 22:44:54.000000000 +0300
+++ dovecot-sieve-1.0.1-new/src/sieve-cmu.c	2007-07-06 19:05:29.000000000 +0300
@@ -18,9 +18,12 @@
 
 #include <fcntl.h>
 #include <unistd.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 
+static int contains_8bit(const char *msg);
+
 /* data per script */
 typedef struct script_data {
 	const char *username;
@@ -252,30 +255,84 @@
     return SIEVE_OK;
 }
 
-static int sieve_notify(void *ac __attr_unused__,
-			void *interp_context __attr_unused__,
-			void *script_context __attr_unused__,
-			void *mc __attr_unused__,
-			const char **errmsg __attr_unused__)
-{
-#if 0
-    const char *notifier = config_getstring(IMAPOPT_SIEVENOTIFIER);
-
-    if (notifier) {
-	sieve_notify_context_t *nc = (sieve_notify_context_t *) ac;
-	script_data_t *sd = (script_data_t *) script_context;
-	int nopt = 0;
-
-	/* count options */
-	while (nc->options[nopt]) nopt++;
-
-	/* "default" is a magic value that implies the default */
-	notify(!strcmp("default",nc->method) ? notifier : nc->method,
-	       "SIEVE", nc->priority, sd->username, NULL,
-	       nopt, nc->options, nc->message);
+static int sieve_notify(void *ac,
+			void *ic __attr_unused__,
+			void *sc __attr_unused__,
+			void *mc,
+			const char **errmsg)
+{
+    sieve_notify_context_t *nc = (sieve_notify_context_t *) ac;
+    sieve_msgdata_t *m = mc;
+
+    int nopt = 0;
+    FILE *f;
+    struct smtp_client *smtp_client;
+    const char *outmsgid;
+
+    /* "default" is "mailto" as only one... */
+    if (!strcasecmp(nc->method, "default")) nc->method = "mailto";
+    /* check method */
+    if (strcasecmp(nc->method, "mailto")) { 
+        *errmsg = "Unknown [unimplemented] notify method";
+        i_info("SIEVE ERROR: Unknown [unimplemented] notify method <%s>", 
+	nc->method);
+        //return SIEVE_FAIL;
+	//just log error, failed notify is not reason to abort all script.
+	//it is not delivery error too... 
+	return SIEVE_OK;
+    }    
+    /* count options */
+    while (nc->options[nopt]) {
+	smtp_client = smtp_client_open(nc->options[nopt], NULL, &f);
+	outmsgid = deliver_get_new_message_id();
+	fprintf(f, "Message-ID: %s\r\n", outmsgid);
+	fprintf(f, "Date: %s\r\n", message_date_create(ioloop_time));
+	fprintf(f, "X-Sieve: %s\r\n", SIEVE_VERSION);
+	if ( strcasecmp(nc->priority, "high") == 0 ) {
+            fprintf(f, "X-Priority: 1 (Highest)\r\n");
+	    fprintf(f, "Importance: High\r\n");
+        } else if ( strcasecmp(nc->priority, "normal") == 0 ) {
+            fprintf(f, "X-Priority: 3 (Normal)\r\n");
+	    fprintf(f, "Importance: Normal\r\n");
+	} else if ( strcasecmp(nc->priority, "low") == 0 ) {
+	    fprintf(f, "X-Priority: 5 (Lowest)\r\n");
+	    fprintf(f, "Importance: Low\r\n");
+	// RFC: If no importance is given, the default value is "2 (Normal)"
+	} else {
+	    fprintf(f, "X-Priority: 3 (Normal)\r\n");
+	    fprintf(f, "Importance: Normal\r\n");
+	} 
+	fprintf(f, "From: Postmaster <%s>\r\n", getenv("POSTMASTER_ADDRESS"));
+	fprintf(f, "To: <%s>\r\n", nc->options[nopt]);
+	fprintf(f, "Subject: [SIEVE] New mail notification\r\n");
+        fprintf(f, "Auto-Submitted: auto-generated (notify)\r\n");
+	fprintf(f, "Precedence: bulk\r\n");
+        if (contains_8bit(nc->message)) {
+            fprintf(f, "MIME-Version: 1.0\r\n");
+	    fprintf(f, "Content-Type: text/plain; charset=UTF-8\r\n");
+	    fprintf(f, "Content-Transfer-Encoding: 8bit\r\n");
+	}
+	fprintf(f, "\r\n");
+	fprintf(f, "%s\r\n", nc->message);
+	if (smtp_client_close(smtp_client) == 0) {
+ 		i_info("msgid=%s: sent notification to <%s> (method=%s)", 
+		m->id == NULL ? "" : str_sanitize(m->id, 80),
+	        str_sanitize(nc->options[nopt], 80),
+		nc->method
+		);
+	} else {
+ 		i_info("msgid=%s: ERROR sending notification to <%s> (method=%s)", 
+		m->id == NULL ? "" : str_sanitize(m->id, 80),
+	        str_sanitize(nc->options[nopt], 80),
+		nc->method);
+		*errmsg = "Error sending notify mail";
+		//return SIEVE_FAIL;
+		//just log error, failed notify is not reson to abort all script...
+		//it is not delivery error too... 
+	}
+	nopt = nopt + 1;
     }
-#endif
-    return SIEVE_FAIL;
+    return SIEVE_OK;
 }
 
 static int autorespond(void *ac, 
@@ -567,12 +624,30 @@
 
 	if ((ret = sieve_script_load(compiled_path, &bytecode)) != SIEVE_OK) {
 		i_error("sieve_script_load(%s) failed: %d", compiled_path, ret);
-		return -1;
+		return 0;
 	}
 
 	if (sieve_execute_bytecode(bytecode, interp,
-				   &sdata, &mdata) != SIEVE_OK)
-		return -1;
-
+				   &sdata, &mdata) != SIEVE_OK) {
+		i_error("sieve_execute_bytecode(%s) failed; script aborted", compiled_path);
+		return 0;
+		}
 	return 1;
 }
+
+static int contains_8bit(const char * msg)
+{
+    int res = 0;
+        if (msg) {
+            const unsigned char *s = (const unsigned char *)msg;
+            while (*s) {
+                if (0 != (*s & 0x80)) {
+                    res = 1;
+                    break ;
+                }
+                s++;
+            }
+        }
+    return res;
+}
+
