// // smtp.c // Version 2.0 // // Created by GREIF Software on 08/02/29. // Copyright GREIF Software 2008. All rights reserved. // #include #include #include #include char buf[64], buf2[401], fromA[61], fromN[61], toA[61], toN[61], title[101], body[201], stat[32]; int main(int argc, char **argv) { int sk, n, ret, err, port; unsigned int ip, i, c; struct sockaddr target; cgi_value(argv[1], "fromA", fromA, 60); cgi_value(argv[1], "fromN", fromN, 60); cgi_value(argv[1], "toA", toA, 60); cgi_value(argv[1], "toN", toN, 60); cgi_value(argv[1], "title", title, 100); cgi_value(argv[1], "body", body, 200); c = 0; ip = strtoip("192.168.1.100", &err); // Mail Server IP Address (192.168.1.100) if(err == -1) { printf("Illegal format\r"); return -1; } sk = tcp_socket(0); target.sin_addr = ip; target.sin_port = 25; // Port No. (25) ret = tcp_connect(sk, &target); if(ret == -1) { printf("Connect error!\r"); return -1; } strcpy(stat,"HELO"); while(1) { n = tcp_read(sk, buf, 63); if(n == -1) { // printf("TCP Read error! %d\r",c); if(c > 20) break; c++; } else { buf[n] = 0; if(strstr(buf,"220") != NULL && strcmp(stat,"HELO") == 0) { strcpy(buf2,"HELO 192.168.1.150\r\n"); // hello (myIP : 192.168.1.150) tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"From"); } else if(strstr(buf,"250") != NULL && strcmp(stat,"From") == 0) { strcpy(buf2,"MAIL FROM:<"); // From strcat(buf2,fromA); // From strcat(buf2,">\r\n"); // From tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"To"); } else if(strstr(buf,"250") != NULL && strcmp(stat,"To") == 0) { strcpy(buf2,"RCPT TO:<"); // To strcat(buf2,toA); // To strcat(buf2,">\r\n"); // To tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"DATA"); } else if(strstr(buf,"250") != NULL && strcmp(stat,"DATA") == 0) { strcpy(buf2,"DATA\r\n"); // DATA tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"Body"); } else if(strstr(buf,"354") != NULL && strcmp(stat,"Body") == 0) { strcpy(buf2,"From: \""); // From strcat(buf2,fromN); // From strcat(buf2,"\" <"); // From strcat(buf2,fromA); // From strcat(buf2,">\r\n"); // From tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,"To: \""); // To strcat(buf2,toN); // To strcat(buf2,"\" <"); // To strcat(buf2,toA); // To strcat(buf2,">\r\n"); // To tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,"Subject: "); // Subject strcat(buf2,title); // Subject strcat(buf2,"\r\n"); // Subject tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,"X-Mailer: SMTPcgi\r\n"); // X-Mailer (SMTPcgi) tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,"\r\n"); // CRLF tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,body); // Body strcat(buf2,"\r\n"); // Body tcp_write(sk, buf2, strlen(buf2)); strcpy(buf2,"\r\n.\r\n"); // Terminate tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"END"); } else if(strstr(buf,"250") != NULL && strcmp(stat,"END") == 0) { strcpy(buf2,"QUIT\r\n"); tcp_write(sk, buf2, strlen(buf2)); strcpy(stat,"Comp"); // Normal End break; } else { // Unexpected End strcpy(buf2,"QUIT\r\n"); tcp_write(sk, buf2, strlen(buf2)); break; } } } tcp_free(sk); printf("
\r\n"); printf("

Send Mail


\r\n"); if (strcmp(stat,"Comp") == 0) { printf("

Transfer Completed.

\r\n"); } else { printf("

Transfer Failed.

\r\n"); } printf("

\r\n"); }