LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2004, 01:22 PM   #1
linuxprogrammer
LQ Newbie
 
Registered: Jul 2004
Posts: 26

Rep: Reputation: 15
mail using a C program


In a C program of mine, I want to mail a message to root if certain conditions are met. How do I send the mail? If there is some mailing library, it'll be very helpful to have its name and description.
 
Old 07-19-2004, 01:43 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Here is one way:
Code:
char fpBuffer[128]={'\0'};
.........
sprintf (fpBuffer,
          "/usr/bin/mailx -s '%s' %s < %s",
          "Email Report",
          EmailId,
          RptName);
       system (fpBuffer);
 
Old 07-20-2004, 08:52 AM   #3
linuxprogrammer
LQ Newbie
 
Registered: Jul 2004
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by jim mcnamara
Here is one way:
Code:
char fpBuffer[128]={'\0'};
.........
sprintf (fpBuffer,
          "/usr/bin/mailx -s '%s' %s < %s",
          "Email Report",
          EmailId,
          RptName);
       system (fpBuffer);
Thanks. I tried this on two machines of mines.

This works on one, where I use Linux directly from my Knoppix CD. But even when I tried sending mail directly from the prompt, it took about 10-15 minutes for root to receive the message. What may be the cause of this delay?

Another machine of mine, when I attempted to send a mail from the command prompt said there was some problem with the name server. This machine is on LAN. How can I correct the problem?
 
Old 07-20-2004, 11:31 PM   #4
afm
LQ Newbie
 
Registered: Jul 2004
Posts: 16

Rep: Reputation: 2
Please, don't use the sample code above.

It has severe security flaws, like allowing for buffers overflows and shell escape sequences attacks.

To send mail using C, try this (untested) code snippet:

Code:
#define SENDMAIL "/usr/sbin/sendmail -i -t"
FILE *sm;
sm = popen(SENDMAIL, "w");
if ( !sm )
{
   your_errno_handling_routine();
   return 1;
}

fprintf(sm, "From: %s\n", from);
fprintf(sm, "To: %s\n", to);
fprintf(sm, "Subject: %s\n", subject);
/* print other headers here */
fputc('\n', sm);
fputs(message, sm);

if ( pclose(sm) == -1 )
{
   your_errno_handling_routine();
   return 1;
}
The trickiest part is to find the correct value for the SENDMAIL macro constant. There's is no standard command line to run the system mailer, though calling /usr/sbin/sendmail and a subset of its parameters is becoming a de facto standard (even other mailers, such as Postfix, install a somewhat compatible /usr/sbin/sendmail).

If you want to write portable programs, use autoconf to discover a sane value for SENDMAIL.
 
Old 07-20-2004, 11:56 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 53
since you are writing in c you could always just implement smtp and talk to the server on "localhost"
too long code to start posting but you can find examples..
I like this solution better than piping
 
Old 07-21-2004, 12:08 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Mail program? cultavix Linux - Software 9 04-14-2005 12:33 PM
The old mail program jeempc Linux - Software 3 10-14-2003 10:04 PM
Best mail program tabsnz Linux - Software 0 08-28-2003 03:30 PM
which mail program ? jamaso Slackware 6 05-15-2002 08:37 AM
e-mail program NSKL Linux - General 6 03-05-2002 08:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration