PHP mail problem

2019-05-31 01:37发布

问题:

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:

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.



回答2:

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



回答3:

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.



回答4:

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



回答5:

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).



回答6:

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.



回答7:

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.



标签: php smtp