1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-sendmail-transport.c: Sendmail-based transport class. */
6 * Authors: Dan Winship <danw@ximian.com>
8 * Copyright 2000 Ximian, Inc. (www.ximian.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU Lesser General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
36 #include <glib/gi18n-lib.h>
38 #include "camel-data-wrapper.h"
39 #include "camel-exception.h"
40 #include "camel-mime-filter-crlf.h"
41 #include "camel-mime-message.h"
42 #include "camel-sendmail-transport.h"
43 #include "camel-stream-filter.h"
44 #include "camel-stream-fs.h"
46 static char *get_name (CamelService *service, gboolean brief);
48 static gboolean sendmail_send_to (CamelTransport *transport,
49 CamelMimeMessage *message,
50 CamelAddress *from, CamelAddress *recipients,
55 camel_sendmail_transport_class_init (CamelSendmailTransportClass *camel_sendmail_transport_class)
57 CamelTransportClass *camel_transport_class =
58 CAMEL_TRANSPORT_CLASS (camel_sendmail_transport_class);
59 CamelServiceClass *camel_service_class =
60 CAMEL_SERVICE_CLASS (camel_sendmail_transport_class);
62 /* virtual method overload */
63 camel_service_class->get_name = get_name;
64 camel_transport_class->send_to = sendmail_send_to;
68 camel_sendmail_transport_get_type (void)
70 static CamelType camel_sendmail_transport_type = CAMEL_INVALID_TYPE;
72 if (camel_sendmail_transport_type == CAMEL_INVALID_TYPE) {
73 camel_sendmail_transport_type =
74 camel_type_register (CAMEL_TRANSPORT_TYPE, "CamelSendmailTransport",
75 sizeof (CamelSendmailTransport),
76 sizeof (CamelSendmailTransportClass),
77 (CamelObjectClassInitFunc) camel_sendmail_transport_class_init,
79 (CamelObjectInitFunc) NULL,
83 return camel_sendmail_transport_type;
88 sendmail_send_to (CamelTransport *transport, CamelMimeMessage *message,
89 CamelAddress *from, CamelAddress *recipients,
92 struct _camel_header_raw *header, *savedbcc, *n, *tail;
93 const char *from_addr, *addr, **argv;
94 int i, len, fd[2], nullfd, wstat;
95 CamelStreamFilter *filter;
96 CamelMimeFilter *crlf;
101 if (!camel_internet_address_get (CAMEL_INTERNET_ADDRESS (from), 0, NULL, &from_addr))
104 len = camel_address_length (recipients);
105 argv = g_malloc ((len + 6) * sizeof (char *));
106 argv[0] = "sendmail";
112 for (i = 0; i < len; i++) {
113 if (!camel_internet_address_get (CAMEL_INTERNET_ADDRESS (recipients), i, NULL, &addr)) {
114 camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
115 _("Could not parse recipient list"));
125 /* unlink the bcc headers */
127 tail = (struct _camel_header_raw *) &savedbcc;
129 header = (struct _camel_header_raw *) &CAMEL_MIME_PART (message)->headers;
132 if (!g_ascii_strcasecmp (n->name, "Bcc")) {
133 header->next = n->next;
144 if (pipe (fd) == -1) {
145 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
146 _("Could not create pipe to sendmail: "
147 "%s: mail not sent"),
150 /* restore the bcc headers */
151 header->next = savedbcc;
156 /* Block SIGCHLD so the calling application doesn't notice
157 * sendmail exiting before we do.
160 sigaddset (&mask, SIGCHLD);
161 sigprocmask (SIG_BLOCK, &mask, &omask);
166 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
167 _("Could not fork sendmail: "
168 "%s: mail not sent"),
172 sigprocmask (SIG_SETMASK, &omask, NULL);
175 /* restore the bcc headers */
176 header->next = savedbcc;
181 nullfd = open ("/dev/null", O_RDWR);
182 dup2 (fd[0], STDIN_FILENO);
183 /*dup2 (nullfd, STDOUT_FILENO);
184 dup2 (nullfd, STDERR_FILENO);*/
188 execv (SENDMAIL_PATH, (char **)argv);
193 /* Parent process. Write the message out. */
195 out = camel_stream_fs_new_with_fd (fd[1]);
197 /* workaround for lame sendmail implementations that can't handle CRLF eoln sequences */
198 filter = camel_stream_filter_new_with_stream (out);
199 crlf = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_DECODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY);
200 camel_stream_filter_add (filter, crlf);
201 camel_object_unref (crlf);
202 camel_object_unref (out);
204 out = (CamelStream *) filter;
205 if (camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), out) == -1
206 || camel_stream_close (out) == -1) {
207 camel_object_unref (CAMEL_OBJECT (out));
208 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
209 _("Could not send message: %s"),
212 /* Wait for sendmail to exit. */
213 while (waitpid (pid, &wstat, 0) == -1 && errno == EINTR)
216 sigprocmask (SIG_SETMASK, &omask, NULL);
218 /* restore the bcc headers */
219 header->next = savedbcc;
224 camel_object_unref (CAMEL_OBJECT (out));
226 /* Wait for sendmail to exit. */
227 while (waitpid (pid, &wstat, 0) == -1 && errno == EINTR)
230 sigprocmask (SIG_SETMASK, &omask, NULL);
232 /* restore the bcc headers */
233 header->next = savedbcc;
235 if (!WIFEXITED (wstat)) {
236 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
237 _("sendmail exited with signal %s: "
239 g_strsignal (WTERMSIG (wstat)));
241 } else if (WEXITSTATUS (wstat) != 0) {
242 if (WEXITSTATUS (wstat) == 255) {
243 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
244 _("Could not execute %s: "
248 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
249 _("sendmail exited with status "
250 "%d: mail not sent."),
251 WEXITSTATUS (wstat));
260 get_name (CamelService *service, gboolean brief)
263 return g_strdup (_("sendmail"));
265 return g_strdup (_("Mail delivery via the sendmail program"));