Friday, March 27, 2009

PHP mail() function send sendmail slow delay issue

PHP mail() function send sendmail slow issue

I found that sendmail was slow on my machine because I had my hostname mapped to in my /etc/hosts file. I now just have:

cat /etc/hosts

::1 localhost localhost loghost web020 mx0 # this machine WAN web021 # this machine LAN

I also found that I needed to specify an additional_parameter to the mail function. The -f sendmail option sets the envelope sender address. Without this, sendmail refused to send my messages.


Now the PHP mail function returns almost instantly and my messages are sent successfully.

I changed the above information to reflect my current configurations:

nameserver my actual name server and not my router/gateway, as I am not behind a router, but I was when I configured the server
nameserver my backup name server

//domain: The local domain name.
//search: Search list for hostname lookup. This is normally determined by the domain of the local hostname.
//nameserver: The IP address of a name server the resolver should query. The servers are queried in the order listed with a maximum of three.

When I tested my sendmail script again, it fired off like a charm!

How can I remove the php delay when users are emailed?
I've noticed with certain plugins that email users, like favorites notification and others, that PHP will sit and wait until it gets a response back from the mail function.

Is there any kind of drop in replacement for mail() that will just queue the email and come back to php and load the rest of the page asap?

I think I found the answer to my question with a bit of sendmail/exim research.

Apparently adding "-odq" to the end of the mail() options will force it to queue only, which means instant response back to the user. However that delays mail of course until the next exim run. "-odb" is supposed to be the default, running the delivery in the background and not while mail() waits, but I seem to get the best of both worlds, instant delivery and no wait on mail() when I force it, so forced it is.

Recommended for other plugins that use mail:


mail($usermail, $subject, $message, "From: $adminmail\nX-Mailer: PHP/", "-odb -f $adminmail");

All PHP coders are aware of the mail() function in PHP, which can be used to send mails. On Linux systems we ussually have Sendmail program installed, if someone is facing a problem sending mails with mail() function, they can alternatively use Sendmail to send mails.
For sending mails using Sendmail we need to open a pipe to the sendmail program, which can be accomplished using the popen() function in php.


PHP Code:
$fd = popen("/usr/sbin/sendmail -t","w") or die("Couldn't Open Sendmail");
fputs($fd, "To: \n");
fputs($fd, "From: \"Your Name\" \n");
fputs($fd, "Subject: Test message from my web site \n");
fputs($fd, "X-Mailer: PHP3 \n\n");
fputs($fd, "Testing. \n");

Danny's DNS TXT Record Setting

v=spf1 ip4: a mx ~all


No comments: