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 1997 Eric S. Raymond
8 * For license terms, see the file COPYING in this directory.
15 #include "fetchmail.h"
25 static struct opt extensions[] =
27 {"8BITMIME", ESMTP_8BITMIME},
33 char smtp_response[MSGBUFSIZE];
35 int SMTP_helo(int sock,char *host)
36 /* send a "HELO" message to the SMTP listener */
40 SockPrintf(sock,"HELO %s\r\n", host);
41 if (outlevel == O_VERBOSE)
42 error(0, 0, "SMTP> HELO %s", host);
47 int SMTP_ehlo(int sock, char *host, int *opt)
48 /* send a "EHLO" message to the SMTP listener, return extension status bits */
52 SockPrintf(sock,"EHLO %s\r\n", host);
53 if (outlevel == O_VERBOSE)
54 error(0, 0, "SMTP> EHLO %s", host);
57 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
59 int n = strlen(smtp_response);
61 if (smtp_response[strlen(smtp_response)-1] == '\n')
62 smtp_response[strlen(smtp_response)-1] = '\0';
63 if (smtp_response[strlen(smtp_response)-1] == '\r')
64 smtp_response[strlen(smtp_response)-1] = '\0';
67 smtp_response[n] = '\0';
68 if (outlevel == O_VERBOSE)
69 error(0, 0, "SMTP< %s", smtp_response);
70 for (hp = extensions; hp->name; hp++)
71 if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name)))
73 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
75 else if (smtp_response[3] != '-')
78 return SM_UNRECOVERABLE;
81 int SMTP_from(int sock, char *from, char *opts)
82 /* send a "MAIL FROM:" message to the SMTP listener */
87 sprintf(buf, "MAIL FROM:<%s>", from);
90 SockPrintf(sock,"%s\r\n", buf);
91 if (outlevel == O_VERBOSE)
92 error(0, 0, "SMTP> %s", buf);
97 int SMTP_rcpt(int sock, char *to)
98 /* send a "RCPT TO:" message to the SMTP listener */
102 SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
103 if (outlevel == O_VERBOSE)
104 error(0, 0, "SMTP> RCPT TO:<%s>", to);
109 int SMTP_data(int sock)
110 /* send a "DATA" message to the SMTP listener */
114 SockPrintf(sock,"DATA\r\n");
115 if (outlevel == O_VERBOSE)
116 error(0, 0, "SMTP> DATA");
121 int SMTP_rset(int sock)
122 /* send a "RSET" message to the SMTP listener */
126 SockPrintf(sock,"RSET\r\n");
127 if (outlevel == O_VERBOSE)
128 error(0, 0, "SMTP> RSET");
133 int SMTP_quit(int sock)
134 /* send a "QUIT" message to the SMTP listener */
138 SockPrintf(sock,"QUIT\r\n");
139 if (outlevel == O_VERBOSE)
140 error(0, 0, "SMTP> QUIT");
145 int SMTP_eom(int sock)
146 /* send a message data terminator to the SMTP listener */
150 SockPrintf(sock,".\r\n");
151 if (outlevel == O_VERBOSE)
152 error(0, 0, "SMTP>. (EOM)");
157 int SMTP_ok(int sock)
158 /* returns status of SMTP connection */
160 while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
162 int n = strlen(smtp_response);
164 if (smtp_response[strlen(smtp_response)-1] == '\n')
165 smtp_response[strlen(smtp_response)-1] = '\0';
166 if (smtp_response[strlen(smtp_response)-1] == '\r')
167 smtp_response[strlen(smtp_response)-1] = '\r';
170 smtp_response[n] = '\0';
171 if (outlevel == O_VERBOSE)
172 error(0, 0, "SMTP< %s", smtp_response);
173 if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ')
175 else if (smtp_response[3] != '-')
178 return SM_UNRECOVERABLE;
181 /* smtp.c ends here */