0

I am trying to send an email using msmtp SMTP client in Linux from C++ code. Using below code I am able to send out an email successfully. If email is not successful, then error information shown on terminal as shown in below terminal image.

I want to know how can I read error message which is dumpped on terminal using C++ code on Linux?.

int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
 int retval = -1;
 std::string command("/usr/bin/msmtp -t");
 
 try{

  FILE *mailpipe = popen(command.c_str(), "w");
  if (mailpipe != NULL) {
   fprintf(mailpipe, "To: %s\n", to);
   fprintf(mailpipe, "From: %s\n", from);
   fprintf(mailpipe, "Subject: %s\n\n", subject);
   fwrite(message, 1, strlen(message), mailpipe);
   fwrite(".\n", 1, 2, mailpipe);
   retval = pclose(mailpipe); // returns 0 on successful mail sent. else greater than 0 will be given.
   }
   else {
    perror("Couldn't start command. Make sure msmtp installed and configured properly");
   }
 }
 catch(exception& e)
 {
  cout<<"Error: ";
 }
  
     return retval;
}

enter image description here

0 Answers0