sending an email from a C/C++ program in linux

后端 未结 5 1664
花落未央
花落未央 2020-12-24 08:57

I would like to send an email to my gmail account everytime my simulation ends. I have tried searching the web and found sendEmail but it is timing-out. If anyone could poin

5条回答
  •  春和景丽
    2020-12-24 09:57

    Do a fork exec and pipe the body to a program like sendmail/mail:

    #include 
    #include 
    #include 
    #include 
    
    using std::string;
    
    static const int READEND = 0;
    static const int WRITEEND = 1;
    
    int sendEmail(const string& to, const string& subject, const string& body) {
      int p2cFd[2];
    
      int ret = pipe(p2cFd);
      if (ret) {
        return ret;
      }
    
      pid_t child_pid = fork();
      if (child_pid < 0) {
        close(p2cFd[READEND]);
        close(p2cFd[WRITEEND]);
    
        return child_pid;
      }
      else if (!child_pid) {
        dup2(p2cFd[READEND], READEND);
        close(p2cFd[READEND]);
        close(p2cFd[WRITEEND]);
    
        execlp("mail", "mail", "-s", subject.c_str(), to.c_str(), NULL);
    
        exit(EXIT_FAILURE);
      }
    
      close(p2cFd[READEND]);
    
      ret = write(p2cFd[WRITEEND], body.c_str(), body.size());
      if (ret < 0) {
        return ret;
      }
    
      close(p2cFd[WRITEEND]);
    
      if (waitpid(child_pid, &ret, 0) == -1) {
        return ret;
      }
    
      return 0;
    }
    
    int main() {
      return sendEmail("email@hostname.com", "Subject", "Body");
    }
    

提交回复
热议问题