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.
17 #include "fetchmail.h"
20 int SMTP_helo(FILE *sockfp,char *host)
21 /* send a "HELO" message to the SMTP listener */
25 fprintf(sockfp,"HELO %s\r\n", host);
26 if (outlevel == O_VERBOSE)
27 fprintf(stderr, "SMTP> HELO %s\n", host);
28 ok = SMTP_ok(sockfp,NULL);
32 int SMTP_from(FILE *sockfp, char *from)
33 /* send a "MAIL FROM:" message to the SMTP listener */
37 fprintf(sockfp,"MAIL FROM:<%s>\r\n", from);
38 if (outlevel == O_VERBOSE)
39 fprintf(stderr, "SMTP> MAIL FROM:<%s>\n", from);
40 ok = SMTP_ok(sockfp,NULL);
44 int SMTP_rcpt(FILE *sockfp, char *to)
45 /* send a "RCPT TO:" message to the SMTP listener */
49 fprintf(sockfp,"RCPT TO:<%s>\r\n", to);
50 if (outlevel == O_VERBOSE)
51 fprintf(stderr, "SMTP> RCPT TO:<%s>\n", to);
52 ok = SMTP_ok(sockfp,NULL);
56 int SMTP_data(FILE *sockfp)
57 /* send a "DATA" message to the SMTP listener */
61 fprintf(sockfp,"DATA\r\n");
62 if (outlevel == O_VERBOSE)
63 fprintf(stderr, "SMTP> DATA\n");
64 ok = SMTP_ok(sockfp,NULL);
68 int SMTP_quit(FILE *sockfp)
69 /* send a "QUIT" message to the SMTP listener */
73 fprintf(sockfp,"QUIT\r\n");
74 if (outlevel == O_VERBOSE)
75 fprintf(stderr, "SMTP> QUIT\n");
76 ok = SMTP_ok(sockfp,NULL);
80 int SMTP_eom(FILE *sockfp)
81 /* send a message data terminator to the SMTP listener */
85 fprintf(sockfp,".\r\n");
86 if (outlevel == O_VERBOSE)
87 fprintf(stderr, "SMTP>. (EOM)\n");
88 ok = SMTP_ok(sockfp,NULL);
92 void SMTP_rset(FILE *sockfp)
93 /* send a "RSET" message to the SMTP listener */
95 fprintf(sockfp,"RSET\r\n");
96 if (outlevel == O_VERBOSE)
97 fprintf(stderr, "SMTP> RSET\n");
100 static int SMTP_check(FILE *sockfp,char *argbuf)
101 /* returns status of SMTP connection */
104 char buf[SMTPBUFSIZE];
106 if ((ok = SockGets(buf, sizeof(buf)-1, sockfp)) > 0) {
108 if (outlevel == O_VERBOSE)
109 fprintf(stderr, "SMTP< %s\n", buf);
112 if (buf[0] == '1' || buf[0] == '2' || buf[0] == '3')
118 ok = SM_UNRECOVERABLE;
122 int SMTP_ok(FILE *sockfp,char *argbuf)
123 /* accepts SMTP response, returns status of SMTP connection */
127 /* I can tell that the SMTP server connection is ok if I can read a
128 status message that starts with "1xx" ,"2xx" or "3xx".
129 Therefore, it can't be ok if there's no data waiting to be read
131 Tried to deal with this with a call to SockDataWaiting, but
136 ok = SMTP_check(sockfp,argbuf);
137 if (ok == SM_ERROR) /* if we got an error, */
140 ok = SMTP_check(sockfp,argbuf); /* how does it look now ? */
142 ok = SM_ERROR; /* It's just a simple error, for*/
143 /* the current message */
145 ok = SM_UNRECOVERABLE; /* if it still says error, we're */
151 /* smtp.c ends here */