SndMail

kleine Funktion um Mails ohne Authentifizierung innerhalb einer Organisation zu versenden.

sndmail.c
//kleines mailprogramm ohne SMTP-Authentifizierung / richard@borwinius.de
// gcc sndmail.c -o sndmail -Wall
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
int MySnd(char *SMTPsrv,char *From,char *To,char *ReplyTo,
	    char *Subject, char *Mail)
{
    struct servent *lpServ;
    unsigned long addr;
    int s, iProtocolPort;
    struct sockaddr_in  sockadd;
    struct hostent *host_info;
    char *seps = "\r", *token = NULL;
    char StrRcv[1024], StrSnd[1024], ContentType[1024];
 
    if (strcmp(SMTPsrv, "") == 0) {	 // Fehler kein Server
        perror("kein SMTPsrv ?\n");
        return(1);
        }
    if (strcmp(To, "") == 0) {		// Fehler kein Adressat
        perror("kein Adressat ?\n");
        return(2);
        }
    memset(&sockadd,0,sizeof(sockadd));
    if ((addr = inet_addr(SMTPsrv)) != INADDR_NONE)
	{
	memcpy ((char *)&sockadd.sin_addr,&addr,sizeof(addr));
	}
    else
    {
    host_info = gethostbyname(SMTPsrv);
    if (host_info == NULL) {		// falscher Server
        fprintf(stderr,"%s existiert nicht oder kann nicht aufgelöst werden\n", SMTPsrv);
        return(3);
        }
    }
    memcpy((char *)&sockadd.sin_addr,host_info->h_addr,host_info->h_length);
 
    s = socket(PF_INET, SOCK_STREAM, 0);
    if (s < 0) {
        perror("INVALID_SOCKET\n");
        return(4);
    }
    lpServ = getservbyname("mail", NULL); // SMTPPort  ermitteln
    if (lpServ == NULL) {
        iProtocolPort = htons(IPPORT_SMTP);
    } else {
        iProtocolPort = lpServ->s_port;
    }
    sockadd.sin_family = AF_INET;
    sockadd.sin_port = iProtocolPort;
    if (connect(s, (struct sockaddr*)&sockadd, sizeof(sockadd))) { // kein Connect
        perror("connect\n");
        return(5);
    }
//    memset(StrRcv,'\0' , sizeof(StrRcv));
//    recv(s, StrRcv, sizeof(StrRcv), 0);
 
    strcpy(StrSnd,  "HELO ");			//Verbindung mit Server herstellen
    strcat(StrSnd, SMTPsrv);
    strcat(StrSnd, "\r\n");
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
    if (strcmp(From, "") == 0) {
        perror("kein Absender ?\n");
        strcpy(From, StrSnd);
    }
    sprintf(StrSnd, "MAIL FROM : <%s>\r\n", From); // Absender angeben
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
 
    sprintf(StrSnd, "RCPT TO :<%s>\r\n", To); 	// Empfänger angeben
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
 
    if(strstr(StrRcv, "550") != NULL){		 //unable to relay
	fprintf(stderr,"StrRcv: %s\n",StrRcv);    
	strcpy(StrSnd,  "QUIT\r\n");
        send(s, StrSnd, strlen(StrSnd), 0);
	return(6);
    }
    strcpy(StrSnd, "DATA\r\n");			//Daten übergeben
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
 
	//strcpy(StrSnd, "X-Mailer: ");
	strcpy(StrSnd, "Content-Type: ");
    strcat(StrSnd,ContentType);strcat(StrSnd,"\r\n");
	send(s, StrSnd, strlen(StrSnd), 0);
 
	strcpy(StrSnd, "From: ");
    strcat(StrSnd,From);strcat(StrSnd,"\r\n");
	send(s, StrSnd, strlen(StrSnd), 0);
 
	strcpy(StrSnd, "To: ");
    strcat(StrSnd,To);strcat(StrSnd,"\r\n");
	send(s, StrSnd, strlen(StrSnd), 0);
 
    if (strcmp(ReplyTo, "") != 0) {
        sprintf(StrSnd, "Reply-To: %s\r\n", ReplyTo); // Rückantwortadresse
        send(s, StrSnd, strlen(StrSnd), 0);
    }
    sprintf(StrSnd, "Subject: %s \r\n", Subject); // Betreff
    send(s, StrSnd, strlen(StrSnd), 0);
    strcpy(StrSnd, "\r\n");
    send(s, StrSnd, strlen(StrSnd), 0);
    token = strtok(Mail, seps);
    while (token != NULL) {
        if (token[0] == '\n') {
            strcpy(StrSnd, token + 1);
        } else {
            strcpy(StrSnd, token);
        }
        strcat(StrSnd, "\r\n");
        send(s, StrSnd, strlen(StrSnd), 0);
        token = strtok(NULL, seps);
    }
    strcpy(StrSnd, ".\r\n");		// Ende der Daten
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
    if(strstr(StrRcv, "500") != NULL){
	fprintf(stderr,"StrRcv: %s\n",StrRcv);    
	strcpy(StrSnd,  "QUIT\r\n");		// Ende der Verbindung
        send(s, StrSnd, strlen(StrSnd), 0);
	return(7);
    }
 
    strcpy(StrSnd,  "QUIT\r\n");		// Ende der Verbindung
    send(s, StrSnd, strlen(StrSnd), 0);
    memset(StrRcv, '\0', sizeof(StrRcv));
    recv(s, StrRcv, sizeof(StrRcv), 0);
    close(s);
    return(0);
}
 
//-------------------------------------------
int main(void)
{
char *SMTPsrv = "meinSMTPsrv";
char *From = "ich@irgendwo.net";
char *To = "admin@myDom.ain";
char *ReplyTo = "staff@myDom.ain";
char *Subject = "testmailueberschrift";
char Mail[] =	"erste Zeile\nzweite Zeile"\
		" abcdefg";
 
return MySnd(SMTPsrv,From,To,ReplyTo,Subject,Mail);  // Mails senden
}