Link to home
Start Free TrialLog in
Avatar of wguiot
wguiot

asked on

C function to interact with /usr/sbin/sendmail

I'm trying to do a function to send emails using Linux's /usr/sbin/sendmail. My code is:

void send_email(const char *email_address)
{
     FILE *email;
     email = popen("/usr/sbin/sendmail -t", "w");
     fprintf(email, "To: %s\n", email_address);
     fprintf(email, "From: %s\n", "user@domain.com\0");
     fprintf(email, "Subject: %s\n", "Automatic email\0");
     fprintf(email, "This is the email body.\n\n");
     fprintf(email, "\n");
     pclose(email);
}

It does not work.
ASKER CERTIFIED SOLUTION
Avatar of skian
skian

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wguiot
wguiot

ASKER

I'm using valid FROM and TO addresses.

What about "wrt your sendmail config"? I don't understand your comment. Is there something I need to do in the sendmail config?

Well, your current sendmail config may not allow
mail relay, ie it could reject a mail if the TO
or FROM address is not known.
For example, to have this piece of code running
here, I had to replace the from address by mylogin@localhost

What is the return value from pclose ?

Have you tried sending the same message from
the shell command line ?

Stephane
Avatar of wguiot

ASKER

The function works, the call to it was unreachable.

My bad!.

Thanks, anyway. I'm accepting the answer because it might serve as an example for pipes for someone else.