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},
34 char smtp_response[MSGBUFSIZE];
36 int SMTP_helo(FILE *sockfp,char *host)
37 /* send a "HELO" message to the SMTP listener */
41 SockPrintf(sockfp,"HELO %s\r\n", host);
42 if (outlevel == O_VERBOSE)
43 error(0, 0, "SMTP> HELO %s", host);
48 int SMTP_ehlo(FILE *sockfp, char *host, int *opt)
49 /* send a "EHLO" message to the SMTP listener, return extension status bits */
55 SockPrintf(sockfp,"EHLO %s\r\n", host);
56 if (outlevel == O_VERBOSE)
57 error(0, 0, "SMTP> EHLO %s", host);
60 while ((ip = SockGets(smtp_response, sizeof(smtp_response)-1, sockfp)))
64 if (smtp_response[strlen(smtp_response)-1] == '\n')
65 smtp_response[strlen(smtp_response)-1] = '\0';
66 if (smtp_response[strlen(smtp_response)-1] == '\r')
67 smtp_response[strlen(smtp_response)-1] = '\r';
70 smtp_response[n] = '\0';
71 if (outlevel == O_VERBOSE)
72 error(0, 0, "SMTP< %s", smtp_response);
73 for (hp = extensions; hp->name; hp++)
74 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
76 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
78 else if (smtp_response[3] != '-')
81 return SM_UNRECOVERABLE;
84 int SMTP_from(FILE *sockfp, char *from, char *opts)
85 /* send a "MAIL FROM:" message to the SMTP listener */
91 sprintf(buf, "MAIL FROM:<%s>", from);
94 SockPrintf(sockfp,"%s\r\n", buf);
95 if (outlevel == O_VERBOSE)
96 error(0, 0, "SMTP> %s", buf);
101 int SMTP_rcpt(FILE *sockfp, char *to)
102 /* send a "RCPT TO:" message to the SMTP listener */
106 SockPrintf(sockfp,"RCPT TO:<%s>\r\n", to);
107 if (outlevel == O_VERBOSE)
108 error(0, 0, "SMTP> RCPT TO:<%s>", to);
109 ok = SMTP_ok(sockfp);
113 int SMTP_data(FILE *sockfp)
114 /* send a "DATA" message to the SMTP listener */
118 SockPrintf(sockfp,"DATA\r\n");
119 if (outlevel == O_VERBOSE)
120 error(0, 0, "SMTP> DATA");
121 ok = SMTP_ok(sockfp);
125 int SMTP_rset(FILE *sockfp)
126 /* send a "RSET" message to the SMTP listener */
130 SockPrintf(sockfp,"RSET\r\n");
131 if (outlevel == O_VERBOSE)
132 error(0, 0, "SMTP> RSET");
133 ok = SMTP_ok(sockfp);
137 int SMTP_quit(FILE *sockfp)
138 /* send a "QUIT" message to the SMTP listener */
142 SockPrintf(sockfp,"QUIT\r\n");
143 if (outlevel == O_VERBOSE)
144 error(0, 0, "SMTP> QUIT");
145 ok = SMTP_ok(sockfp);
149 int SMTP_eom(FILE *sockfp)
150 /* send a message data terminator to the SMTP listener */
154 SockPrintf(sockfp,".\r\n");
155 if (outlevel == O_VERBOSE)
156 error(0, 0, "SMTP>. (EOM)");
157 ok = SMTP_ok(sockfp);
161 int SMTP_ok(FILE *sockfp)
162 /* returns status of SMTP connection */
166 while ((ip = SockGets(smtp_response, sizeof(smtp_response)-1, sockfp)))
170 if (smtp_response[strlen(smtp_response)-1] == '\n')
171 smtp_response[strlen(smtp_response)-1] = '\0';
172 if (smtp_response[strlen(smtp_response)-1] == '\r')
173 smtp_response[strlen(smtp_response)-1] = '\r';
176 smtp_response[n] = '\0';
177 if (outlevel == O_VERBOSE)
178 error(0, 0, "SMTP< %s", smtp_response);
179 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
181 else if (smtp_response[3] != '-')
184 return SM_UNRECOVERABLE;
187 /* smtp.c ends here */