[英]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.