Question

The problem: Failed to invoke sendmail: Invalid argument

The code

#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;
}

I tryed to send email whith Mac OS X using c program. I dont know where the problem is(in the code or in the local MTA). Can someone give any advice what to do?

Was it helpful?

Solution

There's a definite bug and a probable one:

  • Definitely need to change "w+" to "w" in popen(). That will solve the invalid argument, as the valid arguments are w,r, and r+.
  • Probably want to look at using /usr/sbin/sendmail on modern OS X, since there is no /usr/lib/sendmail on the default install
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top