-1

C++ -------> g++ -------> bash :

#include <cstdlib>

using namespace std;

int main(){

    system("mail -s test_mail xxxx@testdomain.com");
    system("test msg");
    system(".");

    return 0;
 }

The first system command runs fine, but then it's like it takes input from another input stream (not stdin). system("test msg") only runs after i cntl+c out of the mail program, only then does bash try to interpret "test msg".

closed as off-topic by derobert, don_crissti, garethTheRed, Anthon, Jeff Schaller May 16 '16 at 21:50

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    system is not intended to act like a way to inject stuff like it is coming from stdin. It runs whole command lines. Write your message to a temporary file, then do something like system("mail -s test_mail xxxx@testdomain.com < tempfile.txt"); – infixed May 16 '16 at 19:51
  • 1
    You're looking for popen, not system, as you need to write to mail's stdin. Or possibly the much more complicated fork/pipe/dup2/exec combo. But you probably ought to ask your programming questions on Stack Overflow. – derobert May 16 '16 at 20:00
1

system() really just runs command lines. Its not a pipe like thing.

The smallest possible change to make your example program work would be

int main()
{
    system("echo test msg | mail -s test_mail xxxx@testdomain.com");
    return 0;
}

because then your message is going into mail program standard input via a pipe.

More practical would be for you to write your message to a temporary fle, and then redirect mail stdin from that file

  • infixed: I tried piping it like that but it sends the content as an attachment, not as the email content. I also tried the "< tempfile.txt" method but that also sends it as an attachment. (i've yet to try derobert's suggestion i'll get back to you after I try it) – Ben Marconi May 16 '16 at 22:32
  • That would seem to be non-standard behavior for the usual unix mail program. This sort of question has come up before, and the stdin solution was given as one answer stackoverflow.com/a/36500/6047952 I'd suggest doing a man mail and seeing if your version has some switch that can control it to . behave as you wish. You are sending just simple ASCII stuff, right? – infixed May 16 '16 at 22:43
  • lol dunno, how can I check? – Ben Marconi May 16 '16 at 22:49
  • The simple ASCII bit is import. For instance look at access.redhat.com/solutions/1136493 where anything but printable ASCII and linefeeds ( no returns!) becomes attachments. This is probably what is happening to you. That echo text | mail yourself should be simple – infixed May 16 '16 at 22:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.