Unable to Send Email Using PHP

tbdei picture tbdei · Jul 17, 2013 · Viewed 8.6k times · Source

Preview: I have been trying to send an email using Moodle for a long time and finally decided to test sending an email by using a standard PHP mail() function to test if mail is working fine.

BUT EVEN PHP DOES NOT SEND AN EMAIL!!

Problem Scenario:

This is my code for PHP:

$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
ini_set( "sendmail_from", "[email protected]" ); 
ini_set( "SMTP", "smtp.gmail.com" );  
ini_set( "smtp_port", "25" );
ini_set("username","[email protected]"); 
ini_set("password","password");
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

The Error which I get is :

    Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS                command first. sz6sm10013088pab.5 - gsmtp in C:\Program Files (x86)\Moodle\server\moodle\user\edit.php on line 252

I have tested my gmail server using Telnet and it is listening fine on Port 25. I have done everything that has been told by this error and other related posts

Tried: "ssl://smtp.gmail.com" but it would simply give the following error:

    Warning: mail() [function.mail]: Failed to connect to mailserver at  "ssl://smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files (x86)\Moodle\server\moodle\user\edit.php on line 252

even though openssl.dll has been uncommented in PHP.ini.

 ;extension=php_tidy.dll
 extension=php_xmlrpc.dll
 ;extension=php_openssl.dll;

Also, I have configured the php.ini file(php.ini-production and php.ini-development also):

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

But adding this to php.ini did not make much of a difference(therefore I resorted to set_ini()) as the server would coninue saying that "localhost smtp server cannot be configured on Port 25" though I had set the SMTP=smtp.gmail.com in php.ini.

Any help on this would be greatly appreciated. Thanks in advance.

Answer

Amal Murali picture Amal Murali · Jul 17, 2013

You've configured port 25 for the mail server.

The error message you're getting says that it's unable to connect to localhost:25.

Therefore you have two options:

  1. Install / Properly configure an SMTP server on localhost port 25
  2. Change the configuration to different port to which you can connect to:

.

  • Port for TLS/STARTTLS: 587
  • Port for SSL: 465

This support forum thread may be helpful.