[PHP] Re: mail() takes too much time

2007-09-03 Thread Matthew Lasar
I run pretty simple mail group distribution program that uses 
php/mysql and the mail() function. I adapted it from an open source 
script. Most of the time it runs well. But it does take a while to 
run through all 150 members of the list. So I'm half glad that I 
don't have a list of 1000 people or more.


Any way to optimize the mail function in terms of speed? Probably too 
vague a question, sorry.


/ml

At 07:50 AM 9/2/2007, you wrote:

Stut wrote:
 Unless your script is actually hanging for the 4-6 hours this problem
 has nothing to do with PHP.

 How mail is delivered depends on the OS you are using, but usually it
 will get passed to a local MTA which then handles delivering it, at
 which point PHP involvement ends.

 I suggest you look at the Received headers in the messages your getting.
 You should be able to see which server is causing the delay.

Yup, 100% agree that it's not PHP.

Reasons for delays can include a dodgy ISP just taking a long time to
process things, or more typically some form of greylisting e.g. one
MTA deliberately telling the delivering MTA to P*ss off for a while -
compliant servers will resend the mail after a while at which point it
will be accepted. This is a spam prevention technique. See:
http://en.wikipedia.org/wiki/Greylisting

Col

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 
9/1/2007 4:20 PM


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: mail() takes too much time

2007-09-03 Thread Colin Guthrie
Matthew Lasar wrote:
 I run pretty simple mail group distribution program that uses php/mysql
 and the mail() function. I adapted it from an open source script. Most
 of the time it runs well. But it does take a while to run through all
 150 members of the list. So I'm half glad that I don't have a list of
 1000 people or more.
 
 Any way to optimize the mail function in terms of speed? Probably too
 vague a question, sorry.

Yeah it is fairly vauge.

I guess one obvious way is to ask whether or not you send the same mail
content to people but just do it 150 times? Or whether you customise the
mail to each person putting in e.g. their name at the top etc. If the
former then you can send the mails in batches of about 30-50 at a time
using the BCC header rather than a direct To header (set the To address
to e.g. yourself or a dummy address that works but is a bitbucket). That
way there will be considerably less calls to mail() to process.

The other way would be to write the mails to a database and process them
later. You could write a special cronjob that runs every few minutes and
pushes them out in the background. It could run every minute or two
(with file locking to prevent more than one instance at a time) and
either process all mails it can or just do about 50 of them and then
quit and leave the rest for the next run. Obviously you have to manage
it to make sure that it can physically catch up with how many mails are
being generated but this isn't too hard.

There are lots of other ways too - I've even seen some people program a
MySQL UDF to send mail and let the database do the work, but I would
have serious doubts about that approach for this use!!

Col

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: mail() takes too much time

2007-09-03 Thread brian

Matthew Lasar wrote:
I run pretty simple mail group distribution program that uses php/mysql 
and the mail() function. I adapted it from an open source script. Most 
of the time it runs well. But it does take a while to run through all 
150 members of the list. So I'm half glad that I don't have a list of 
1000 people or more.


Any way to optimize the mail function in terms of speed? Probably too 
vague a question, sorry.




Have a look at the Swift Mailer package:

http://www.swiftmailer.org/

I use it to send out well over 1000 mails at a time and it works quite 
fine for me. Check out the anti-flood extension for sending large batches:


$swift = new Swift(new Swift_Connection_SMTP('localhost'), 'domain.org');


/* 100 mails per batch with a 30 second pause between batches
 */
$swift-attachPlugin(new Swift_Plugin_AntiFlood(100, 30), 'anti-flood');


/* list of recipients is an object wrapping an array
 */
$recipients = new Swift_RecipientList();
$recipients-addTo('[EMAIL PROTECTED]', 'foo bar');


/* headers
 */
$message = new Swift_Message('the subject');
$message-setCharset('utf-8');
$message-setReplyTo('[EMAIL PROTECTED]');
$message-setReturnPath('[EMAIL PROTECTED]');
$message-headers-set('Errors-To', '[EMAIL PROTECTED]');


/* multipart is a breeze
 */
$message-attach(new Swift_Message_Part($plain_content));
$message-attach(new Swift_Message_Part($html_content, 'text/html'));


/* if you're sending the mails from, eg. an admin page ...
 */
set_time_limit(0);
ignore_user_abort();
flush();


/* send it out
 */
$num_sent = $swift-batchSend($message, $recipients, new 
Swift_Address('[EMAIL PROTECTED]', 'from'));


This really should be rolled into the PEAR project, IMO.

brian

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: mail() takes too much time

2007-09-03 Thread Stut

