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(int sock,char *host)
37 /* send a "HELO" message to the SMTP listener */
41 SockPrintf(sock,"HELO %s\r\n", host);
42 if (outlevel == O_VERBOSE)
43 error(0, 0, "SMTP> HELO %s", host);
48 int SMTP_ehlo(int sock, char *host, int *opt)
49 /* send a "EHLO" message to the SMTP listener, return extension status bits */
53 SockPrintf(sock,"EHLO %s\r\n", host);
54 if (outlevel == O_VERBOSE)
55 error(0, 0, "SMTP> EHLO %s", host);
58 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
60 int n = strlen(smtp_response);
62 if (smtp_response[strlen(smtp_response)-1] == '\n')
63 smtp_response[strlen(smtp_response)-1] = '\0';
64 if (smtp_response[strlen(smtp_response)-1] == '\r')
65 smtp_response[strlen(smtp_response)-1] = '\r';
68 smtp_response[n] = '\0';
69 if (outlevel == O_VERBOSE)
70 error(0, 0, "SMTP< %s", smtp_response);
71 for (hp = extensions; hp->name; hp++)
72 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
74 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
76 else if (smtp_response[3] != '-')
79 return SM_UNRECOVERABLE;
82 int SMTP_from(int sock, char *from, char *opts)
83 /* send a "MAIL FROM:" message to the SMTP listener */
88 sprintf(buf, "MAIL FROM:<%s>", from);
91 SockPrintf(sock,"%s\r\n", buf);
92 if (outlevel == O_VERBOSE)
93 error(0, 0, "SMTP> %s", buf);
98 int SMTP_rcpt(int sock, char *to)
99 /* send a "RCPT TO:" message to the SMTP listener */
103 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
104 if (outlevel == O_VERBOSE)
105 error(0, 0, "SMTP> RCPT TO:<%s>", to);
110 int SMTP_data(int sock)
111 /* send a "DATA" message to the SMTP listener */
115 SockPrintf(sock,"DATA\r\n");
116 if (outlevel == O_VERBOSE)
117 error(0, 0, "SMTP> DATA");
122 int SMTP_rset(int sock)
123 /* send a "RSET" message to the SMTP listener */
127 SockPrintf(sock,"RSET\r\n");
128 if (outlevel == O_VERBOSE)
129 error(0, 0, "SMTP> RSET");
134 int SMTP_quit(int sock)
135 /* send a "QUIT" message to the SMTP listener */
139 SockPrintf(sock,"QUIT\r\n");
140 if (outlevel == O_VERBOSE)
141 error(0, 0, "SMTP> QUIT");
146 int SMTP_eom(int sock)
147 /* send a message data terminator to the SMTP listener */
151 SockPrintf(sock,".\r\n");
152 if (outlevel == O_VERBOSE)
153 error(0, 0, "SMTP>. (EOM)");
158 int SMTP_ok(int sock)
159 /* returns status of SMTP connection */
161 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
163 int n = strlen(smtp_response);
165 if (smtp_response[strlen(smtp_response)-1] == '\n')
166 smtp_response[strlen(smtp_response)-1] = '\0';
167 if (smtp_response[strlen(smtp_response)-1] == '\r')
168 smtp_response[strlen(smtp_response)-1] = '\r';
171 smtp_response[n] = '\0';
172 if (outlevel == O_VERBOSE)
173 error(0, 0, "SMTP< %s", smtp_response);
174 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
176 else if (smtp_response[3] != '-')
179 return SM_UNRECOVERABLE;
182 /* smtp.c ends here */