Hi,
I have an application which sends mail wen a new post is made in my blog.my blog is setup in local server using xampp.wen i send mail to a single address its working fine and wen i tried to send mail to multiple address i get an error saying that invalid address format in TO field.Below is the code i have used..plz help me out...

function authgMail($from, $namefrom, $to, $nameto, $subject, $message)
	{
	 	
	$smtpServer = "tls://smtp.gmail.com"; //does not accept STARTTLS
	$port = "465"; // try 587 if this fails
	$timeout = "45"; //typical timeout. try 45 for slow servers
	$username = "user@gmail.com"; //your gmail account
	$password = "pass"; //the pass for your gmail
	$localhost = $_SERVER['REMOTE_ADDR']; //requires a real ip
	$newLine = "\r\n"; //var just for newlines
	 
	/*  you shouldn't need to mod anything else */
	 
	//connect to the host and port
	$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
	//echo $errstr." - ".$errno;
	$smtpResponse = fgets($smtpConnect, 4096);
	if(empty($smtpConnect))
	{
	   $output = "Failed to connect: $smtpResponse";
	   //echo $output;
	   return $output;
	}
	else
	{
	   $logArray['connection'] = "Connected to: $smtpResponse";
	   //echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
	   //echo $tadr;
	}
	 
	//you have to say HELO again after TLS is started
	   fputs($smtpConnect, "HELO $localhost". $newLine);
	   $smtpResponse = fgets($smtpConnect, 4096);
	   $logArray['heloresponse2'] = "$smtpResponse";
	  
	//request for auth login
	fputs($smtpConnect,"AUTH LOGIN" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['authrequest'] = "$smtpResponse";
	 
	//send the username
	fputs($smtpConnect, base64_encode($username) . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['authusername'] = "$smtpResponse";
	 
	//send the password
	fputs($smtpConnect, base64_encode($password) . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['authpassword'] = "$smtpResponse";
	 
	//email from
	fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['mailfromresponse'] = "$smtpResponse";
	 
	//email to
	fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['mailtoresponse'] = "$smtpResponse";
	 
	//the email
	fputs($smtpConnect, "DATA" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['data1response'] = "$smtpResponse";
	 
	//construct headers
	$headers = "MIME-Version: 1.0" . $newLine;
	$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
	$headers .= "To: $nameto $to1" . $newLine;
	$headers .= "From: $namefrom <$from>" . $newLine;
	 
	//observe the . after the newline, it signals the end of message
	fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['data2response'] = "$smtpResponse";
	 
	// say goodbye
	fputs($smtpConnect,"QUIT" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['quitresponse'] = "$smtpResponse";
	$logArray['quitcode'] = substr($smtpResponse,0,3);
	fclose($smtpConnect);
	//a return value of 221 in $retVal["quitcode"] is a success
	
	
	
	$a='Mail Sent Sucessfully';
	return($a);
	}

you say you have problems sending it to multiple addresses, but its fine for one. Maybe theres a format error, like you didnt put a , after each email or wrap the emails in "":

To: "user@address.com", "other@address.com"

Only a simple idea, but its all i could come up with. Apart from maybe you need to increase the $timeout length for more emails

i have tried all formats but i get delivery mail stating that wrong TO format...

i need a method to send mail through smtp in php by which i have to send mail to atleast 200 recepients at a time and it should be fast...thats my requirement now..plz help me out..

hello, i just named the script as mail.php and tried it but i am receiving the following error

Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in c:\program files\easyphp1-7\www\main.php on line 15

Warning: fsockopen(): unable to connect to tls:\smtp.gmail.com:587 in c:\program files\easyphp1-7\www\main.php on line 15

Warning: fgets(): supplied argument is not a valid stream resource in c:\program files\easyphp1-7\www\main.php on line 17

The whole script of mine looks like this but i made changes regarding user_name and password and even enabled the POP service in my gmail.Please help regarding this because the same script was working for U, thanks in advance.

<?php
function authgMail($from, $namefrom, $to, $nameto, $subject, $message)
    {

    $smtpServer = "tls://smtp.gmail.com"; //does not accept STARTTLS
    $port = "465"; // try 587 if this fails
    $timeout = "45"; //typical timeout. try 45 for slow servers
    $username = "user@gmail.com"; //your gmail account
    $password = "pass"; //the pass for your gmail
    $localhost = $_SERVER['REMOTE_ADDR']; //requires a real ip
    $newLine = "\r\n"; //var just for newlines

    /*  you shouldn't need to mod anything else */

    //connect to the host and port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    //echo $errstr." - ".$errno;
    $smtpResponse = fgets($smtpConnect, 4096);
    if(empty($smtpConnect))
    {
       $output = "Failed to connect: $smtpResponse";
       //echo $output;
       return $output;
    }
    else
    {
       $logArray['connection'] = "Connected to: $smtpResponse";
       //echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
       //echo $tadr;
    }

    //you have to say HELO again after TLS is started
       fputs($smtpConnect, "HELO $localhost". $newLine);
       $smtpResponse = fgets($smtpConnect, 4096);
       $logArray['heloresponse2'] = "$smtpResponse";

    //request for auth login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authrequest'] = "$smtpResponse";

    //send the username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authusername'] = "$smtpResponse";

    //send the password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['authpassword'] = "$smtpResponse";

    //email from
    fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['mailfromresponse'] = "$smtpResponse";

    //email to
    fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['mailtoresponse'] = "$smtpResponse";

    //the email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['data1response'] = "$smtpResponse";

    //construct headers
    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto $to" . $newLine;
    $headers .= "From: $namefrom <$from>" . $newLine;

    //observe the . after the newline, it signals the end of message
    fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['data2response'] = "$smtpResponse";

    // say goodbye
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 4096);
    $logArray['quitresponse'] = "$smtpResponse";
    $logArray['quitcode'] = substr($smtpResponse,0,3);
    fclose($smtpConnect);
    //a return value of 221 in $retVal["quitcode"] is a success

    $a='Mail Sent Sucessfully';
    return($a);
    }
authgMail("me@localhost.com", "vvv", "xxx@gmail.com","mmm","mail","message");
?>

Edited 3 Years Ago by Nick Evan: Fixed formatting

hey,i just used this code with corresponding arguments...its working fine for me...just check whether ur firewall block this port????bcoz i had same problems and i disabled my antivirus and seems to be working fine..

The problem is that your function does not support sending to multiple email addresses.

With SMTP, multiple addresses are sent one after the other, and the client has to wait for a response before writing the next recipient. So in your function change:

//email to
	fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['mailtoresponse'] = "$smtpResponse";

To something like:

//email to

$addresses = explode(',', $to);
foreach($addresses as $to) {
	fputs($smtpConnect, "RCPT TO: <".trim($to).">" . $newLine);
	$smtpResponse = fgets($smtpConnect, 4096);
	$logArray['mailtoresponse'] = "$smtpResponse";
}

What that code does is first explode your $to address into an array, $addresses. Then each address is sent to the SMTP server like:

RCPT TO: <email1@example.com>
RCTP TO: <email2@example.com>

So now you can use the format:

$to = 'email1@example.com, email2@example.com';

You can extend it further to include the name associated with emails if you need.

BTW: I'd recommend the SWIFT mailer library if you plan to incorporate sending multipart mails (HTML, Attachments etc.).

The function you have only sends a single part mail, text/html. Some mail clients cannot read html mail.

i just came to run the code


<?phpinfo()
?>


Here i came to notice a strange thing that in the Registered stream socket transports the ssl and tls is missing, kindly try the same in Ur system and intimate me bcuz i hope my ssl is not enabled,i have disabled my antivirus and firewall as U said, but now getting the error as

Warning: fsockopen() [function.fsockopen]: unable to connect to tls://smtp.gmail.com:465 (Unable to find the socket transport "tls" - did you forget to enable it when you configured PHP?) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\new.php on line 17

Warning: fgets(): supplied argument is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\new.php on line 19

One more doubt is that, what should i replace in the below mentioned line, presently i have given
$localhost = $_SERVER;
MY CURRENT SCRIPT LINE
$localhost = $_SERVER;

I WOULD ALSO ATTACH THE OUTPUT GOT WHEN I RAN PHPINFO WITH THIS, kindly take a look

thanks digital-ether ...your solution worked 200% for me..

thanks digital-ether ...your solution worked 200% for me..

No problem :)

vipin_php, what OS and Server are you using? It looks like you don't have TLS support.

I just tested the gmail smtp and it also accepts non-encrypted SMTP transfer. So you could use a simple tcp://smtp.gmail.com for your host and 25 for the port if you don't mind sending non-secure emails.

Otherwise you have to set up TLS support on your PHP build.

Thank U very much for Ur reply.

I reinstalled the whole thing including apache server2.2.8 and php5.2.5 and downloaded open ssl0.9.8g and installed them , now it is working fine and showing

- 0connection accepted
220 mx.google.com ESMTP f10sm4556194rvb.17
Continuing


Now my problem is that i am expecting a mail to my mail id for eg:xxx@gmail.com but i am not getting it, i enabled the POP service also in my gmail, what may be the problem? kindly help, i am attaching the coding also with this mail in the form of a.php, please have a look at it.

Attachments
<?php
function authgmail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "smtp.gmail.com"; //does not accept STARTTLS
$port = "587"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "xxx@gmail.com"; //your gmail account
$password = "xyz"; //the pass for your gmail
$localhost = $_SERVER["REMOTE_ADDR"]; //requires a real ip
$newLine = "\r\n"; //var just for newlines
/* you shouldn't need to mod anything else */
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
echo $errstr." - ".$errno;
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else
{
$logArray['connection'] = "Connected to: $smtpResponse";
echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
echo $tadr;
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto $to" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: 

$subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
$a='Mail Sent Sucessfully';
return($a);
}
authgmail("admin@smallbusiness.local","vvv","xxx@gmail.com","mmm","mail","message");
?>

Thank U very much for Ur reply.

I reinstalled the whole thing including apache server2.2.8 and php5.2.5 and downloaded open ssl0.9.8g and installed them , now it is working fine and showing

- 0connection accepted
220 mx.google.com ESMTP f10sm4556194rvb.17
Continuing


Now my problem is that i am expecting a mail to my mail id for eg:xxx@gmail.com but i am not getting it, i enabled the POP service also in my gmail, what may be the problem? kindly help, i am attaching the coding also with this mail in the form of a.php, please have a look at it.

Are you using your Gmail email as the sender? I think you'll have to use your gmail email, or another email that you have verified with Gmail as your email under the account that logs into SMTP.

Also, try dumping $logArray to see how far you got.

eg:

echo '<pre>'.implode("\r\n", $logArray).'</pre>';

Plase that just before the return in the function.

One problem I see with the script is that it has no error handling at all. If the SMTP server closes the connection mid-way, the script keeps sending data, and listening for responses.

Since php streams as 'blocking' (listen until some data is received) by default, the script will hang until killed - since there is no more data coming.

i have included the line which U gave and now getting the output like this, kindly go through the code rewritten by me which i am attaching with this mail and correct the code if necessary and give me the guide lines also, the output is like:

Resource id #2
- 0
220 mx.google.com ESMTP b8sm865562rvf.31
connection accepted
220 mx.google.com ESMTP b8sm865562rvf.31
Continuing
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31
Connected to: 220 mx.google.com ESMTP b8sm865562rvf.31

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

530 5.7.0 Must issue a STARTTLS command first b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502 5.5.1 Unrecognized command b8sm865562rvf.31

502

Attachments
<?php
function authgmail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "smtp.gmail.com"; //does not accept STARTTLS
$port = "587"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "xxx@gmail.com"; //your gmail account
$password = "abc"; //the pass for your gmail
$localhost = $_SERVER["REMOTE_ADDR"]; //requires a real ip
$newLine = "\r\n"; //var just for newlines
/* you shouldn't need to mod anything else */
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
echo '<pre>'.$smtpConnect.'</pre>';
echo '<pre>'.$errstr." - ".$errno.'<pre>';
$smtpResponse = fgets($smtpConnect, 4096);
echo '<pre>'.$smtpResponse.'<pre>';
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
echo '<pre>'.$output.'<pre>';
return $output;
}
else
{
$logArray['connection'] = "Connected to: $smtpResponse";
echo '<pre>'."connection accepted<br>".$smtpResponse."<p />Continuing<p />".'<pre>';
echo $tadr;
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto $to" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: 

$subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
//a return value of 221 in $retVal["quitcode"] is a success
$a='Mail Sent Sucessfully';
return($a);
}
authgmail("admin@smallbusiness.local","vvv","xxx@gmail.com","mmm","mail","message");
?>

sorry the previous post was not correct since i included the lines every where, now i am giving the right one , kindly take a look at the php script which i am attaching with this post and make necessary corrections if necessary and guide me to clear this problem, thank U very much for Ur support. The output is pasted below- 0connection accepted


220 mx.google.com ESMTP g1sm960727rvb.0
Continuing


Connected to: 220 mx.google.com ESMTP g1sm960727rvb.0

Connected to: 220 mx.google.com ESMTP g1sm960727rvb.0

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502

Attachments
<?php
function authgmail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "smtp.gmail.com"; //does not accept STARTTLS
$port = "587"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "xxx@gmail.com"; //your gmail account
$password = "abc"; //the pass for your gmail
$localhost = $_SERVER["REMOTE_ADDR"]; //requires a real ip
$newLine = "\r\n"; //var just for newlines
/* you shouldn't need to mod anything else */
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
echo $errstr." - ".$errno;
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else
{
$logArray['connection'] = "Connected to: $smtpResponse";
echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
echo $tadr;
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto $to" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject: 

$subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
$a='Mail Sent Sucessfully';
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
return($a);
}
authgmail("admin@smallbusiness.local","vvv","xxx@gmail.com","mmm","mail","message");
?>

sorry the previous post was not correct since i included the lines every where, now i am giving the right one , kindly take a look at the php script which i am attaching with this post and make necessary corrections if necessary and guide me to clear this problem, thank U very much for Ur support. The output is pasted below- 0connection accepted


220 mx.google.com ESMTP g1sm960727rvb.0
Continuing


Connected to: 220 mx.google.com ESMTP g1sm960727rvb.0

Connected to: 220 mx.google.com ESMTP g1sm960727rvb.0

250 mx.google.com at your service

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

530 5.7.0 Must issue a STARTTLS command first g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502 5.5.1 Unrecognized command g1sm960727rvb.0

502

Try writing the STARTTLS command to the SMTP server after HELO.

can U please specify how to issue the STARTTLS command

when i wrote like this

fputs($smtpConnect, "STARTTLS $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";

i got a syntax error

i have made the coding like this, now the STARTTLS error is gone but a syntax error is coming, pls help
<?php
function authgmail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "smtp.gmail.com"; //does not accept STARTTLS
$port = "587"; // try 587 if this fails
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "xxx@gmail.com"; //your gmail account
$password = "abc"; //the pass for your gmail
$localhost = $_SERVER["REMOTE_ADDR"]; //requires a real ip
$newLine = "\r\n"; //var just for newlines
/* you shouldn't need to mod anything else */
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
echo $errstr." - ".$errno;
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else
{
$logArray = "Connected to: $smtpResponse";
echo "connection accepted<br>".$smtpResponse."<p />Continuing<p />";
echo $tadr;
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "EHLO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//you have to say STARTTLS again after TLS is started
fputs($smtpConnect, "STARTTLS $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//you have to say HELO again after TLS is started
fputs($smtpConnect, "EHLO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto $to" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: <$to>\r\nFrom: $from\r\nSubject:$subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray = "$smtpResponse";
$logArray = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
$a='Mail Sent Sucessfully';
echo '<pre>'.implode("\r\n", $logArray).'</pre>';
return($a);
}
authgmail("admin@smallbusiness.local","vvv","xxx@gmail.com","mmm","mail","message");
?>


now i am getting an output like this


- 0connection accepted
220 mx.google.com ESMTP b39sm1796682rvf.8
Continuing


Connected to: 220 mx.google.com ESMTP b39sm1796682rvf.8

250-mx.google.com at your service, [59.94.135.253]

250-SIZE 28311552

250-8BITMIME

250-STARTTLS

250 ENHANCEDSTATUSCODES

555 5.5.2 Syntax error b39sm1796682rvf.8

250-mx.google.com at your service, [59.94.135.253]

250-SIZE 28311552

250-8BITMIME

250-STARTTLS

250

hello, i just did like as U told and i am getting the output as

- 0connection accepted
220 mx.google.com ESMTP c20sm2091776rvf.33 
Continuing


Connected to: 220 mx.google.com ESMTP c20sm2091776rvf.33

250-mx.google.com at your service, [59.94.135.253]

250-SIZE 28311552

250-8BITMIME

250-STARTTLS

250 ENHANCEDSTATUSCODES

555 5.5.2 Syntax error c20sm2091776rvf.33

250-mx.google.com at your service, [59.94.135.253]

250-SIZE 28311552

250-8BITMIME

250-STARTTLS

250

the problem i hope is

555 5.5.2 Syntax error c20sm2091776rvf.33
is there any solution?, pls help

hey..smtp access is blocked for me ..so can anyone tell me yahoo or aol smtp adress to send mail through php???

That is a REALLY old version of my code - you should have just written me and asked for help; that is assuming you have the original download with my email address or that you copied it from php.net.

Current version supports multiple recipients, cc & bcc, attachments, etc

I just wanted to add that in my case I found a solution here: http://forum.powweb.com/showthread.php?t=73406 - so after EHLO command one needs to send STARTTLS command, enabling cryptography with stream_socket_enable_crypto( $connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT ); and again EHLO command. Only this allowed me to send emails with this "stubborn" SMTP server.

Edited 5 Years Ago by boryn: n/a

Hello, digital-ether,
Can you plese give me the code for sending mail using php "mail" function to a gmail account.
Please, help me get rid of "Issue STARTTLS command"

This article has been dead for over six months. Start a new discussion instead.