Matthew Lasar wrote:
I run pretty simple mail group distribution program that uses php/mysql 
and the mail() function. I adapted it from an open source script. Most 
of the time it runs well. But it does take a while to run through all 
150 members of the list. So I'm half glad that I don't have a list of 
1000 people or more.


Any way to optimize the mail function in terms of speed? Probably too 
vague a question, sorry.


Don't use the mail function. On a unix-based system it will pass the 
message directly to sendmail which will attempt to deliver the message 
in realtime. When you're sending a large amount of mail that sucks.


Your best option is to switch to using a system that connects to the 
SMTP server on localhost directly. That way you can dump each message on 
to the local MTA quickly and then forget about it.


As an example one of the newletters I maintain has over 300,000 
subscribers and the system that sends the emails (written in PHP of 
course) takes less than 6 hours. The mail queue on that machine gets 
very big and then it's up to the MTA to work through sending the 
messages as quickly as it can (which usually takes about 28 hours).


-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: mail() takes too much time

2007-09-03 Thread Colin Guthrie
Stut wrote:
 Matthew Lasar wrote:
 I run pretty simple mail group distribution program that uses
 php/mysql and the mail() function. I adapted it from an open source
 script. Most of the time it runs well. But it does take a while to run
 through all 150 members of the list. So I'm half glad that I don't
 have a list of 1000 people or more.

 Any way to optimize the mail function in terms of speed? Probably too
 vague a question, sorry.
 
 Don't use the mail function. On a unix-based system it will pass the
 message directly to sendmail which will attempt to deliver the message
 in realtime. When you're sending a large amount of mail that sucks.
 
 Your best option is to switch to using a system that connects to the
 SMTP server on localhost directly. That way you can dump each message on
 to the local MTA quickly and then forget about it.

I don't think this is correct, at least on my system. I know this
because I deliberatly ban internal machines on my network from
delivering mail to outside server (internal LAN cannot connect to port
25 on any system other than our gateway - this is to stop any windows
machines that may sneak into my network from spamming the world!).

When I test web apps locally I have to watch to override email addresses
such that I don't try to sent to real people but when I forget, they all
end up stuck in my local machine's MTA. So for that reason, mail() must
speak to my MTA, not try to deliver directly. Perhaps it depends on your
sendmail implementation? I prefer postfix (which has a sendmail
compatible binary). YMMV.

Col.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: mail() takes too much time

2007-09-03 Thread Stut

Colin Guthrie wrote:

Stut wrote:

Matthew Lasar wrote:

I run pretty simple mail group distribution program that uses
php/mysql and the mail() function. I adapted it from an open source
script. Most of the time it runs well. But it does take a while to run
through all 150 members of the list. So I'm half glad that I don't
have a list of 1000 people or more.

Any way to optimize the mail function in terms of speed? Probably too
vague a question, sorry.

Don't use the mail function. On a unix-based system it will pass the
message directly to sendmail which will attempt to deliver the message
in realtime. When you're sending a large amount of mail that sucks.

Your best option is to switch to using a system that connects to the
SMTP server on localhost directly. That way you can dump each message on
to the local MTA quickly and then forget about it.


I don't think this is correct, at least on my system. I know this
because I deliberatly ban internal machines on my network from
delivering mail to outside server (internal LAN cannot connect to port
25 on any system other than our gateway - this is to stop any windows
machines that may sneak into my network from spamming the world!).

When I test web apps locally I have to watch to override email addresses
such that I don't try to sent to real people but when I forget, they all
end up stuck in my local machine's MTA. So for that reason, mail() must
speak to my MTA, not try to deliver directly. Perhaps it depends on your
sendmail implementation? I prefer postfix (which has a sendmail
compatible binary). YMMV.


It depends a lot on how your machine is configured, but the way I 
described it is usually the way it works. Note that if sendmail cannot 
send the messages immediately it will pass it into the local mail queue 
and that's probably what's happening on your system because sendmail 
will find port 25 blocked.


-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: mail() takes too much time

2007-09-02 Thread Colin Guthrie
Stut wrote:
 Unless your script is actually hanging for the 4-6 hours this problem
 has nothing to do with PHP.
 
 How mail is delivered depends on the OS you are using, but usually it
 will get passed to a local MTA which then handles delivering it, at
 which point PHP involvement ends.
 
 I suggest you look at the Received headers in the messages your getting.
 You should be able to see which server is causing the delay.

Yup, 100% agree that it's not PHP.

Reasons for delays can include a dodgy ISP just taking a long time to
process things, or more typically some form of greylisting e.g. one
MTA deliberately telling the delivering MTA to P*ss off for a while -
compliant servers will resend the mail after a while at which point it
will be accepted. This is a spam prevention technique. See:
http://en.wikipedia.org/wiki/Greylisting

Col

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php