Linux:找不到 Sendmail 的错误代码
Linux: Error Code for Sendmail not Found
i 在 Linux 系统上按照C++代码部署
int sendEMail ( string sEMailAddress, string sEMailSubject , string sEMailText )
{
int nRc = nOK;
// send email here
const int nBUFFERSIZE = 55000;
static char szCommand [ nBUFFERSIZE ] = { 0 };
const char * szEmailText = NULL;
FILE *fpipe = popen("sendmail -t", "w");
szEmailText=sEMailText.c_str();
if ( fpipe != NULL )
{
fprintf(fpipe, "To: %sn", sEMailAddress.c_str());
fprintf(fpipe, "From: %sn", "test@mail.de");
fprintf(fpipe, "Subject: %snn", sEMailSubject.c_str());
fwrite(sEMailText.c_str(), 1, strlen(sEMailText.c_str()), fpipe);
pclose(fpipe);
}
else
{
Logger_log ( 1 , "ERROR: Cannot create pipe to mailx" );
nRc = -1;
}
return nRc;
}
此代码工作正常。我必须确保应该在系统上找到发送邮件。因为我遇到了一个问题。路径变量设置不正确。因此,在系统上找不到发送邮件。我没有收到错误消息。电子邮件似乎已发送出去。但事实并非如此。如果找不到 Sendmail 进程,如何在代码(返回或错误代码)中意识到我收到错误消息?提前感谢
我不确定,但我认为从手册中有一些答案:
1. popen 调用/bin/sh -c <你的命令>所以我想 popen 总是会成功,除非找不到
/bin/sh2.您应该检查返回代码:你的命令>
int ret_code=pclose(fpipe);
if (ret_code != 0)
{
// Error handling comes here
}
从手册页(男人打开)
pclose() 函数等待关联的进程终止,并返回 wait4(2) 返回的命令的退出状态。
。
如果 wait4(2) 返回错误,或者检测到其他错误,则 pclose() 函数返回 -1。
一种方法(专门检查找不到命令错误):
2(stderr)
是 Linux 系统上的默认文件描述符。
将此错误重定向到文件错误文件。现在比较错误文件的内容,如果内容具有command not found
字符串,这将确保找不到该命令。
FILE* fpipe = popen("sendmail 2>errorfile", "w");
FILE* file = fopen("complete path to errorfile", "r");
char buf[124];
fgets(buf, 100, file);
printf("%s", buf);
if(strstr(buf, "command not found")!=NULL)
printf("Command not found");
其他方式,
可以使用system
功能
#include <stdlib.h>
int i;
i = system("sendmail");
printf("The return value is %d", i);
此返回值可用于检查命令是否成功执行。返回值取决于您的计算机操作系统,因此请检查返回值。但是,成功通常为零。
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中
- Linux:找不到 Sendmail 的错误代码