#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
int senderr(int s, char * message){
if((send(s, message, strlen(message), 0)) < 0){
printf("send() error, while sending \"%s\"\n", message);
return -1;
}
return 0;
}
int main(int argc, char** argv){
if(argc < 4){
printf("Usage: ./email <to> <from> <message>\n");
return 0;
}
const struct addrinfo hints = {AI_CANONNAME, AF_INET, SOCK_STREAM, IPPROTO_TCP};
struct addrinfo *res;
int s;
char * buffer;
if((buffer = calloc(512, 1)) == NULL){
printf("calloc() error.\n");
return 0;
}
if(getaddrinfo("smtp.charter.net", "25", &hints, &res) != 0){ //or w/e server you use.
printf("getaddrinfo() error.\n");
return 0;
}
if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("socket() error.\n");
return 0;
}
if(connect(s, res->ai_addr, res->ai_addrlen) < 0){
printf("connect() error.\n");
return 0;
}
senderr(s, "HELO hi\r\n");
sprintf(buffer, "MAIL FROM: %s\r\n", argv[2]);
if(senderr(s, buffer) < 0) return 0;
memset(buffer, 0, 512);
sprintf(buffer, "RCPT TO: %s\r\n", argv[1]);
if(senderr(s, buffer) < 0) return 0;
memset(buffer, 0, 512);
if(senderr(s, "DATA\r\n") < 0) return 0;
sprintf(buffer, "%s\r\n.\r\n", argv[3]);
if(senderr(s, buffer) < 0) return 0;
senderr(s, "QUIT\r\n");
return 0;
}