PHP script time and smtp send mail
I have a Cakephp app that has users with ids and emails (emails might not be a valid address).
So I use pairmail and loop开发者_运维百科 through the users table to send emails.
foreach($users as $u)
{
if($this->sendMail($u['to'], $u['subject'], $u['text_message'], $u['html_message']))
//update database with sent email
}
It works fine with 1 or 2 addresses, but it gets stuck when I run it through all the users. Is it because I have invalid addresses or my php script times out?
- If you have a lot of mails to send write a script that is run in the background (called by cron or whatever); or else your request will time out.
To cut down the delivery cruft you could:
- Validate (with a regex) and sanitize (trim, etc.) email addresses before you try to send emails to them. People are stupid and put www. in their address or mistype them.
- Catch delivery errors, see what happend (Why didn't the mail server accept the email? Does the recipient exist?) and if the address is wrong mark and ignore it in future delivery runs.
I haven't used PairMailer, but I've run into this problem with SwiftMailer. This happens when the email is invalid and is not accepted by the SMTP server. This happens before the mail is sent out. This may take a few seconds and a few emails like this will add up.
A work around for this is to run the script as a cron job and build up a list of the ones that are giving you trouble. You could write a cake shell that does this so you can run it from the command line : http://book.cakephp.org/view/1107/Creating-Shells-Tasks
精彩评论