mail
目标是像 php函数一样在 Unix 机器上发送电子邮件。在 StackOverflow 上我已经找到了一个功能,但也许有人可以提出更优雅的解决方案或没有fprintf
and的解决方案fwrite
?因为我不在printf
我的C++程序中使用:
#include<stdio.h>
#include<errno.h>
int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
int retval = -1;
FILE *mailpipe = popen("usr/lib/sendmail -t", "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);
pclose(mailpipe);
retval = 0;
}
else
{
perror("Failed to invoke sendmail”);
}
return retval;
}