LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Thread Tools
Old 03-31-2005, 07:49 PM   #1
bmorel
LQ Newbie
 
Registered: Mar 2005
Location: Paris, France
Distribution: RHEL 3
Posts: 17
Thanked: 0
Mail command (much) faster than sendmail


[Log in to get rid of this advertisement]
Hello all,

I'd like to understand why "mail" command is much faster than sendmail.
Here's an example :

mail my@email.com < a_mail_body.txt : about 15 mS
sendmail -t < a_mail.txt : about 300 mS (20x slower!)

mail does use sendmail internaly, doesn't it ? If it does, how can it speed up sendmail this way ? What's its secret?

I tried several sendmail options to speed it up, nothing works. (min 150mS using option -O DeliveryMode=q, which is not what I want !)

My aim is to speed up php's mail() function, which only delivers 3 emails/second using sendmail command.

Any clues ?

Thanks in advance
Ben
bmorel is offline     Reply With Quote
Old 04-01-2005, 05:27 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,709
Thanked: 19
Where does sendmail send to?

You'll find, Mail puts in in the sendmail queue '/var/mail/queue' (?)
and sendmail sends it later.

Whereas direct sendmail does it NOW

try 'sendmail -bp' to look at the queue after Mail.
bigearsbilly is online now     Reply With Quote
Old 04-01-2005, 05:50 AM   #3
bmorel
LQ Newbie
 
Registered: Mar 2005
Location: Paris, France
Distribution: RHEL 3
Posts: 17
Thanked: 0

Original Poster
Lightbulb

Ok thank you for this answer, this explains a little bit why i'ts faster !
But then :

- Why does "sendmail -O DeliveryMode=q" is still 10x slower than "mail" ?
- Why does an e-mail sent with "mail" seem to be delivered very quickly ? Which process then tells sendmail to procede with the mail queue ?

Does someone have an url for in-details explanations of the "mail" command ?

Thanks in advance
Ben
bmorel is offline     Reply With Quote
Old 04-01-2005, 07:39 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,709
Thanked: 19
could it be this?

http://www.sendmail.org/tips/private-dns/
bigearsbilly is online now     Reply With Quote
Old 04-01-2005, 09:13 AM   #5
bmorel
LQ Newbie
 
Registered: Mar 2005
Location: Paris, France
Distribution: RHEL 3
Posts: 17
Thanked: 0

Original Poster
Hm thanks for the link but I don't think it's the problem here.
My server seems to be properly configurated (working successfully for more than 1 year), and if I ask sendmail to "queue only", it should not try to reverse dns's

I'm really interested in knowing how "mail" works internally. Is its source code easily available ? And where can I find it ?

Thanks
Ben
bmorel is offline     Reply With Quote
Old 04-01-2005, 10:01 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,709
Thanked: 19
google of course
bigearsbilly is online now     Reply With Quote
Old 04-01-2005, 06:41 PM   #7
bmorel
LQ Newbie
 
Registered: Mar 2005
Location: Paris, France
Distribution: RHEL 3
Posts: 17
Thanked: 0

Original Poster
After a couple of hours I finally found why !
I saw in the source code that mail (mailx is the real name of the package) fork()s itself to run multiple instances of sendmail simultaneously using multithreading !

That's why it's so fast :
- Program forks
- Child executes sendmail
- Parent exits
- sendmails are executed in background

Note: the mails are not simply queued, sendmail tries to deliver them directly !

Ben
bmorel is offline     Reply With Quote
Old 04-02-2005, 06:57 PM   #8
bmorel
LQ Newbie
 
Registered: Mar 2005
Location: Paris, France
Distribution: RHEL 3
Posts: 17
Thanked: 0

Original Poster
Hello again

My problem now is that I need to make sendmail command work in the background when calling it from php
(In order not to wait 300mS for each mail when I need to send 20 mails: my web user cannot wait 6 seconds for the page to load)

Unfortunately, multitasking in PHP is not powerful enough for the moment (I don't want to waste resources with a pcntl_fork() on a long program)

So the problem is that I can't right now make sendmail run in the background (with the & at the end of the command line) and pipe it some data.
Because the & makes it work at once in the background, not waiting for stdin.

I mean, this solution doesn't work : popen("/usr/sbin/sendmail -t -i &");
If I fwrite to this pointer, it's too late, sendmail has already gone into the background.

The only solution I found for the moment is to use a sendmail_fork command that I quickly wrote :

==============================================================
#include <stdio.h>
#define SENDMAIL_PATH "/usr/sbin/sendmail"

int main(int argc, char *argv[])
{
char buf[1024];
int i;
int pid;
FILE* p;
strcpy(buf, SENDMAIL_PATH);
for (i=1; i<argc; i++) {
strcat(buf, " ");
strcat(buf, argv[i]);
}
p = popen(buf, "w");
while ((i=fgetc(stdin)) != EOF) {
fputc(i, p);
}

pid = fork();

switch (pid) {
case -1:
printf("fork() error.\n");
return 1;
case 0:
pclose(p);
}

return 0;
}
==============================================================

This program, once compiled, can replace sendmail command.
This is of course a temporary solution.
Is there a good tip to make a program run in the background AFTER all the pipe is received ??

Thanks
Ben
bmorel is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
sendmail error Fetching mail could not lock /var/spool/mail/username sukhdev50 Linux - Networking 0 05-04-2005 04:41 AM
How can I use the Mail command with Sendmail Options Kalar Linux - Newbie 1 07-16-2004 11:35 PM
How to make Sendmail forward all incoming mail to localhost port 26 (Domino mail) speedgelb Linux - Software 2 04-04-2004 05:41 PM
sendmail: Routing dead mail to a shell command? noisybastard Linux - Networking 1 10-05-2003 05:13 PM
Is it possible to force Sendmail to deliver 'faster'? gboutwel Linux - Networking 1 05-25-2001 06:21 PM


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

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration