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 gint send_smtp_open (const gchar *server, gushort port,
49 const gchar *domain, gboolean use_smtp_auth);
51 #define SEND_EXIT_IF_ERROR(f, s) \
54 log_warning("Error occurred while %s\n", s); \
55 if (smtp_sock > 0) sock_close(smtp_sock); \
60 #define SEND_EXIT_IF_NOTOK(f, s) \
63 log_warning("Error occurred while %s\n", s); \
64 if (smtp_quit(smtp_sock) != SM_OK) \
65 log_warning("Error occurred while sending QUIT\n"); \
66 sock_close(smtp_sock); \
71 gint send_message(const gchar *file, PrefsAccount *ac_prefs, GSList *to_list)
78 g_return_val_if_fail(file != NULL, -1);
79 g_return_val_if_fail(ac_prefs != NULL, -1);
80 g_return_val_if_fail(to_list != NULL, -1);
82 if ((fp = fopen(file, "r")) == NULL) {
83 FILE_OP_ERROR(file, "fopen");
87 port = ac_prefs->set_smtpport ? ac_prefs->smtpport : SMTP_PORT;
88 domain = ac_prefs->set_domain ? ac_prefs->domain : NULL;
90 val = send_message_smtp(to_list, ac_prefs->address,
91 ac_prefs->smtp_server, port, domain,
92 ac_prefs->userid, ac_prefs->passwd,
93 ac_prefs->use_smtp_auth, fp);
106 gint send_message_queue(const gchar *file)
108 static HeaderEntry qentry[] = {{"S:", NULL, FALSE},
109 {"SSV:", NULL, FALSE},
111 {NULL, NULL, FALSE}};
115 gchar *server = NULL;
116 GSList *to_list = NULL;
120 g_return_val_if_fail(file != NULL, -1);
122 if ((fp = fopen(file, "r")) == NULL) {
123 FILE_OP_ERROR(file, "fopen");
127 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry))
129 gchar *p = buf + strlen(qentry[hnum].name);
133 if (!from) from = g_strdup(p);
136 if (!server) server = g_strdup(p);
139 to_list = address_list_append(to_list, p);
145 if (!to_list || !from || !server) {
146 g_warning(_("Queued message header is broken.\n"));
153 ac = account_find_from_smtp_server(from, server);
155 g_warning(_("Account not found. Using current account...\n"));
160 port = ac->set_smtpport ? ac->smtpport : SMTP_PORT;
161 domain = ac->set_domain ? ac->domain : NULL;
162 val = send_message_smtp
163 (to_list, from, server, port, domain,
164 ac->userid, ac->passwd, ac->use_smtp_auth, fp);
166 g_warning(_("Account not found.\n"));
167 val = send_message_smtp
168 (to_list, from, server, SMTP_PORT, NULL,
169 NULL, NULL, FALSE, fp);
173 slist_free_strings(to_list);
174 g_slist_free(to_list);
182 static gint send_message_smtp(GSList *to_list, const gchar *from,
183 const gchar *server, gushort port,
184 const gchar *domain, const gchar *userid,
185 const gchar* passwd, gboolean use_smtp_auth,
192 g_return_val_if_fail(to_list != NULL, -1);
193 g_return_val_if_fail(from != NULL, -1);
194 g_return_val_if_fail(server != NULL, -1);
195 g_return_val_if_fail(fp != NULL, -1);
197 log_message(_("Connecting to SMTP server: %s ...\n"), server);
199 SEND_EXIT_IF_ERROR((smtp_sock = send_smtp_open
200 (server, port, domain, use_smtp_auth)),
201 "connecting to server");
203 (smtp_from(smtp_sock, from, userid, passwd, use_smtp_auth),
204 "sending MAIL FROM");
205 for (cur = to_list; cur != NULL; cur = cur->next)
206 SEND_EXIT_IF_NOTOK(smtp_rcpt(smtp_sock, (gchar *)cur->data),
208 SEND_EXIT_IF_NOTOK(smtp_data(smtp_sock), "sending DATA");
211 while (fgets(buf, sizeof(buf), fp) != NULL) {
214 /* escape when a dot appears on the top */
216 SEND_EXIT_IF_ERROR(sock_write(smtp_sock, ".", 1),
219 SEND_EXIT_IF_ERROR(sock_puts(smtp_sock, buf), "sending data");
222 SEND_EXIT_IF_NOTOK(smtp_eom(smtp_sock), "terminating data");
223 SEND_EXIT_IF_NOTOK(smtp_quit(smtp_sock), "sending QUIT");
225 sock_close(smtp_sock);
229 static gint send_smtp_open(const gchar *server, gushort port,
230 const gchar *domain, gboolean use_smtp_auth)
236 g_return_val_if_fail(server != NULL, -1);
238 if ((sockinfo = sock_connect(server, port)) == NULL) {
239 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
243 sock = sockinfo->sock;
244 sock_sockinfo_free(sockinfo);
246 if (smtp_ok(sock) == SM_OK) {
247 val = smtp_helo(sock, domain ? domain : get_domain_name(),
249 if (val == SM_OK) return sock;
252 log_warning(_("Error occurred while sending HELO\n"));