Hi Forum,
As mentioned in title, I am trying to send an email using gmail server…Its making connection perfectly…but asking for issuing a STARTTLS command first…
Any help regarding this, would be highly appreciated.
Please find the code below, I am using.
You can test it…at your server…too…
function authgMail($from, $namefrom, $to, $nameto, $subject, $message) {
$smtpServer = "smtp.gmail.com"; //ip address of the mail server. This can also be the local domain name
$port = "587"; // should be 25 by default, but needs to be whichever port the mail server will be using for smtp
$timeout = "45"; // typical timeout. try 45 for slow servers
$username = "yourname@gmail.com"; // the login for your smtp
$password = "password"; // the password for your smtp
$localhost = "smtp.gmail.com"; // Defined for the web server. Since this is where we are gathering the details for the email
$newLine = "\\r\
"; // aka, carrage return line feed. var just for newlines in MS
$secure = 0; // change to 1 if your server is running under SSL
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
$output = "Failed to connect: $smtpResponse";
echo $output;
return $output;
}
else {
$logArray['connection'] = "<p>Connected to: $smtpResponse";
echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
echo $logArray['heloresponse2']."<br />";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
echo $logArray['authrequest']."<br />";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
echo $logArray['authusername']."<br />";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
echo $logArray['authpassword']."<br />";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
echo $logArray['mailfromresponse']."<br />";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
echo $logArray['mailtoresponse']."<br />";
//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
echo $logArray['data1response']."<br />";
//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\
From: $from\\r\
Subject: $subject\\r\
$headers\\r\
\\r\
$message\\r\
.\\r\
");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
//echo $logArray['data2response']."<br />";
// 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
return($logArray);
}
$err=0; // so far, so good
$err_msg="";
if($_POST['name']!="") { echo $_POST['name']."<br>"; }
else {
$err=1;
$err_msg="You must include your name";
}
if($_POST['phone']!="") {echo $_POST['phone']."<br>"; }
else {
$err=1;
$err_msg="You must include a daytime phone number.";
}
if($_POST['streetaddress']!="") { echo $_POST['streetaddress']."<br>"; }
else {
$err=1;
$err_msg="You must include your address.";
}
if($_POST['city']!="") { echo $_POST['city']."<br>"; }
else {
$err=1;
$err_msg="You must include the city.";
}
// Check for the existence of an AT symbol inside the email.
if (strpos($_POST['email'],"@")) { echo $_POST['email']."<br>"; }
else {
$err=1;
$err_msg="You must include a current email address.";
}
if($_POST['email']!="") { echo $_POST['email']."<br>"; }
else {
$err=1;
$err_msg="You must include your e-mail address.";
}
echo $err_msg;
if($err<=0) {
$from="youremail@gmail.com";
$namefrom="Customer Care Department of ";
$to = "yourreceipent9@yahoo.com";
$nameto = "Admin";
$subject = "THis is Testing Email";
$message = "Dear Admin, Message ";
// this is it, lets send that email!
if(authgMail($from, $namefrom, $to, $nameto, $subject, $message))
{
echo "Message Sent Successfully";
}
else
{
echo "Error Message cannot send!";
}
Please help as I want to know how to use TSL/STARTTLS, as what changes required. I am running it at my pc…using WAMP SERVER…
Any Help would be appreciated.