c++ - reading msmtp response from terminal
问题描述
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;
}
解决方案
推荐阅读
- java - 如何在 docker 容器中部署 Spring 应用程序?
- android - 我可以从 android.view.View.onDraw() 中查看输入手势吗?
- django - 如何实现每页有效的访问计数器?
- php - url中的加密参数
- swift - SKView.presentScene(scene) 不起作用,但 SKView.presentScene(scene, transition) 起作用
- javascript - html5画布文本不是指定的大小
- c# - 这个 LINQ 查询在 C# 中做了什么?
- karate - 在单个场景中的请求之间重置路径是预期的行为吗?
- wordpress - WooCommerce 登录后重定向页面
- java - 禁用 IntelliJ IDEA 中的所有提示