sending large amounts of email in a loop - php

I have a system that sends lots of emails reports ( about 500+ emails per day). I am not a spammer :) It's not really big amount of e-mails, but they are send in a loop and I often get this error "PHP Warning: mail(): Could not execute mail delivery program". I know there is PEAR::Mail_Queue package for this issue. But can you please let me know if that package is really useful thing, or do I need to look for something else. Thanks a lot

You can use PEAR's Mail_Queue package to send mails through an SMTP server directly - also it will work in the background and so won't delay or increase your script execution time.
Another advantage of using the Mail_Queue package is that you can retrieve the esmtp id of each mail sent for logging purposes - I don't know if this is possible with the Zeta Mail component or any other one.
I've developed a number of public-facing, and also intranet only, solutions that use this component and haven't had any major problems with it.

"PHP Warning: mail(): Could not execute mail delivery program"
...means exactly what it says on the tin. PHP will just hand the email off to an external program on a Linux/Unix/POSIX system (as defined by sendmail_path in php.ini). And in some cases that is returning an error. It's not a fault within the PHP code.
Unless you've got a really badly configured MTA, then the problem will not be resolved by using an SMTP connection instead.
You need to look at the logs from your MTA to see why its failing to send the mail - or wrap the mail executable in a logging script.

You gotta use sleep() between mails, that solves this.

An alternative could also be to send mails through an SMTP server directly. This saves you the round trip of PHP calling sendmail (or whatever MTA is used) "through the shell" in order to deliver the mail. For example the Zeta Mail component allows you to send mails directly through an SMTP server without needing a special extension for this.

Related

how can i process incoming mail with php?

