LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 02-19-2004, 05:20 PM   #1
samble
LQ Newbie
 
Registered: Nov 2003
Posts: 15

Rep: Reputation: 0
How to send an e-mail from within a linux application


I am running Red Hat Linux 7.3 on my server. The linux kernel is "2.4.18-5". I have an application running on this server. Whenever the server is rebooted I would like to send an e-mail out to an administrator from within this application. I have tried the following piece of code to be convinced about the possibility but to no avail. Would any knowledgeable source help me out here?

The sample code is:

int main (void)
{

execl ("/bin/mail", "/bin/mail", "-s Test", "joe@yourtown.com < /tmp/relInfo.c", (char *)0);

}

When I cmpile the above code and execute then ti prompts me for "Message body", "Cc", and then it complains that "/tmp/relInfo.c" cannot be found.

When I execute the command "mail -s Test joe@yourtown.com < /tmp/relInfo.c" the mail is sent out without any problem.

Thanks in advance for all helpful suggestions

Srini
 
Old 02-20-2004, 03:03 AM   #2
PSIplus
Member
 
Registered: Feb 2004
Location: Vienna/Austria
Distribution: Debian Unstable
Posts: 36

Rep: Reputation: 15
Hmmm... Try it this way:

Code:
spid=fork();
if(!spid)
{
	stdin=freopen("/path/to/your/email.txt","r",stdin);
	stdout=freopen("/dev/null","w+",stdout);
	stderr=freopen("/dev/null","w+",stderr);
	execlp("/usr/sbin/sendmail","/usr/sbin/sendmail","target@mail.addr",NULL);
}
First of all: Pipelining with >, < and | is Shell-Style, e.g. you cann't use it directly from within an c app :-)

And: You have to specify each parameter it's own value when you use execlp

If you want to redirect from within a c app, use freopen (as seen above).

Have Fun!
Martin
 
Old 02-20-2004, 07:09 AM   #3
samble
LQ Newbie
 
Registered: Nov 2003
Posts: 15

Original Poster
Rep: Reputation: 0
Martin,

Thank you very much for taking time to read my novice question and posting a suggestion that worked very well for me.

Kind regards

Srini
 
  


Reply



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
Send mail from Linux machine to outside world schamie Linux - Networking 1 11-10-2005 02:15 AM
Server does'nt send mail until shutdown and LAN can't send or receive. Wolfy Linux - Networking 0 08-02-2004 07:31 PM
Cannot send mail with mail() command in PHP 4.3.4, Apache 2.0.48, Mandrake Linux 10 arcanum Linux - General 3 06-27-2004 06:06 PM
Send mail from Linux thru MSExchange server himan0110 Linux - Networking 2 05-04-2004 11:28 AM
windows clients not being able to send mail through linux server kpg Linux - Networking 3 02-12-2004 01:35 PM

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

All times are GMT -5. The time now is 12:55 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