| version 1.1, 2002/09/29 22:37:19 |
version 1.2, 2002/09/30 07:55:48 |
| Line 40 unsigned long __smtpTimeout = 30; |
|
| Line 40 unsigned long __smtpTimeout = 30; |
|
| unsigned char __smtpTimedOut = 0; /* 0==FALSE 1==TRUE */ |
unsigned char __smtpTimedOut = 0; /* 0==FALSE 1==TRUE */ |
| |
|
| #define __SMTP_ALARM_SET \ |
#define __SMTP_ALARM_SET \ |
| signal(SIGALRM,(void*)smtp_alarm); \ |
signal(SIGALRM, (void *) smtp_alarm); \ |
| (void)alarm(__smtpTimeout); |
(void) alarm(__smtpTimeout); |
| #define __SMTP_ALARM_CLEAR \ |
#define __SMTP_ALARM_CLEAR \ |
| signal(SIGALRM,SIG_DFL); \ |
signal(SIGALRM, SIG_DFL); \ |
| (void)alarm(0); |
(void) alarm(0); |
| |
|
| int smtp_clear_buf(int nSocket); |
int smtp_clear_buf(int nSocket); |
| void smtp_alarm(void); |
void *smtp_alarm(int unused); |
| |
|
| int smtp_connect(const char* zServer, int nPort) |
int smtp_connect(const char* zServer, int nPort) |
| { |
{ |
| Line 64 int smtp_connect(const char* zServer, in |
|
| Line 64 int smtp_connect(const char* zServer, in |
|
| with the server */ |
with the server */ |
| |
|
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Server is \"%s\", port |
printf("libsmtp : smtp_connect : Server is \"%s\", port %i\n", |
| %i\n",zServer,nPort); |
zServer,nPort); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Lookup the hostname of the server */ |
/* Lookup the hostname of the server */ |
| Line 93 int smtp_connect(const char* zServer, in |
|
| Line 93 int smtp_connect(const char* zServer, in |
|
| &(sSocketAddr.sin_addr))==0) |
&(sSocketAddr.sin_addr))==0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Connect failed at |
printf("libsmtp : smtp_connect : Connect failed at inet_ntoa()\n"); |
| inet_ntoa()\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_CONNECT_FAILED); |
return(-SMTP_CONNECT_FAILED); |
| } |
} |
| Line 108 int smtp_connect(const char* zServer, in |
|
| Line 107 int smtp_connect(const char* zServer, in |
|
| sizeof(struct sockaddr))==-1) |
sizeof(struct sockaddr))==-1) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Connect failed at |
printf("libsmtp : smtp_connect : Connect failed at connect()\n"); |
| connect()\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_CONNECT_FAILED); |
return(-SMTP_CONNECT_FAILED); |
| } |
} |
| Line 131 int smtp_connect(const char* zServer, in |
|
| Line 129 int smtp_connect(const char* zServer, in |
|
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Server did not respond |
printf("libsmtp : smtp_connect : Server did not respond correctly!\n"); |
| correctly!\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_CONNECT_FAILED); |
return(-SMTP_CONNECT_FAILED); |
| } |
} |
| Line 143 int smtp_connect(const char* zServer, in |
|
| Line 140 int smtp_connect(const char* zServer, in |
|
| if(strcmp(nRecvBuf,"220")!=0) |
if(strcmp(nRecvBuf,"220")!=0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Server responded |
printf("libsmtp : smtp_connect : Server responded %s\n", |
| %s\n",nRecvBuf); |
nRecvBuf); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_CONNECT_FAILED); |
return(-SMTP_CONNECT_FAILED); |
| } |
} |
| Line 187 int smtp_connect(const char* zServer, in |
|
| Line 184 int smtp_connect(const char* zServer, in |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : Recieved less than 3 |
printf("libsmtp : smtp_connect : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 203 int smtp_connect(const char* zServer, in |
|
| Line 199 int smtp_connect(const char* zServer, in |
|
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_connect : HELO failed\n"); |
printf("libsmtp : smtp_connect : HELO failed\n"); |
| printf("libsmtp : smtp_connect : Server responded |
printf("libsmtp : smtp_connect : Server responded %s\n", |
| %s\n",nRecvBuf); |
nRecvBuf); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_CONNECT_FAILED); |
return(-SMTP_CONNECT_FAILED); |
| } |
} |
| Line 227 int smtp_sendmail(const char* zTo, const |
|
| Line 223 int smtp_sendmail(const char* zTo, const |
|
| { |
{ |
| /* Send the mail zBody FROM: zFrom, TO: zTo */ |
/* Send the mail zBody FROM: zFrom, TO: zTo */ |
| |
|
| int nLength; /* Used to calculate line |
int nLength; /* Used to calculate line |
| lengths etc */ |
lengths etc */ |
| int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ |
int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ |
| char nRecvBuf[3]; /* Return code recieve buffer |
char nRecvBuf[3]; /* Return code recieve buffer |
| */ |
*/ |
| char *nFromMsg, *nRcptMsg; /* Mail from & rcpt to messages */ |
char *nFromMsg, *nRcptMsg; /* Mail from & rcpt to messages */ |
| |
|
| Line 254 int smtp_sendmail(const char* zTo, const |
|
| Line 250 int smtp_sendmail(const char* zTo, const |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Recieved less than 3 |
printf("libsmtp : smtp_sendmail : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 263 int smtp_sendmail(const char* zTo, const |
|
| Line 258 int smtp_sendmail(const char* zTo, const |
|
| if(__smtpTimedOut==1) |
if(__smtpTimedOut==1) |
| return(-SMTP_TIMED_OUT); |
return(-SMTP_TIMED_OUT); |
| |
|
| nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
| will be just the return code, as we use strcmp on the buffer */ |
will be just the return code, as we use strcmp on the buffer */ |
| |
|
| if(strcmp(nRecvBuf,"250")!=0) |
if(strcmp(nRecvBuf,"250")!=0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : MAIL FROM: failed\n"); |
printf("libsmtp : smtp_sendmail : MAIL FROM: failed\n"); |
| printf("libsmtp : smtp_sendmail : Server responded |
printf("libsmtp : smtp_sendmail : Server responded %s\n", nRecvBuf); |
| %s\n",nRecvBuf); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_INVALID_FROM); |
return(-SMTP_INVALID_FROM); |
| } |
} |
| Line 302 int smtp_sendmail(const char* zTo, const |
|
| Line 296 int smtp_sendmail(const char* zTo, const |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Recieved less than 3 |
printf("libsmtp : smtp_sendmail : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 311 int smtp_sendmail(const char* zTo, const |
|
| Line 304 int smtp_sendmail(const char* zTo, const |
|
| if(__smtpTimedOut==1) |
if(__smtpTimedOut==1) |
| return(-SMTP_TIMED_OUT); |
return(-SMTP_TIMED_OUT); |
| |
|
| nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
| will be just the return code, as we use strcmp on the buffer */ |
will be just the return code, as we use strcmp on the buffer */ |
| |
|
| if(strcmp(nRecvBuf,"250")!=0) |
if(strcmp(nRecvBuf,"250")!=0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : RCPT TO: failed\n"); |
printf("libsmtp : smtp_sendmail : RCPT TO: failed\n"); |
| printf("libsmtp : smtp_sendmail : Server responded |
printf("libsmtp : smtp_sendmail : Server responded %s\n", |
| %s\n",nRecvBuf); |
nRecvBuf); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_INVALID_TO); |
return(-SMTP_INVALID_TO); |
| } |
} |
| Line 338 int smtp_sendmail(const char* zTo, const |
|
| Line 331 int smtp_sendmail(const char* zTo, const |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Recieved less than 3 |
printf("libsmtp : smtp_sendmail : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 347 int smtp_sendmail(const char* zTo, const |
|
| Line 339 int smtp_sendmail(const char* zTo, const |
|
| if(__smtpTimedOut==1) |
if(__smtpTimedOut==1) |
| return(-SMTP_TIMED_OUT); |
return(-SMTP_TIMED_OUT); |
| |
|
| nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which |
| will be just the return code, as we use strcmp on the buffer */ |
will be just the return code, as we use strcmp on the buffer */ |
| |
|
| if(strcmp(nRecvBuf,"354")!=0) |
if(strcmp(nRecvBuf,"354")!=0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : DATA failed\n"); |
printf("libsmtp : smtp_sendmail : DATA failed\n"); |
| printf("libsmtp : smtp_sendmail : Server responded |
printf("libsmtp : smtp_sendmail : Server responded %s\n", nRecvBuf); |
| %s\n",nRecvBuf); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_SEND_FAILED); |
return(-SMTP_SEND_FAILED); |
| } |
} |
| Line 370 int smtp_sendmail(const char* zTo, const |
|
| Line 361 int smtp_sendmail(const char* zTo, const |
|
| if(nBytesSend<strlen(zBody)) |
if(nBytesSend<strlen(zBody)) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Failed to send entire |
printf("libsmtp : smtp_sendmail : " |
| message body\n"); |
"Failed to send entire message body\n"); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_SEND_FAILED); |
return(-SMTP_SEND_FAILED); |
| } |
} |
| Line 386 int smtp_sendmail(const char* zTo, const |
|
| Line 377 int smtp_sendmail(const char* zTo, const |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Recieved less than 3 |
printf("libsmtp : smtp_sendmail : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 401 int smtp_sendmail(const char* zTo, const |
|
| Line 391 int smtp_sendmail(const char* zTo, const |
|
| if(strcmp(nRecvBuf,"250")!=0) |
if(strcmp(nRecvBuf,"250")!=0) |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_sendmail : Send failed after message |
printf("libsmtp : smtp_sendmail : " |
| send completed\n"); |
"Send failed after message send completed\n"); |
| printf("libsmtp : smtp_sendmail : Server responded |
printf("libsmtp : smtp_sendmail : Server responded %s\n", |
| %s\n",nRecvBuf); |
nRecvBuf); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_SEND_FAILED); |
return(-SMTP_SEND_FAILED); |
| } |
} |
| Line 422 int smtp_disconnect(void) |
|
| Line 412 int smtp_disconnect(void) |
|
| /* Disconnect politly from the mail server */ |
/* Disconnect politly from the mail server */ |
| |
|
| int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ |
int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ |
| char nRecvBuf[3]; /* Return code recieve buffer |
char nRecvBuf[3]; /* Return code recieve buffer |
| */ |
*/ |
| |
|
| /* Send the QUIT command */ |
/* Send the QUIT command */ |
| Line 435 int smtp_disconnect(void) |
|
| Line 425 int smtp_disconnect(void) |
|
| nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
nBytesRecv=recv(__smtpGlobalSocket,(void*)&nRecvBuf,3,0); |
| if(nBytesRecv<3) |
if(nBytesRecv<3) |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_disconnect : Recieved less than 3 |
printf("libsmtp : smtp_disconnect : Recieved less than 3 bytes\n"); |
| bytes\n"); |
|
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| |
|
| /* Clear the timeout alarm & see if we timedout */ |
/* Clear the timeout alarm & see if we timedout */ |
| Line 451 int smtp_disconnect(void) |
|
| Line 440 int smtp_disconnect(void) |
|
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| printf("libsmtp : smtp_disconnect : QUIT failed\n"); |
printf("libsmtp : smtp_disconnect : QUIT failed\n"); |
| printf("libsmtp : smtp_disconnect : Server responded |
printf("libsmtp : smtp_disconnect : Server responded %s\n", |
| %s\n",nRecvBuf); |
nRecvBuf); |
| #endif /* __SMTP_DEBUG */ |
#endif /* __SMTP_DEBUG */ |
| return(-SMTP_DISCONNECT_FAILED); |
return(-SMTP_DISCONNECT_FAILED); |
| } |
} |
| Line 502 int smtp_clear_buf(int nSocket) |
|
| Line 491 int smtp_clear_buf(int nSocket) |
|
| return(-1); |
return(-1); |
| } |
} |
| |
|
| void smtp_alarm(void) |
void *smtp_alarm(int unused) |
| /* Callback function for alarm() */ |
/* Callback function for alarm() */ |
| { |
{ |
| #ifdef __SMTP_DEBUG |
#ifdef __SMTP_DEBUG |
| Line 510 void smtp_alarm(void) |
|
| Line 499 void smtp_alarm(void) |
|
| #endif |
#endif |
| |
|
| __smtpTimedOut=1; |
__smtpTimedOut=1; |
| return; |
return NULL; |
| } |
} |
| |
|
| #if defined(SELF) || defined(SELFTEST) || defined(SELF_SMTP) |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_SMTP) |
| Line 520 void smtp_alarm(void) |
|
| Line 509 void smtp_alarm(void) |
|
| int main(void) |
int main(void) |
| { |
{ |
| /* int nReturnCode=0; */ /* Return code from libsmtp */ |
/* int nReturnCode=0; */ /* Return code from libsmtp */ |
| char message[]="From: frodo@bagend.com\r\nTo: fred@foo.com, |
char message[] = |
| jane@baz.org\r\nSubject: Libsmtp example\r\n\r\nThis is a message to show libsmtp in |
"From: frodo@bagend.com\r\n" |
| action\r\n"; |
"To: fred@foo.com, jane@baz.org\r\n" |
| |
"Subject: Libsmtp example\r\n\r\n" |
| |
"This is a message to show libsmtp in action\r\n"; |
| |
|
| /* Obviously you can build your message dynamically. This message does not |
/* Obviously you can build your message dynamically. This message does not |
| include all the headers required by RFC2822, only the bare minimum */ |
include all the headers required by RFC2822, only the bare minimum */ |