2 * smtp.c -- code for speaking SMTP to a listener port
4 * Concept due to Harry Hochheiser. Implementation by ESR. Cleanup and
5 * strict RFC821 compliance by Cameron MacPherson.
7 * Copyright 1996 Eric S. Raymond
9 * For license terms, see the file COPYING in this directory.
16 #include "fetchmail.h"
26 static struct opt extensions[] =
28 {"8BITMIME", ESMTP_8BITMIME},
33 char smtp_response[MSGBUFSIZE];
35 int SMTP_helo(FILE *sockfp,char *host)
36 /* send a "HELO" message to the SMTP listener */
40 SockPrintf(sockfp,"HELO %s\r\n", host);
41 if (outlevel == O_VERBOSE)
42 error(0, 0, "SMTP> HELO %s", host);
47 int SMTP_ehlo(FILE *sockfp, char *host, int *opt)
48 /* send a "EHLO" message to the SMTP listener, return extension status bits */
54 SockPrintf(sockfp,"EHLO %s\r\n", host);
55 if (outlevel == O_VERBOSE)
56 error(0, 0, "SMTP> EHLO %s", host);
59 while ((ip = SockGets(smtp_response, sizeof(smtp_response)-1, sockfp)))
63 if (smtp_response[strlen(smtp_response)-1] == '\n')
64 smtp_response[strlen(smtp_response)-1] = '\0';
65 if (smtp_response[strlen(smtp_response)-1] == '\r')
66 smtp_response[strlen(smtp_response)-1] = '\r';
69 smtp_response[n] = '\0';
70 if (outlevel == O_VERBOSE)
71 error(0, 0, "SMTP< %s", smtp_response);
72 for (hp = extensions; hp->name; hp++)
73 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
75 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
77 else if (smtp_response[3] != '-')
80 return SM_UNRECOVERABLE;
83 int SMTP_from(FILE *sockfp, char *from, char *opts)
84 /* send a "MAIL FROM:" message to the SMTP listener */
90 sprintf(buf, "MAIL FROM:<%s>", from);
93 SockPrintf(sockfp,"%s\r\n", buf);
94 if (outlevel == O_VERBOSE)
95 error(0, 0, "SMTP> %s", buf);
100 int SMTP_rcpt(FILE *sockfp, char *to)
101 /* send a "RCPT TO:" message to the SMTP listener */
105 SockPrintf(sockfp,"RCPT TO:<%s>\r\n", to);
106 if (outlevel == O_VERBOSE)
107 error(0, 0, "SMTP> RCPT TO:<%s>", to);
108 ok = SMTP_ok(sockfp);
112 int SMTP_data(FILE *sockfp)
113 /* send a "DATA" message to the SMTP listener */
117 SockPrintf(sockfp,"DATA\r\n");
118 if (outlevel == O_VERBOSE)
119 error(0, 0, "SMTP> DATA");
120 ok = SMTP_ok(sockfp);
124 int SMTP_rset(FILE *sockfp)
125 /* send a "RSET" message to the SMTP listener */
129 SockPrintf(sockfp,"RSET\r\n");
130 if (outlevel == O_VERBOSE)
131 error(0, 0, "SMTP> RSET");
132 ok = SMTP_ok(sockfp);
136 int SMTP_quit(FILE *sockfp)
137 /* send a "QUIT" message to the SMTP listener */
141 SockPrintf(sockfp,"QUIT\r\n");
142 if (outlevel == O_VERBOSE)
143 error(0, 0, "SMTP> QUIT");
144 ok = SMTP_ok(sockfp);
148 int SMTP_eom(FILE *sockfp)
149 /* send a message data terminator to the SMTP listener */
153 SockPrintf(sockfp,".\r\n");
154 if (outlevel == O_VERBOSE)
155 error(0, 0, "SMTP>. (EOM)");
156 ok = SMTP_ok(sockfp);
160 int SMTP_ok(FILE *sockfp)
161 /* returns status of SMTP connection */
165 while ((ip = SockGets(smtp_response, sizeof(smtp_response)-1, sockfp)))
169 if (smtp_response[strlen(smtp_response)-1] == '\n')
170 smtp_response[strlen(smtp_response)-1] = '\0';
171 if (smtp_response[strlen(smtp_response)-1] == '\r')
172 smtp_response[strlen(smtp_response)-1] = '\r';
175 smtp_response[n] = '\0';
176 if (outlevel == O_VERBOSE)
177 error(0, 0, "SMTP< %s", smtp_response);
178 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
180 else if (smtp_response[3] != '-')
183 return SM_UNRECOVERABLE;
186 /* smtp.c ends here */