#include "ioloop.h"
#include "hostpid.h"
#include "istream.h"
+#include "ostream.h"
#include "str.h"
#include "str-sanitize.h"
#include "var-expand.h"
struct mail *mail = ctx->src_mail;
struct istream *input;
struct smtp_client *smtp_client;
- FILE *f;
+ struct ostream *output;
const char *return_addr, *hdr;
- const unsigned char *data;
const char *value, *msgid, *orig_msgid, *boundary;
string_t *str;
- size_t size;
int ret;
if (mail_get_first_header(mail, "Message-ID", &orig_msgid) < 0)
str_sanitize(reason, 512));
}
- smtp_client = smtp_client_open(ctx->set, return_addr, NULL, &f);
+ smtp_client = smtp_client_open(ctx->set, return_addr, NULL, &output);
msgid = mail_deliver_get_new_message_id(ctx);
boundary = t_strdup_printf("%s/%s", my_pid, ctx->set->hostname);
- fprintf(f, "Message-ID: %s\r\n", msgid);
- fprintf(f, "Date: %s\r\n", message_date_create(ioloop_time));
- fprintf(f, "From: Mail Delivery Subsystem <%s>\r\n",
- ctx->set->postmaster_address);
- fprintf(f, "To: <%s>\r\n", return_addr);
- fprintf(f, "MIME-Version: 1.0\r\n");
- fprintf(f, "Content-Type: "
- "multipart/report; report-type=%s;\r\n"
- "\tboundary=\"%s\"\r\n",
- ctx->dsn ? "delivery-status" : "disposition-notification",
- boundary);
-
- str = t_str_new(256);
+ str = t_str_new(512);
+ str_printfa(str, "Message-ID: %s\r\n", msgid);
+ str_printfa(str, "Date: %s\r\n", message_date_create(ioloop_time));
+ str_printfa(str, "From: Mail Delivery Subsystem <%s>\r\n",
+ ctx->set->postmaster_address);
+ str_printfa(str, "To: <%s>\r\n", return_addr);
+ str_append(str, "MIME-Version: 1.0\r\n");
+ str_printfa(str, "Content-Type: "
+ "multipart/report; report-type=%s;\r\n"
+ "\tboundary=\"%s\"\r\n",
+ ctx->dsn ? "delivery-status" : "disposition-notification",
+ boundary);
+ str_append(str, "Subject: ");
var_expand(str, ctx->set->rejection_subject,
get_var_expand_table(mail, reason, recipient));
- fprintf(f, "Subject: %s\r\n", str_c(str));
+ str_append(str, "\r\n");
- fprintf(f, "Auto-Submitted: auto-replied (rejected)\r\n");
- fprintf(f, "Precedence: bulk\r\n");
- fprintf(f, "\r\nThis is a MIME-encapsulated message\r\n\r\n");
+ str_append(str, "Auto-Submitted: auto-replied (rejected)\r\n");
+ str_append(str, "Precedence: bulk\r\n");
+ str_append(str, "\r\nThis is a MIME-encapsulated message\r\n\r\n");
/* human readable status report */
- fprintf(f, "--%s\r\n", boundary);
- fprintf(f, "Content-Type: text/plain; charset=utf-8\r\n");
- fprintf(f, "Content-Disposition: inline\r\n");
- fprintf(f, "Content-Transfer-Encoding: 8bit\r\n\r\n");
+ str_printfa(str, "--%s\r\n", boundary);
+ str_append(str, "Content-Type: text/plain; charset=utf-8\r\n");
+ str_append(str, "Content-Disposition: inline\r\n");
+ str_append(str, "Content-Transfer-Encoding: 8bit\r\n\r\n");
- str_truncate(str, 0);
var_expand(str, ctx->set->rejection_reason,
get_var_expand_table(mail, reason, recipient));
- fprintf(f, "%s\r\n", str_c(str));
+ str_append(str, "\r\n");
if (ctx->dsn) {
/* DSN status report: For LDA rejects. currently only used when
user is out of quota */
- fprintf(f, "--%s\r\n"
- "Content-Type: message/delivery-status\r\n\r\n",
- boundary);
- fprintf(f, "Reporting-MTA: dns; %s\r\n",
- ctx->set->hostname);
+ str_printfa(str, "--%s\r\n"
+ "Content-Type: message/delivery-status\r\n\r\n",
+ boundary);
+ str_printfa(str, "Reporting-MTA: dns; %s\r\n", ctx->set->hostname);
if (mail_get_first_header(mail, "Original-Recipient", &hdr) > 0)
- fprintf(f, "Original-Recipient: rfc822; %s\r\n", hdr);
- fprintf(f, "Final-Recipient: rfc822; %s\r\n", recipient);
- fprintf(f, "Action: failed\r\n");
- fprintf(f, "Status: %s\r\n", ctx->mailbox_full ? "5.2.2" : "5.2.0");
+ str_printfa(str, "Original-Recipient: rfc822; %s\r\n", hdr);
+ str_printfa(str, "Final-Recipient: rfc822; %s\r\n", recipient);
+ str_append(str, "Action: failed\r\n");
+ str_printfa(str, "Status: %s\r\n", ctx->mailbox_full ? "5.2.2" : "5.2.0");
} else {
/* MDN status report: For Sieve "reject" */
- fprintf(f, "--%s\r\n"
- "Content-Type: message/disposition-notification\r\n\r\n",
- boundary);
- fprintf(f, "Reporting-UA: %s; Dovecot Mail Delivery Agent\r\n",
- ctx->set->hostname);
+ str_printfa(str, "--%s\r\n"
+ "Content-Type: message/disposition-notification\r\n\r\n",
+ boundary);
+ str_printfa(str, "Reporting-UA: %s; Dovecot Mail Delivery Agent\r\n",
+ ctx->set->hostname);
if (mail_get_first_header(mail, "Original-Recipient", &hdr) > 0)
- fprintf(f, "Original-Recipient: rfc822; %s\r\n", hdr);
- fprintf(f, "Final-Recipient: rfc822; %s\r\n", recipient);
+ str_printfa(str, "Original-Recipient: rfc822; %s\r\n", hdr);
+ str_printfa(str, "Final-Recipient: rfc822; %s\r\n", recipient);
if (orig_msgid != NULL)
- fprintf(f, "Original-Message-ID: %s\r\n", orig_msgid);
- fprintf(f, "Disposition: "
- "automatic-action/MDN-sent-automatically; deleted\r\n");
+ str_printfa(str, "Original-Message-ID: %s\r\n", orig_msgid);
+ str_append(str, "Disposition: "
+ "automatic-action/MDN-sent-automatically; deleted\r\n");
}
- fprintf(f, "\r\n");
+ str_append(str, "\r\n");
/* original message's headers */
- fprintf(f, "--%s\r\nContent-Type: message/rfc822\r\n\r\n", boundary);
+ str_printfa(str, "--%s\r\nContent-Type: message/rfc822\r\n\r\n", boundary);
+ o_stream_send(output, str_data(str), str_len(str));
if (mail_get_hdr_stream(mail, NULL, &input) == 0) {
/* Note: If you add more headers, they need to be sorted.
N_ELEMENTS(exclude_headers),
null_header_filter_callback, NULL);
- while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
- if (fwrite(data, size, 1, f) == 0)
- break;
- i_stream_skip(input, size);
- }
+ ret = o_stream_send_istream(output, input);
i_stream_unref(&input);
i_assert(ret != 0);
}
- fprintf(f, "\r\n\r\n--%s--\r\n", boundary);
+ str_truncate(str, 0);
+ str_printfa(str, "\r\n\r\n--%s--\r\n", boundary);
+ o_stream_send(output, str_data(str), str_len(str));
return smtp_client_close(smtp_client);
}
"Return-Path"
};
struct istream *input;
+ struct ostream *output;
struct smtp_client *smtp_client;
- FILE *f;
- const unsigned char *data;
const char *return_path;
- size_t size;
if (mail_get_stream(ctx->src_mail, NULL, NULL, &input) < 0)
return -1;
forwardto, return_path);
}
- smtp_client = smtp_client_open(ctx->set, forwardto, return_path, &f);
+ smtp_client = smtp_client_open(ctx->set, forwardto, return_path, &output);
input = i_stream_create_header_filter(input, HEADER_FILTER_EXCLUDE |
HEADER_FILTER_NO_CR, hide_headers,
N_ELEMENTS(hide_headers),
null_header_filter_callback, NULL);
- while (i_stream_read_data(input, &data, &size, 0) > 0) {
- if (fwrite(data, size, 1, f) == 0)
- break;
- i_stream_skip(input, size);
- }
+ o_stream_send_istream(output, input);
i_stream_unref(&input);
return smtp_client_close(smtp_client);