LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use "mail" program from System() call in C? (https://www.linuxquestions.org/questions/programming-9/how-to-use-mail-program-from-system-call-in-c-475289/)

minhsangniit 08-18-2006 11:42 PM

How to use "mail" program from System() call in C?
 
Hello everybody,

I have a problem to use "mail" program for my code C to send email.

#include <stdio.h>

int main()
{
char cmd[80];
int icount = 0;
int i = 0;


for (i = 0; i < 10; i++)
{
sprintf(cmd, "mail -s \"This a test message%d\" minhsangtech@gmail.com);//<- HOW TO PASS MESSAGE BODY IN IT?
if (system(cmd) == -1)
{
printf("Cannot send email\n");
}
}

return -1;
}

kstan 08-18-2006 11:58 PM

u may put the required string into a text file, just like
"echo this it the text > msgbody"
then something like this (I'd ) mail user < msgbody.

example of the code maybe (I'm not really sure it works)

#include <stdio.h>

int main()
{
char cmd[80];
int icount = 0;
int i = 0;


for (i = 0; i < 10; i++)
{
system("echo this is msgbody > msgbody");
sprintf(cmd, "mail -s \"This a test message%d\" minhsangtech@gmail.com < msgbody");//<- HOW TO PASS MESSAGE BODY IN IT?
if (system(cmd) == -1)
{
printf("Cannot send email\n");
}
}

return -1;
}

hope this help u.

minhsangniit 08-19-2006 12:08 AM

Quote:

Originally Posted by kstan
u may put the required string into a text file, just like
"echo this it the text > msgbody"
then something like this (I'd ) mail user < msgbody.

example of the code maybe (I'm not really sure it works)

#include <stdio.h>

int main()
{
char cmd[80];
int icount = 0;
int i = 0;


for (i = 0; i < 10; i++)
{
system("echo this is msgbody > msgbody");
sprintf(cmd, "mail -s \"This a test message%d\" minhsangtech@gmail.com < msgbody");//<- HOW TO PASS MESSAGE BODY IN IT?
if (system(cmd) == -1)
{
printf("Cannot send email\n");
}
}

return -1;
}

hope this help u.

Thanks! It worked!


All times are GMT -5. The time now is 05:22 AM.