I am trying to make a c++ program that sends an email using the mail command, using a string as the body. To set the string of the message, I am trying to pipe a message in.
For example, I am trying the following, which should send an email to email@gmail.com with a message saying “test”, but no message is sent. Any ideas?:
fp = fopen("mail email@gmail.com", "w+");
dup2(fileno(fp), 1);
fclose(fp);
printf("test\n");
Your first argument to
fopenis not a file, and fopen is for opening files.Try using popen to open a pipe connected to a subprocess instead: