diff --git a/src/lib-sieve/sieve-smtp.c b/src/lib-sieve/sieve-smtp.c
index a4b7cdc633d950647731b335b7376250917bcafd..28923100cc5e12913518ebc329e50aa8061230b5 100644
--- a/src/lib-sieve/sieve-smtp.c
+++ b/src/lib-sieve/sieve-smtp.c
@@ -42,7 +42,7 @@ void sieve_smtp_add_rcpt
 (struct sieve_smtp_context *sctx, const char *address)
 {
 	i_assert(!sctx->sent);
-	sctx->senv->smtp_add_rcpt(sctx->handle, address);
+	sctx->senv->smtp_add_rcpt(sctx->senv, sctx->handle, address);
 }
 
 struct ostream *sieve_smtp_send
@@ -51,7 +51,7 @@ struct ostream *sieve_smtp_send
 	i_assert(!sctx->sent);
 	sctx->sent = TRUE;
 
-	return sctx->senv->smtp_send(sctx->handle);
+	return sctx->senv->smtp_send(sctx->senv, sctx->handle);
 }
 
 struct sieve_smtp_context *sieve_smtp_start_single
@@ -74,6 +74,6 @@ int sieve_smtp_finish
 	void *handle = sctx->handle;
 
 	i_free(sctx);
-	return senv->smtp_finish(handle, error_r);
+	return senv->smtp_finish(senv, handle, error_r);
 }
 
diff --git a/src/lib-sieve/sieve-types.h b/src/lib-sieve/sieve-types.h
index ddcebca7dadf774f190d1972e7f78b5b88c619e0..0b6b803c196595bd4bf2e0e907d12c579f397e66 100644
--- a/src/lib-sieve/sieve-types.h
+++ b/src/lib-sieve/sieve-types.h
@@ -193,12 +193,17 @@ struct sieve_script_env {
 	void *(*smtp_start)
 		(const struct sieve_script_env *senv, const char *return_path);
 	/* Add a new recipient */
-	void (*smtp_add_rcpt)	(void *handle, const char *address);
+	void (*smtp_add_rcpt)	
+		(const struct sieve_script_env *senv, void *handle,
+			const char *address);
 	/* Get an output stream where the message can be written to. The recipients
 	   must already be added before calling this. */
-	struct ostream *(*smtp_send)(void *handle);
+	struct ostream *(*smtp_send)
+		(const struct sieve_script_env *senv, void *handle);
 	/* Returns 1 on success, 0 on permanent failure, -1 on temporary failure. */
-	int (*smtp_finish)(void *handle, const char **error_r);
+	int (*smtp_finish)
+		(const struct sieve_script_env *senv, void *handle,
+			const char **error_r);
 
 	/* Interface for marking and checking duplicates */
 	int (*duplicate_check)
diff --git a/src/plugins/lda-sieve/lda-sieve-plugin.c b/src/plugins/lda-sieve/lda-sieve-plugin.c
index 52023a35ba1196d610d20755027bfcc289bbc68b..1187a40f1ec1f2fb7132079d422c8e60d04b1ee0 100644
--- a/src/plugins/lda-sieve/lda-sieve-plugin.c
+++ b/src/plugins/lda-sieve/lda-sieve-plugin.c
@@ -77,14 +77,17 @@ static void *lda_sieve_smtp_start
 	return (void *)smtp_client_init(dctx->set, return_path);
 }
 
-static void lda_sieve_smtp_add_rcpt(void *handle, const char *address)
+static void lda_sieve_smtp_add_rcpt
+(const struct sieve_script_env *senv ATTR_UNUSED, void *handle,
+	const char *address)
 {
 	struct smtp_client *smtp_client = (struct smtp_client *) handle;
 
 	smtp_client_add_rcpt(smtp_client, address);
 }
 
-static struct ostream *lda_sieve_smtp_send(void *handle)
+static struct ostream *lda_sieve_smtp_send
+(const struct sieve_script_env *senv ATTR_UNUSED, void *handle)
 {
 	struct smtp_client *smtp_client = (struct smtp_client *) handle;
 
@@ -92,11 +95,15 @@ static struct ostream *lda_sieve_smtp_send(void *handle)
 }
 
 static int lda_sieve_smtp_finish
-(void *handle, const char **error_r)
+(const struct sieve_script_env *senv, void *handle,
+	const char **error_r)
 {
+	struct mail_deliver_context *dctx =
+		(struct mail_deliver_context *) senv->script_context;
 	struct smtp_client *smtp_client = (struct smtp_client *) handle;
 
-	return smtp_client_deinit(smtp_client, error_r);
+	return smtp_client_deinit_timeout
+		(smtp_client, dctx->timeout_secs, error_r);
 }
 
 static int lda_sieve_reject_mail
