mail() timeout issue - php

When I execute my email script via browser a timeout fatal error is returned (unless I drastically increase the execution time, then it will run ok, not the solution I'm looking for). The email is sent tho, but it takes forever (5 min. average) to arrive (at my inbox)!
(Considering that via command line it works perfectly I think that SMTP at php.ini is certainly well configured.)
So this is the code executed by browser request:
<?php
mail('amatos#example.com', 'test subject', 'test body', 'From: Andre Matos <amatos#example.com>');
?>
and when I run this same (is it really the same? I'm starting to doubt myself) code via command line:
php -r "mail('amatos#example.com', 'test subject', 'test body', 'From: Andre Matos <amatos#example.com>');"
it works perfectly! The script runs, it stop and the email arrives instantly (2/3 seconds).
So, what can cause this difference and how to fix it? Any ideas?
Thanks in advance.
[edit] some extra info:
- the machine is windows
- the server is localhost
- php.ini is the same for both the browser and the cli instance
[edit2]
Thank you all for trying to guess which was the problem. I placed the question hopping that someone had the problem before and knew of something specific. Given nothing specific showed up and none of the suggestions really worked, I've decided to accept the one that allowed me to reach more conclusions about the problem... +1 For all your helpful knowledge/thoughts (/guesses) :-)

I've hypothesised some couses, but I used to linux and on windows I can olny guess:
php_cli and mod_php are 2 different binaries, mod_php can be slightly damaged
php_cli and mod_php use 2 different users, the network profile of apache user can be the problem (dns, firewall, proxy...)
your php script is on "problematic" location or contains some problematic character, but your cli script is by param, try to execute same script: php -f z:\path\to\php\mail.php

Given this note from http://php.net/manual/en/function.mail.php, it seems very likely that the issue is with the MTA and not PHP directly:
The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Perhaps it has something to do with the way the MTA is responding to the particular user, or user-specific firewall rules for outgoing mail connections on your machine. Can you run the command-line as the web server user rather than yourself? If so, does that re-create the problem from the command-line?
How about having the web server execute the command-line PHP rather than the parsed PHP file? (For example, perhaps you can run a batch script via CGI.) Does that solve the problem?
(Sorry that these are more guesses than definite answers.)

Just to be clear: The php-instance used by the script is the same as the one used by the command line code?
Many web hosts use smtp-relay, which will gather a bunch of emails and send them all at once, so it won't be strange behaviour if your mail is late. However, the long execution time is not normal.

Before you send your mail in the script make sure to print out the ini_get() of the variables SMTP, smtp_port, and sendmail_from and be certain that these are working values. PHP running on Windows does not have the benefit of sending mail out via Sendmail, and whatever the PHP devs cobbled together for it is sketchy at best.
I always like to test mail via telnet so I can see if the server is giving an error that is not being passed back properly by the client:
c:\> telnet smtp.domain.com 25
220 smtp.domain.com ESMTP Postfix
helo mailtest
250 smtp.domain.com
mail from: user#local.com
250 2.1.0 Ok
rcpt to: user#remote.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
from: user#local.com
to: user#remote.com
subject: test mail
this is a test message
.
250 2.0.0 Ok: queued as 42AD8364FE0E
quit
221 2.0.0 Bye

Try to set username and password to "From" mail id. so it can authenticate and sends the mail quickly.
Have you tried PHP mailer?
In my observation its sends mails within seconds. Below example will give you a quick look how to use php mailer class.
include "class.phpmailer.php";
$msg="Hello! This is a test..."
$mail=new PHPMailer();
$email="someone#friend.com"; //person who receives your mail
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "admin#example.com"; //your mail id
$mail->Password = "sdfsd441"; //password for your mail id
$mail->SetFrom('admin#example.com', 'admin'); //from address
$mail->AddAddress($email);
$mail->Subject ="Test Mail";
$mail->Body = $msg;
$mail->IsHTML(true);
$mail->MsgHTML($msg);
$mail->Send();
EDIT :
In PHP manual they stated like this,
The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
so that may cause delay? I think this link might help you.

My first guess would be that the browser version of your mailing has already a context or connections ready for the sending.
On the contrary, the direct (php -r) execution has to load the mailing context.
To confirm this idea, you can make a loop for sending 10 mails and check wether the mails after the first are much quicker.

Related

Ubuntu sendmail only sends to localhost

I have set up a LAMP server with sendmail on Ubuntu 14.04.
When sending mail, either from terminal or PHP mail(), it won't work properly.
When I use mail("user#localhost","test","test"); it sends it correctly, and I can read the message with mail from terminal, but when sending to Gmail address, message won't go trough.
Same outcome when using "test" | mail -s "test" xyz#gmail.com" from server terminal.
Any idea how to fix this?
I have been Googling for several hours now, I have tried everything without finding working solution, and I'm starting to lose hope.
If you're running Ubuntu here is a much simpler solution:
issue from command line >
apt-get install ssmtp
Then edit the configuration file in /etc/ssmtp/ssmtp.conf
A sample configuration to use your gmail for sending e-mails:
# root is the person who gets all mail for userids < 1000
root=your#email.com
# Here is the gmail configuration (or change it to your private smtp server)
mailhub=smtp.gmail.com:587 (leave this the way it is)
AuthUser=your#gmail.com (just change this)
AuthPass=yourGmailPass (and change the password for your gmail account)
UseTLS=YES (leave this the way it is)
UseSTARTTLS=YES (leave this the way it is)
Note: Make sure the "mail" command is present in your system. mailutils package should provide this one in Debian based systems.
Then try send emails again, send it to another domain #gmail, #aol, #yahoo, #privatedomain besides localhost email addresses.
There're tons of possible problems, so you need to get some logs of error that happening.
Most probably, Google just rejects your email as your server is not correctly configured.

PHP Mail Error in Microsoft WebMatrix

I am a newbie in Web Development an I am currently learning PHP and MySQL. I have read HeadFirst PHP and MySQL, and tried the examples. But The PHP Mail() function doesn't work on my Local Machine. I have tried the script on a web server but nothing happens, the mail isn't sent. Please Help me. I have configured the PHP.INI file to send emails but still the problem persists.
<?php
$to = "me#me.com";
$sub = "hello";
$msg = "Hello, how are you?";
//Mail Function
mail($to,$sub,$msg);
?>
I am using WebMatrix with PHP 5.2 installed. Please help me, I am trying out this one since last 2 hours! I am stuck!
mail() uses 'localhost' to send - it generally assumes it's on Linux.
You will need to aquire a basic SMTP server and run it on windows, OR you may be able to use the SMTP server of your're ISP.
Whichever option, you will need to edit your php.ini, you will find:
[mail function]
; For Win32 only.
;SMTP =
you must set SMTP to the ip/port of a mailserver - again wither run one locally or use your ISP.
EDIT
You could try this approach - I have personally never tried to use GMail for sending: http://www.linuxquestions.org/questions/programming-9/php-pear-mail-packege-support-security-through-ssl-586976/
But The PHP Mail() function doesn't work
Yes it does. The problem is either with how you configured PHP or with the MTA you configured it to use. You'd need to provide details of both for us to understand why mail is not getting sent.
The above answer also is applicable to those who use a simulated local IIS e.g. the WebMatrix on IIS Express users who reside within a corporate network with a SMTP machine available.
To be able to send emails out from within it one needs to edit the PHP.INI file (found typically in \Program Files (x86)\IIS Express\PHP\v5.3) and replace 'localhost' with the SMTP server IP or DNS name.

PHP mail() failing to send to internal addresses

I am trying to send out password reset links for when users forget their password to login to a system I am creating. The problem is, the smtp server is supposedly not configured on the server my system is hosted on. So whenever I try to use the php mail() function to send an email to an internal email address, the emails fail to send, but outside email address (tested with a gmail account), the emails go through. I believe this is because my server is sending directly out to the internet instead of passing through an internal smtp server to resolve where our domain emails should be sent. I was wondering if anyone knew how to configure this on an Xserve or if they knew how to specifically tell the php mail() function where to initially send the email. Any help or pointing in the right direction would be extremely helpful.
Thank you!
mail() doesn't send mail, it just hands things over to the local SMTP server. It does NOT reach out to the recipient's server to deliver the mail. In real world terms, mail() walks your letter down the street and drops it into the neighborhood mailbox. After that, it has absolutely nothing more to do with mail delivery.
Check your local SMTP server's logs to see why the local mails aren't being delivered. There should be a line or two saying why it's registered. Perhaps the local MTA (mail-transfer agent, aka the local "mail man") isn't configured properly.
You can control mail() with it's settings.
This might not solve your overall problem, but hopefully it's useful. This related answer has more information.
We just addressed this problem internally here. Hopefully this will help you as well.
Our environment:
Ubuntu 12.04 LTS
PHP 5.3.10
We could telnet into our SMTP server and send mail from our web server, so it wasn't a permissions issue. When attempting to mail externally, all works perfectly. When attempting to mail internally, silent failure.
Our PHP is using sendmail, which by default, attempts to relay mail to 127.0.0.1. Point this at your SMTP server by editing /etc/mail/sendmail.cf. Update the line from:
# "Smart" relay host (may be null)
DS
to
# "Smart" relay host (may be null)
DSyour.smtp.server.com
Restart sendmail and try sending yourself an email via PHP.
This is something that occurs on Parellels’ PLESK server administration software.
Find your ‘main.cf’ configuration file for PostFix, which for CentOS 6, is located at
/etc/postfix/main.cf
If you can’t find it, do a
which postfix
SSH command to at least see where Postfix is on your server.
Then, open the file up through a text editor, or in the Linux shell, and make these lines (should be at the end of the file, around line 677) :
virtual_mailbox_domains = $virtual_mailbox_maps, hash:/var/spool/postfix/plesk/virtual_domains
virtual_alias_maps = $virtual_maps, hash:/var/spool/postfix/plesk/virtual
virtual_mailbox_maps = hash:/var/spool/postfix/plesk/vmailbox
commented out like this :
#virtual_mailbox_domains = $virtual_mailbox_maps, hash:/var/spool/postfix/plesk/virtual_domains
#virtual_alias_maps = $virtual_maps, hash:/var/spool/postfix/plesk/virtual
#virtual_mailbox_maps = hash:/var/spool/postfix/plesk/vmailbox
Then, restart the Postfix service
sudo service postfix restart
Apache while your at it (can’t hurt), and voila! Your email address should be receiving those emails now. This also doesn’t affect any of your regular emails or anything else, either.

How to send an email using Zend_Mail, sendmail, and localhost?

I'm developing a zend framework application that includes a simple email function. The development version is running on my computer, which is running Ubuntu. The production version is going to run on a production server.
When trying to send a test email to myself, I get an exception with the message: "Unable to send mail". I don't know if this is an environment issue, or a code issue. I'm not using a transport so I think it is defaulting to Zend_Mail_Transport_Sendmail. Here's my code:
public function sendtestAction()
{
$mail = new Zend_Mail();
$mail->setFrom('test#aol.com', 'Test Email');
$mail->addTo('my#email.com', 'My Name');
$mail->setSubject('This is just a test.');
$mail->setBodyText('This is only a test.');
$mail->send();
}
Update: I tried a different approach by setting the SMTP transport to use localhost:
transport = new Zend_Mail_Transport_Smtp('localhost');
Zend_Mail::setDefaultTransport($transport);
I got a different error this time: "Connection refused" Not sure what that means. Maybe I haven't set something up yet?
Update: I guess I didn't have an SMTP server installed/setup. This tutorial made it really easy for me to get an SMTP server up an running. Now both of the code samples above work.
It sounds like you need to configure an MTA, or find one that you can send to. Ubuntu desktop should set one up by default, probably either exim or postfix, but if you haven't configured it, it will unlikely to be running.
You don't want to set the default transport if you wish to use sendmail (it is the default) and SMTP is different.
That it doesn't send the emails suggests that sendmail or the MTA on your server is not installed/not setup correctly.

php mail() not working windows 2003, IIS SMTP

I'm getting this problem:
PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable to relay for chris.mahan#gmail.com in c:\inetpub\wwwroot\mailtest.php on line 12
from this script:
<?php
$to = "chris.mahan#gmail.com";
$subject = "test";
$body = "this is a test";
if (mail($to, $subject, $body)){
echo "mail sent";
}
else {
echo "problem";
}
?>
section from php.ini on the server:
[mail function]
; For Win32 only.
SMTP = server.domain.com; for Win32 only
smtp_port = 25
; For Win32 only.
sendmail_from = support#domain.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
(note that "server" and "domain" refer accurately to the actual server and domain name)
In IIS, SMTP is running. Under "Access" tab, "Relay" button, the Select which computers may relay through this virtual server is set to checkbox "only the list below" and on the list is "127.0.0.1(xxx.xxx.xxx.xxx)" (x's representing actual server IP address).
Server is running Windows Server 2003 Service Pack 2, fully patched as of 5 PM Sept 1st 2008. I assume it is running IIS7 (how to check?).
Any ideas?
In reponse to Espo: This machine is hosted at a datacenter. We do not want to use a gmail account (were doing it, want to move away from that). Windows server 2003 comes with its own SMTP server.
Update: Per Yaakov Ellis' advice, I dropped all relay restrictions and added the server IP to the allowed list (using the reverse DNS button provided) and the thing started working.
Thanks to both Espo and Yaakov for helping me out.
Try removing the IP restrictions for Relaying in the SMTP server, and opening it up to all relays. If it works when this is set, then you know that the problem has to do with the original restrictions. In this case, it may be a DNS issue, or perhaps you had the wrong IP address listed.
You are using the wrong SMTP-server. If you you are only going to send emails to your gmail-account, have a look at my answer here.
If you also need to send email to other accounts, ask you ISP for your SMTP-details.
EDIT: I think it is always better to use the ISP SMTP-server as they (should) have people monitoring the mail-queues, checking for exploits and updating the mail-software. If you business is developing web-applications it is almost always best to stick with what you do, and let other people do their stuff (eg running mailservers).
If you still for some reason want to use you local SMTP server, the first thing would be to rule out the php-part. Try folowing KB153119 and then check you SMTPServer IISlog for errors.
EDIT2:
That KB-article says it is for exchange, but the same commands are used for other SMTP-servers (including IIS) as well, so please try and see if you can send mails using the examples from the article.
#Espo: I'll do that re KB153119. Thanks.
About the mail server: I hear you.
I'll update when I uncover more.
#Espo, the article in question relates to Exchange servers, not IIS7.0 SMTP server.
From the summary: This article describes how to telnet to port 25 on a computer that runs Simple Mail Transfer Protocol (SMTP) services to troubleshoot SMTP communication problems. The information in this article, including error messages, only applies to issues when attempting to resolve SMTP communication issues with Microsoft Exchange-based servers and is not intended for general troubleshooting purposes.
I had the same problem, php 5 on iis6, 2003 server. Php always failed when trying to use mail().
I've managed to get it accepting mail from php by changing the Relay Restrictions from 'Only the list below' (which is empty by default) to 'All except the list below' .
The relay restrictions can be found in the Access tab in the smtp servers properties screens.
Of course if the server is open to the internet then one would have to be more sensible about these relaying restrictions but in my case this is on a virtual server on a dev box.
hope that helps.

Categories