tags:

views:

386

answers:

7
+1  Q: 

PHP mail problem

Hi guys,

I am trying to implement a password reset functionality on my company website where if a user needs to reset her password, she can click on a link and a new, randomly generated password will be sent to her inbox.

I am using the PHP mail() function. Now, I am running PHP5 on an Ubuntu machine. In php.ini I have declared SMTP as the IP of the machine running the mail server and smtp_port as 25. Further, I have tried to telnet into the mailserver on port 25 and send a mail - it works (my work terminal is Windows).

The problem is that the mail is not being sent / received - the PHP script calling mail() hangs for about 1 minute at the end of which mail() returns true.

The worst part is that I am out of ideas on how to even find out where the problem lies - with PHP, with Ubuntu, with the mailserver or with the code!

Can somebody please help?

+1  A: 

Can you check your mail.err and mail.log files ?

Lliane
I am absolutely new to Linux. I can't seem to find mail.err and mail.log files :(
Crimson
The /var/log/ directory usually holds all the logs for most linux distros
shambleh
mail.err has the following entry: "My unqualified host name (OCR) unknown; sleeping for retry". I do not know what it means.
Crimson
A: 

Your email server probably needs authentication ("POP before SMTP" or ASMTP - Authenticated SMTP). Since you've probably recently checked your email from your Windows machine, your IP address will be authenticated and allowed to send email for a short time: from your Ubuntu machine, probably not.

On the Ubuntu machine, see if you can NOT use the IP address of your SMTP server: PHP should work out how to send the email out anyway. Otherwise, you'll have to find out how to authenticate against your email server and implement it into PHP.

Richy C.
No authentication needed. I sent a mail using telnet (Ubuntu) to the mailserver - it worked without authentication.
Crimson
Ok, try removing the entry in the php.ini file referencing the IP address of the server to see if PHP is able to send the email using sendmail.
Richy C.
A: 

Try to send an email with mail program from shell. First see if that works.

grigy
Sending a mail from shell - telnet (Ubuntu) worked.
Crimson
Then it must be the php's configuration. Can you see how the mail function is configured in php.ini?
grigy
[mail function]; For Win32 only.SMTP = 192.168.1.10smtp_port = 25; For Win32 only.sendmail_from = [email protected]; For Unix only. You may supply arguments as well (default: "sendmail -t -i").sendmail_path = /usr/sbin/sendmail
Crimson
+1  A: 

I haven't looked in php.ini recently, but doesn't it say:

[mail function]
; For Win32 only.
;SMTP =

Emphasis on the For Win32 only.

You may want to look at PEAR::Mail instead. When pear.php.net is actually responding, that is.

R. Bemrose
I read in a tutorial that linux will also successfully use the SMTP field if sendmail_path is not specified. Is that incorrect?
Crimson
To my knowledge, it's not correct. In the notes section for the mail command, it even says "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 implies that the unix version always uses sendmail.
R. Bemrose
I will be using PEAR::Mail then. Am leaving for home now (Its 10 in the night here). Will write about it in the morning :)
Crimson
Swift Mailer would be a better alternative to PEAR::Mail.
Alix Axel
PEAR::Mail worked
Crimson
A: 

Your ISP may be the problem. Port 25 is frequently blocked by ISPs in order to prevent mass spamming. Try changing to a non-standard port and see if that helps.

Edit: And here is something detailing the issue (although their solution is to relay to the ISP mail server, not change ports).

shambleh
The machines are all in an internal LAN (no gateways even)
Crimson
If that is the case, the logs would also be my next stop.
shambleh
A: 

You need the Zend Framework SMTP mail class for example because this stupid braindead php implementation does not work with SMTP ports on Unix.

Please subscribe to the PHP Core mailing list and complain about it - maybe they change it if more and more people are coming in and they see that not everyone what sendmail.

Lothar
A: 

It is also possible to bypass the mail() function of php by using a mail library. For example SwiftMailer (http://swiftmailer.org/) This is a really good class for safely sending e-mails (plain/html).

With this class you are able to connect directly to an SMTP server and so bypassing the server configuration.

Marien