I assume I will need to point MX records at my server (LAMP), -- what processes the incoming e-mail message?
Are there any existing PHP libraries to do this?
You don't want to use PHP as a mail server. You've got two options:
Set up a classic email server (postfix, sendmail, exim, etc) that delivers new messages to a local mailbox. Use IMAP or POP to access that mailbox from PHP, and pull messages out of it. Alternatively, this same method may be used with (virtually) any remote mail service as well, thus relieving you of the duty of administering a mail server. (Which you'll likely find to be not worth it for the sake of one mailbox.) This method would usually be run via cron every few minutes, so you're not going to get "instant" activation if that's a requirement.
Set up a classic email server (postfix, sendmail, exim, etc) and use procmail or similar to intercept messages at delivery time, and pipe them to a PHP script. This method will fire the script the instant the email arrives, so you'll have no lag time like in #1. However, it's more difficult to configure (especially if you haven't maintained a mail server before) and won't work with most external hosted email services.
Use a pipe alias to receive the emails.
I would recommend you to do processing in Perl (python is also ok, but Perl has very similar syntax to PHP), which is much more suitable for the task. You can also find a lot of libraries through CPAN there.
http://search.cpan.org/~rjbs/Email-Simple-2.100/lib/Email/Simple.pm

Why does PHP mail() require a mail program like sendmail/postfix/etc. for sending emails?

Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
Why does the PHP mail() function require a mail program like sendmail/postfix/etc. for sending emails?
I ask because sending email is a client action and not something that needs to run a server.
What is so complicated about creating a native PHP mail function without the need to install a sendmail/postfix/etc. program that has excessive functionality as a server that can get requests and not just send email as a client?
Most likely: sendmail existed before PHP's mail(), so in the true spirit of *nix:
Why re-create functionality when it already exists on the CLI?
The reason you want to hand the mail off to something else is that Mail is a complicated beast that is best deferred to something better designed to handle that.
A simple case is when you mail something to a server that is offline. It's "OK" for a mail server to go offline, there's built in logic to retry message sends in the operations of the mail servers. But if you simply open up an SMTP socket and start barking protocol, and it fails, then you've lost that queueing capability that you get "for free" with the mail servers.
Mail is just one of those things best delegated to those ancient systems that have all of the lore and details coded in to them over years of painful interoperability testing and implementation.
PHP doesn't send mail itself, but delegates to another program to send mail for it.
You can get around this restriction by using the PEAR Mail package instead, which has SMTP support.
The reason is because the mail() function is not an MTA, only a "encoder" for the actual MTA.
Why not? I believe it would be unpractical to implement a decent and secure MTA setup with PHP alone.
EDIT: What exactly does an MTA do?
When a mail server forwards (routes) email to another server, it acts like a client and uses SMTP to forward the email. The client part is called an Mail Delivery Agent (MDA) and is often a distinct piece of software.
This part of the sendmail tutorial is helpful.
It's called "sendmail" for a reason. Networks' used it for decades for inter-communication. Whether you know it or not, that IS how mail gets sent. -- By a program confined to an RFC protocol and designed to format, package and deliver data to ... other servers. It can run as a daemon sitting on a port or from an instance invoked at the cmdline. It's a simple tool, and an efficient one.
-Gama Xul
(*nix guy)
Trying to send mail with something other than a mail-sending-program (like a web-browser client or a PHP language mod) would be like trying to cut something with a gun. There's a lot more going on there and too many conflicting conditions could hang it up. It just wasn't designed for it and wouldn't function efficiently.

Alternative for PHP mail

In relation to an earlier question I'm looking for an alternative way to send an order from my website to the division in my company that processes the order.
Currently I use PHP mail(), but frequently this gives problems. Big delays occur. Are there alternatives to PHP mail() that pushes the order to my company? So I prefer not to poll the website.
mail() is fine for simple stuff, but often you want a more robust library that has solved the mail problem in PHP.
My personal choice is Swift Mailer.
Also from reading your other question, could this be of benefit
Your app writes the order email to database, in a queue.
You have a Cron running, that sends say 30 emails every 10 minutes.
It then removes the last sent 30, ready to be processed again in 10 minutes.
The advantage is that when you have a queue of emails to be sent out, they can be processed in batches, and also you will have a copy in the database if the mail fails. It will be easier to query the database then chase up the mailed that was undelivered (or delivered late).
Currently I use PHP mail(), but frequently this gives problems.
I'll bet you a lot of money it does NOT frequently give problems.
I have never come across problems in PHP's mail function. I have seen problems with bad php.ini settings for mail, and a host of problems with the programs which process mail after it has left PHP. Your use of the word "frequently" implies that it works some of the time, so, in the absence of frequent updates to php.ini, the problems are all on your mail handling infrastructure.
Indeed - I would recommend you go have a look at the PHP bug list - there will be lots of reports about problems getting mail from scripts to users inboxes - but none of them will be due to a failure of the PHP code.
So if the problem is elsewhere, using a replacement SMTP client will have no effect whatsoever (unless you configure it to bypass the bad MTA).
Understanding how email works, and why it fails, is far from trivial. When you add in to the mix, the lengths some people go to (usually undocumented) to prevent spam, it starts to become very complicated. Even if you had given precise details of your infrastructure and configuration, it would be difficult to even hazard a guess as to where why and how it is failing.
Certainly, you need to start by looking at your email logs and headers and checking your MTA and MUA configurations to start to resolve the problem.
C.
There is inherent insecurity in using SMTP to transmit orders. Not to mention the delays caused by all the routing, spam-checking, etc. that is caused by using the mail server. Are you sure you need to email the order? Wouldn't something more along the lines of a server-to-server HTTP transfer, perhaps XML-based, be a better option?
You could use a second server in the processing division that would receive the order from your web server via a protected (firewalled) connection that would process the order, in this case making it available to the person in the order processing division who reads and deals with the order.

Php Mail Function

I am using php sendmail() function in my projects. When I sent above 3 or more mails processing gets too slow. What is the problem? If I use PHPMailer, will this problem solve?
There are a few things that can be causing this to be slow.
You may be waiting for each email to be fully sent before sendmail() returns. This will mean opening a SMTP socket, talking to another mail server, sending the email and closing up the socket again.
We have a system that sends quite a few emails out in batches and we use PHPMailer for that. We open the socket once, send lots of messages then close it up again, and we get good performance on this (it can send several hundred emails in a single run).
If your message is identical to all recpients, I would recommend sending them all in a single email and BCC'ing everyone, as this takes a lot of pressure away from your script.
Another thing to check is local virus scanners. We used to have problems with the outgoing email scanner grinding the whole thing to a halt. Worth looking into.
Seems, your mail() functions has some restrictions applied to sendmail or some problems. Try sending mail via SMTP for example using this :http://swiftmailer.org/ and see if it will solve your problem.

proper way to send mailers using php?

I have php script to send 1,00,000 mails everyday..
it just runs all emails in loop and invoke mail() function..
I can't use mailinglist manager because. each mail has different content, unique clickurls.
is it the proper way to send mailers?
thank you
Sending thousands of emails from a PHP script is a bit messy at best. One of the real problems is to ensure that the script keeps running as long as necessary to do the job. You can do that if you keep resetting the time out for PHP and you have permission to do that, but there's still going to be a host of other problems.
The best thing to do is to build the email message and the list of recipients in PHP then hand the task of mailing off to something else - either a shell script or some kind of application that gets spawned off and detached from PHP in some way.
Also have you tried using SwiftMailer etc?
It should be alright, as long as you provide users a link to unsubscribe from the mailing list. Make sure you're on good terms with your SMTP server provider. Otherwise, the server will get blacklisted.
EDIT: Many SMTP servers have limitations on the number of emails you can send out in an hour. Find out this limit for your SMTP server and put a small delay() in your loop after sending out each email.
php mail(...) function?

Categories