来自 c++ 的 Linux exec

发布于 2024-12-23 05:28:00 字数 782 浏览 4 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半岛未凉 2024-12-30 05:28:01

由于您希望将文本流式传输到您执行的进程中,因此最好使用 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

望她远 2024-12-30 05:28:01

我猜 | 仅适用于 cshshbash 等 shell。因此,您需要将这些命令包含在 bash 脚本中,或者使用 C++ 进行输入/输出重定向。我会推荐前者。这更容易。如果您想使用后者,请查看 pipe 命令。

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.

爱的那么颓废 2024-12-30 05:28:01

您还可以使用 C 中的 popen 函数,与 C++ 配合得很好。

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

雨后彩虹 2024-12-30 05:28:00

您可能想使用 system() 而不是 execl()。

system("/bin/echo llol | /usr/bin/mail -s "testt" [email protected]");

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

system("/bin/echo llol | /usr/bin/mail -s "testt" [email protected]");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文