[英]Linux exec from c++
我试图在linux c ++中通过mail命令发送电子邮件,但是execl导致错误。
如何使用exec发送此命令?
/ bin / echo llol | / usr / bin / mail -s“ testt” myemail@email.com
谢谢。
这是代码:
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;
}
我得到errno = 2(找不到目录)。
您可能要使用system()而不是execl()。
system("/bin/echo llol | /usr/bin/mail -s "testt" myemail@email.com");
由于您想将文本流式传输到执行的进程中,因此使用popen()可能会更好。 这将消除回显的需要,您可以只popen()/ usr / bin / mail。
http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html
我猜|
仅适用于csh
, sh
或bash
shell。 因此,您需要将这些命令括在bash
脚本中,或者在C ++中执行输入/输出重定向。 我会推荐前者。 更容易。 如果要使用后者,请查看pipe
命令。
您还可以使用C语言的popen函数,与C ++很好地兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.