0

I am trying to send an email via mail command in linux c++, but execl is causing errors.

How do I send this command with exec?

/bin/echo llol | /usr/bin/mail -s "testt" [email protected]

Thanks.

Here is the code:

void AppConfig::sendEmail(string to, string subject, string body)
{
    stringstream ss;

    ss << "/bin/echo " << body << " | /usr/bin/mail -s \"" << subject << "\" " << to;
    cout << ss.str();
    cout << "rofl";
    errno = 0;
    int ret = execl(ss.str().c_str(), "", (char*) 0);
    cout << "ret=" << ret << " errno=" <<errno;
}

I get errno=2(directory not found).

1
  • 1
    Can you show us the code and the errors ? Commented Dec 27, 2011 at 23:38

4 Answers 4

4

You probably want to use system() instead of execl().

system("/bin/echo llol | /usr/bin/mail -s "testt" [email protected]");
Sign up to request clarification or add additional context in comments.

3 Comments

beast, forgot about system lol...I'll acept ur answer soon as I can.
If you've used system() before then you probably know you should be wary about the possibility of user input leaking into the call to system(). This can allow users to execute arbitrary code. Using system() carries a great deal of risk but sometimes it's simply the easiest thing to do.
If you use system() and any of those strings are controlled by the user or potentially contain characters special to the shell, then you will need to add code to ensure that they are quoted properly.
2

Since you want to stream text into the process that you exec you might be better off with popen(). This will eliminate the need for echo and you can just popen() /usr/bin/mail.

http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html

Comments

0

I guess | only works on shells such as csh, sh or bash. So you would need to enclose those command in a bash script, or do the input/output redirection in C++. I would recommend the former. It's way easier. If you want to use the latter, take a look at pipe command.

Comments

0

You can also use popen function from C, works very well with C++.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.