2013-05-14 93 views
1

问题: 未能调用的sendmail:无效的参数使用MTA在Mac OS X中使用c程序发送电子邮件。

#include<stdio.h> 
#include<errno.h> 
#include<string.h> 
int add(char* to,char* from,char* subject,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; 
} 
int main() 
{ 
char to1[256]; 
char from1[256]; 
char message1[256]; 
char sub1[256]; 
int i; 
printf("hello\n"); 
scanf("%s",to1); 
scanf("%s",from1); 
scanf("%s",message1); 
scanf("%s",sub1); 
i=add(to1, from1, sub1, message1); 
return 0; 
} 

我tryed使用C程序发送电子邮件蒙山的Mac OS X中的代码。我不知道问题出在哪里(在代码或本地MTA中)。有人可以给出建议该怎么做?

回答

0

有一个明确的错误和可能的一个:

  • 肯定需要在popen()改变"w+""w"。这将解决无效参数,因为有效参数是wrr+
  • 大概是想看看现代OS X上使用/usr/sbin/sendmail,由于在默认没有安装/usr/lib/sendmail