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.
14 #include <sys/types.h>
18 #include "fetchmail.h"
21 int SMTP_helo(FILE *sockfp,char *host)
22 /* send a "HELO" message to the SMTP listener */
26 SockPrintf(sockfp,"HELO %s\r\n", host);
27 if (outlevel == O_VERBOSE)
28 fprintf(stderr, "SMTP> HELO %s\n", host);
33 int SMTP_from(FILE *sockfp, char *from)
34 /* send a "MAIL FROM:" message to the SMTP listener */
38 SockPrintf(sockfp,"MAIL FROM:<%s>\r\n", from);
39 if (outlevel == O_VERBOSE)
40 fprintf(stderr, "SMTP> MAIL FROM:<%s>\n", from);
45 int SMTP_rcpt(FILE *sockfp, char *to)
46 /* send a "RCPT TO:" message to the SMTP listener */
50 SockPrintf(sockfp,"RCPT TO:<%s>\r\n", to);
51 if (outlevel == O_VERBOSE)
52 fprintf(stderr, "SMTP> RCPT TO:<%s>\n", to);
57 int SMTP_data(FILE *sockfp)
58 /* send a "DATA" message to the SMTP listener */
62 SockPrintf(sockfp,"DATA\r\n");
63 if (outlevel == O_VERBOSE)
64 fprintf(stderr, "SMTP> DATA\n");
69 int SMTP_quit(FILE *sockfp)
70 /* send a "QUIT" message to the SMTP listener */
74 SockPrintf(sockfp,"QUIT\r\n");
75 if (outlevel == O_VERBOSE)
76 fprintf(stderr, "SMTP> QUIT\n");
81 int SMTP_eom(FILE *sockfp)
82 /* send a message data terminator to the SMTP listener */
86 SockPrintf(sockfp,".\r\n");
87 if (outlevel == O_VERBOSE)
88 fprintf(stderr, "SMTP>. (EOM)\n");
93 void SMTP_rset(FILE *sockfp)
94 /* send a "RSET" message to the SMTP listener */
96 SockPrintf(sockfp,"RSET\r\n");
97 if (outlevel == O_VERBOSE)
98 fprintf(stderr, "SMTP> RSET\n");
102 static int SMTP_check(FILE *sockfp)
103 /* returns status of SMTP connection */
106 char buf[SMTPBUFSIZE];
108 while ((n = SockGets(buf, sizeof(buf)-1, sockfp)) > 0)
113 if (outlevel == O_VERBOSE)
114 fprintf(stderr, "SMTP< %s\n", buf);
115 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') && buf[3] == ' ')
117 else if (buf[3] != '-')
120 return SM_UNRECOVERABLE;
123 int SMTP_ok(FILE *sockfp)
124 /* accepts SMTP response, returns status of SMTP connection */
128 /* I can tell that the SMTP server connection is ok if I can read a
129 status message that starts with "1xx" ,"2xx" or "3xx".
130 Therefore, it can't be ok if there's no data waiting to be read
132 Tried to deal with this with a call to SockDataWaiting, but
137 ok = SMTP_check(sockfp);
138 if (ok == SM_ERROR) /* if we got an error, */
141 ok = SMTP_check(sockfp); /* how does it look now ? */
143 ok = SM_ERROR; /* It's just a simple error, for*/
144 /* the current message */
146 ok = SM_UNRECOVERABLE; /* if it still says error, we're */
152 /* smtp.c ends here */