ftp.nice.ch/pub/next/unix/network/www/gform.1.1.NIHS.bs.gnutar.gz#/gform-1.1/send_email.c

This is send_email.c in view mode; [Download] [Up]

#include <string.h>

/***
 * Send an email message
 *
 *  to - The intended recipient.
 * sub - The subject of the message.
 * bod - The message body.
 *
 * returns 0 on success
 *	  -1 on failure.
 *
 ***/
 
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>

#include "config.h"

#ifndef MAILER
#define MAILER "/usr/bin/mail"
#endif


void *malloc();

int send_email(to,sub,body)
char *to, *sub, *body;
{
    int fd[2], pid;
    FILE *f1;
    char *msg;

    msg = (char *) malloc(strlen(body)+strlen(sub)+strlen(to)+16);

    sprintf(msg,"To: %s\n",to);
    strcat(msg,"Subject: "); strcat(msg,sub);strcat(msg,"\n");
    strcat(msg,body);

/* set child process STDIN to parent fd */
	
    if (pipe(fd) <0)
	return(-1);	
  
    if ((pid = fork()) < 0)
 	return(-1);	
    else if (pid >0) {    		/* parent */
	close(fd[0]);	 		/* close read end */
	write(fd[1], msg, strlen(msg));
        close(fd[1]);
	if (waitpid(pid, NULL, 0) <0)
		return(-1);	
/* 	close(fd[1]); */
	return(0);
    } else {		/* child */
	close(fd[1]);	
	if (fd[0] != STDIN_FILENO) {
	   if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) 
		return(-1);	
	   close(fd[0]);
	}	/* invoke mailer which expects STDIN */
        if (execl(MAILER,MAILER,to,0) <0) 
          	return(-1); 
	close(fd[0]);
	exit(0);
    }
    return(0);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.