Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

best way to construct and send email in c

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
hi there,

what is the best way to construct and send an email in c.

i don't want to have to call a bash shell script that uses the mail command.

TIA,

rotis23
 
You can write a C program that
1) reads data from the user and writes it to a file. You write the C code or just fork and exec 'vi'.
2) exec the mail command ,redirecting the input from the file written in (1)


otherwise you can directly exec the mail command and compose the mail over there itself.


Writing a complete mailer application is a much more complex task.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
thanks,

that was what i was thinking of doing. i wasn't sure weather ansi c had a similar command.

does anyone know how to send a pager message in c. it seems the best way, like the email problem, is to call the snpp command using exec.

any other ideas?

rotis23
 
solution (linux):

email - use execl command to call linux mail command

pager - use execl command to call linux snpp command

sorted!
 
if you will do it in 'C', try
--------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

#define NOFILE 64

/*********
change the next 2 lines
*********/

#define SUBJECT &quot;Mail-Subject&quot;
#define MAILTO &quot;username@domain.com&quot;

int main(int argc,char **argv)
{
extern char *basename(char *);
char *myname = basename(*argv);
int ppp[2], mail;

if(0 >pipe(ppp)) exit(fprintf(stderr,&quot;%s: Can't create pipe\n&quot;,myname));

if(0 >(mail = fork())) exit(fprintf(stderr,&quot;%s: Can't fork\n&quot;,myname));
if(!mail){ /* child process, start mail */
(void)dup2(ppp[0],0);

for(mail = 3; mail< NOFILE; (void)close(mail++));

execl(&quot;/bin/mailx&quot;,&quot;mailx&quot;,&quot;-s&quot;,SUBJECT,MAILTO,(char *)0);

exit(fprintf(stderr,&quot;%s: Can't execl\n&quot;,myname));
}

/* father-process, write text for mail */

(void)dup2(ppp[1],1);

fprintf(stdout,&quot;...........mail text............\n&quot;);
fprintf(stdout,&quot;................................\n&quot;);
fprintf(stdout,&quot;................................\n&quot;);
fprintf(stdout,&quot;................................\n&quot;);
fprintf(stdout,&quot;...........end mail.............\n&quot;);

fflush(stdout);
(void)close(1);
(void)close(ppp[0]);
(void)close(ppp[1]);
(void)wait(&mail);
exit(0);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top