c++ cgi执行shell命令l发送qq邮件mailx

#include <cstdlib> //随机数 
#include <iostream>
#include <cstdio> //popen函数调用的需要 
#include <string>
#include <sstream>    //用于整型转字符串 
using namespace std;
int main(){
    
    cout << "Content-type:text/html\n\n";
    srand(time(0));
    int rand=(random()%(99999 - 10000 +1)) + 10000;
    stringstream str_r;
    str_r << rand;
    string str_rand=str_r.str();    //随机数 
    
    string a="echo 'Dear users, your verification code is "+str_rand+" , this verification code is valid in 10 minutes. Thank you for using ' | mail -s 'bin of web'  1926804226@qq.com";
    //cout<<a;
//    FILE *fp=popen(a.c_str(),"r");
//    if(!fp) return 1;
//    pclose(fp);
    cout<<system(a.c_str());    //返回0 为成功
    return 0;
} 

 

g++ -o text.cgi text.cpp

mail 命令不是root命令,非root也可以调用

由于我的证书在root下,apache没有权限访问(/root 其他用户没有读的权限)

所以可以将证书移到apache权限目录或者重新生成证书,修改/etc/mail.rc 文件的证书路径,不用重启如何服务,即可成功

权限,yyds

 

 

 

 

 

 

 

参考:

linux mailx 发送邮件到qq邮箱:https://www.cnblogs.com/CloudComputing-binbin/p/14761645.html

c++ 执行shell命令:https://blog.csdn.net/u012234115/article/details/89215980

linux 非root用户mailx发送邮件:https://www.cnblogs.com/hydd/p/14338536.html

 

posted @ 2021-05-17 01:44  binbin_cloud  阅读(137)  评论(0编辑  收藏  举报