diff --git a/src/sieve-tools/sieve-test.c b/src/sieve-tools/sieve-test.c
index 84e02aca7dec28600226c506d39ca148065cbbe7..09a08715bc1cfca5c2517a905111e4deb79b664b 100644
--- a/src/sieve-tools/sieve-test.c
+++ b/src/sieve-tools/sieve-test.c
@@ -70,12 +70,15 @@ static void *sieve_smtp_start
 }
 
 static void sieve_smtp_add_rcpt
-(void *handle ATTR_UNUSED, const char *address)
+(const struct sieve_script_env *senv ATTR_UNUSED,
+	void *handle ATTR_UNUSED, const char *address)
 {
 	printf("\nRECIPIENT: %s\n", address);
 }
 
-static struct ostream *sieve_smtp_send(void *handle)
+static struct ostream *sieve_smtp_send
+(const struct sieve_script_env *senv ATTR_UNUSED,
+	void *handle)
 {
 	printf("START MESSAGE:\n");
 
@@ -83,7 +86,8 @@ static struct ostream *sieve_smtp_send(void *handle)
 }
 
 static int sieve_smtp_finish
-(void *handle, const char **error_r ATTR_UNUSED)
+(const struct sieve_script_env *senv ATTR_UNUSED,
+	void *handle, const char **error_r ATTR_UNUSED)
 {
 	struct ostream *output = (struct ostream *)handle;
 
diff --git a/src/testsuite/testsuite-smtp.c b/src/testsuite/testsuite-smtp.c
index 6696f711fff690b64996c70229186319e5948be2..e8fee726009fb237a0e4efb79fa0ebe48e960b9a 100644
--- a/src/testsuite/testsuite-smtp.c
+++ b/src/testsuite/testsuite-smtp.c
@@ -74,12 +74,6 @@ struct testsuite_smtp {
 	struct ostream *output;
 };
 
-void testsuite_smtp_add_rcpt(void *handle, const char *address);
-struct ostream *testsuite_smtp_send(void *handle);
-int testsuite_smtp_finish
-	(void *handle, const char **error_r);
-
-
 void *testsuite_smtp_start
 (const struct sieve_script_env *senv ATTR_UNUSED, const char *return_path)
 {
@@ -102,7 +96,9 @@ void *testsuite_smtp_start
 	return (void *) smtp;
 }
 
-void testsuite_smtp_add_rcpt(void *handle, const char *address)
+void testsuite_smtp_add_rcpt
+(const struct sieve_script_env *senv ATTR_UNUSED,
+	void *handle, const char *address)
 {
 	struct testsuite_smtp *smtp = (struct testsuite_smtp *) handle;
 	struct testsuite_smtp_message *msg;
@@ -114,7 +110,8 @@ void testsuite_smtp_add_rcpt(void *handle, const char *address)
 	msg->envelope_to = p_strdup(testsuite_smtp_pool, address);
 }
 
-struct ostream *testsuite_smtp_send(void *handle)
+struct ostream *testsuite_smtp_send
+(const struct sieve_script_env *senv ATTR_UNUSED, void *handle)
 {
 	struct testsuite_smtp *smtp = (struct testsuite_smtp *) handle;
 
@@ -122,7 +119,8 @@ struct ostream *testsuite_smtp_send(void *handle)
 }
 
 int testsuite_smtp_finish
-(void *handle,	const char **error_r ATTR_UNUSED)
+(const struct sieve_script_env *senv ATTR_UNUSED,
+	void *handle, const char **error_r ATTR_UNUSED)
 {
 	struct testsuite_smtp *smtp = (struct testsuite_smtp *) handle;
 
diff --git a/src/testsuite/testsuite-smtp.h b/src/testsuite/testsuite-smtp.h
index 62b472528040bf5573b28c17445c4838499fa194..dd0bf73031fb6202c9e3f96435ea292eaf460848 100644
--- a/src/testsuite/testsuite-smtp.h
+++ b/src/testsuite/testsuite-smtp.h
@@ -15,10 +15,15 @@ void testsuite_smtp_reset(void);
 void *testsuite_smtp_start
 	(const struct sieve_script_env *senv ATTR_UNUSED,
 		const char *return_path);
-void testsuite_smtp_add_rcpt(void *handle, const char *address);
-struct ostream *testsuite_smtp_send(void *handle);
+void testsuite_smtp_add_rcpt
+	(const struct sieve_script_env *senv ATTR_UNUSED,
+		void *handle, const char *address);
+struct ostream *testsuite_smtp_send
+	(const struct sieve_script_env *senv ATTR_UNUSED,
+		void *handle);
 int testsuite_smtp_finish
-	(void *handle, const char **error_r);
+	(const struct sieve_script_env *senv ATTR_UNUSED,
+		void *handle, const char **error_r);
 
 /*
  * Access