2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "prefs_account.h"
38 #include "procheader.h"
43 static gint send_message_smtp (GSList *to_list, const gchar *from,
44 const gchar *server, gushort port,
45 const gchar *domain, const gchar *userid,
46 const gchar *passwd, gboolean use_smtp_auth,
48 static SockInfo *send_smtp_open (const gchar *server, gushort port,
49 const gchar *domain, gboolean use_smtp_auth);
50 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
53 #define SEND_EXIT_IF_ERROR(f, s) \
56 log_warning("Error occurred while %s\n", s); \
57 sock_close(smtp_sock); \
63 #define SEND_EXIT_IF_NOTOK(f, s) \
66 log_warning("Error occurred while %s\n", s); \
67 if (smtp_quit(smtp_sock) != SM_OK) \
68 log_warning("Error occurred while sending QUIT\n"); \
69 sock_close(smtp_sock); \
75 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
82 g_return_val_if_fail(file != NULL, -1);
83 g_return_val_if_fail(ac_prefs != NULL, -1);
84 g_return_val_if_fail(to_list != NULL, -1);
86 if ((fp = fopen(file, "r")) == NULL) {
87 FILE_OP_ERROR(file, "fopen");
91 if (ac_prefs->protocol == A_LOCAL_CMD)
93 val = send_message_with_command(to_list,
94 ac_prefs->mail_command,
99 port = ac_prefs->set_smtpport
100 ? ac_prefs->smtpport : SMTP_PORT;
101 domain = ac_prefs->set_domain
102 ? ac_prefs->domain : NULL;
104 val = send_message_smtp(to_list, ac_prefs->address,
105 ac_prefs->smtp_server, port,
106 domain, ac_prefs->userid,
108 ac_prefs->use_smtp_auth, fp);
122 static gint send_message_with_command(GSList *to_list, gchar * mailcmd,
131 len = strlen(mailcmd);
132 for (cur = to_list; cur != NULL; cur = cur->next)
133 len += strlen((gchar *)cur->data) + 1;
135 cmdline = g_new(gchar, len + 1);
136 strcpy(cmdline, mailcmd);
138 for (cur = to_list; cur != NULL; cur = cur->next)
140 cmdline = strncat(cmdline, " ", len);
141 cmdline = strncat(cmdline, (gchar *)cur->data, len);
144 log_message(_("Using command to send mail: %s ...\n"), cmdline);
146 p = popen(cmdline, "w");
149 while (fgets(buf, sizeof(buf), fp) != NULL) {
152 /* escape when a dot appears on the top */
162 log_message(_("Mail sent successfully ...\n"));
167 gint send_message_queue(const gchar *file)
169 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
170 {"SSV:", NULL, FALSE},
172 {NULL, NULL, FALSE}};
176 gchar *server = NULL;
177 GSList *to_list = NULL;
181 g_return_val_if_fail(file != NULL, -1);
183 if ((fp = fopen(file, "r")) == NULL) {
184 FILE_OP_ERROR(file, "fopen");
188 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
190 gchar *p = buf + strlen(qentry[hnum].name);
194 if (!from) from = g_strdup(p);
197 if (!server) server = g_strdup(p);
200 to_list = address_list_append(to_list, p);
206 if (!to_list || !from || !server) {
207 g_warning(_("Queued message header is broken.\n"));
214 ac = account_find_from_smtp_server(from, server);
216 g_warning(_("Account not found. Using current account...\n"));
221 port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
222 domain = ac->set_domain ? ac->domain : NULL;
223 val = send_message_smtp
224 (to_list, from, server, port, domain,
225 ac->userid, ac->passwd, ac->use_smtp_auth, fp);
227 g_warning(_("Account not found.\n"));
228 val = send_message_smtp
229 (to_list, from, server, SMTP_PORT, NULL,
230 NULL, NULL, FALSE, fp);
234 slist_free_strings(to_list);
235 g_slist_free(to_list);
243 static gint send_message_smtp(GSList *to_list, const gchar *from,
244 const gchar *server, gushort port,
245 const gchar *domain, const gchar *userid,
246 const gchar* passwd, gboolean use_smtp_auth,
253 g_return_val_if_fail(to_list != NULL, -1);
254 g_return_val_if_fail(from != NULL, -1);
255 g_return_val_if_fail(server != NULL, -1);
256 g_return_val_if_fail(fp != NULL, -1);
258 log_message(_("Connecting to SMTP server: %s ...\n"), server);
260 SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
261 (server, port, domain, use_smtp_auth)),
262 "connecting to server");
264 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
265 "sending MAIL FROM");
266 for (cur = to_list; cur != NULL; cur = cur->next)
267 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
269 SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
272 while (fgets(buf, sizeof(buf), fp) != NULL) {
275 /* escape when a dot appears on the top */
277 SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
280 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
283 SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
284 SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
286 sock_close(smtp_sock);
290 static SockInfo *send_smtp_open(const gchar *server, gushort port,
291 const gchar *domain, gboolean use_smtp_auth)
296 g_return_val_if_fail(server != NULL, NULL);
298 if ((sock = sock_connect(server, port)) == NULL) {
299 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
304 if (smtp_ok(sock) == SM_OK) {
305 val = smtp_helo(sock, domain ? domain : get_domain_name(),
307 if (val == SM_OK) return sock;
310 log_warning(_("Error occurred while sending HELO\n"));