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 127.0.0.1 in my /etc/hosts file. I now just have:

cat /etc/hosts

::1 localhost
127.0.0.1 localhost loghost
202.12.122.129 web020.mydomain.com web020 mx0 # this machine WAN
192.168.100.152 web021.mydomain.com 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.



see http://us3.php.net/manual/en/function.mail.php

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


I changed the above information to reflect my current configurations:


domain MyDomain.com
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:


<?php

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.

Example:

PHP Code:
<?php
$fd = popen("/usr/sbin/sendmail -t","w") or die("Couldn't Open Sendmail");
fputs($fd, "To: recipient@hisdomain.com \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");
pclose($fd);
?>


Danny's DNS TXT Record Setting

v=spf1 ip4:202.27.122.192/26 a mx a:web020.mydomain.com a:www.mydomain.com mx:mydomain.com ~all


Reference:
http://ubuntuforums.org/showthread.php?t=873616

http://ubuntuforums.org/archive/index.php/t-658381.html

http://bbpress.org/forums/topic/how-can-i-remove-the-delay-when-users-are-emailed

http://gala4th.blogspot.com/2009/03/spam-mail-what-everybody-ought-to-know.html

No comments: