来自 c++ 的 Linux exec
我试图在 linux c++ 中通过 mail 命令发送电子邮件,但 execl 导致错误。
如何使用 exec 发送此命令?
/bin/echo llol | /usr/bin/mail -s "testt" [电子邮件受保护]< /p>
谢谢。
这是代码:
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(找不到目录)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您希望将文本流式传输到您执行的进程中,因此最好使用 popen()。这将消除对 echo 的需要,您只需 popen() /usr/bin/mail 即可。
http://pubs.opengroup.org/onlinepubs/009604499/functions/popen.html
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
我猜
|
仅适用于csh
、sh
或bash
等 shell。因此,您需要将这些命令包含在 bash 脚本中,或者使用 C++ 进行输入/输出重定向。我会推荐前者。这更容易。如果您想使用后者,请查看pipe
命令。I guess
|
only works on shells such ascsh
,sh
orbash
. So you would need to enclose those command in abash
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 atpipe
command.您还可以使用 C 中的 popen 函数,与 C++ 配合得很好。
You can also use popen function from C, works very well with C++.
您可能想使用 system() 而不是 execl()。
You probably want to use system() instead of execl().