Skip to content

Instantly share code, notes, and snippets.

@kkrolikowski
Created May 29, 2015 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkrolikowski/dfccfeca7432d7bd7b1f to your computer and use it in GitHub Desktop.
Save kkrolikowski/dfccfeca7432d7bd7b1f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
int main(int argc, char *argv[]) {
int smtp_fd;
int seq = 0;
struct sockaddr_in smtp_s;
char buf[1024];
char rec_buf[256];
if((smtp_fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Error creating socket\n");
exit(-1);
}
memset(&smtp_s, 0, sizeof(smtp_s));
smtp_s.sin_family = AF_INET;
smtp_s.sin_addr.s_addr = inet_addr(argv[1]);
smtp_s.sin_port = htons(atoi(argv[2]));
if(connect(smtp_fd, (struct sockaddr *) &smtp_s, sizeof(smtp_s)) < 0) {
fprintf(stderr, "Error connecting to %s\n", argv[1]);
exit(-1);
}
bzero(buf, 256);
bzero(rec_buf, 256);
while(read(smtp_fd, rec_buf, 256) > 0 && (strstr(rec_buf, "220") != NULL || strstr(rec_buf, "250") != NULL || strstr(rec_buf, "354")) && seq < 6) {
switch(seq) {
case 0 :
strcpy(buf, "HELO smtplib-0.1");
break;
case 1 :
strcpy(buf, "MAIL FROM: ");
strcat(buf, argv[3]);
break;
case 2 :
strcpy(buf, "RCPT TO: ");
strcat(buf, argv[4]);
break;
case 3 :
strcpy(buf, "DATA");
break;
case 4 :
strcpy(buf, "X-Author: Krzysztof Krolikowski <kkrolikowski@gmail.com>\n");
strcat(buf, "X-Mailer: smtp-lib-0.1\n");
strcat(buf, "To: ");
strcat(buf, argv[4]);
strcat(buf, "\n");
strcat(buf, "Subject: ");
strcat(buf, argv[5]);
strcat(buf, "\n\n");
strcat(buf, argv[6]);
strcat(buf, "\n.\n");
break;
case 5 :
strcpy(buf, "quit");
break;
}
write(smtp_fd, buf, sizeof(buf));
write(smtp_fd, "\r\n", 2);
seq++;
}
close(smtp_fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment