Solved

send email from php  through gmail server

Posted on 2014-07-21
13
342 Views
Last Modified: 2014-11-12
Hi

I have a requirement t send form data from a webpage through gmail servers, here is a function I have inherited, which is not working...

How can I make this work?

Cheers


function authSendEmail($from,  $to,  $subject, $message)
 {
 //SMTP + SERVER DETAILS
 /* * * * CONFIGURATION START * * * */
 $smtpServer = "smtp.gmail.com";
 $port = "465";
 $timeout = "30";
 $username = "email@mydomain.com";
 $password = "password";
 $localhost = "mail.localserver.com";
 $newLine = "\r\n";
 /* * * * CONFIGURATION END * * * * */
 
//Connect to the host on the specified port
 $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
 $smtpResponse = fgets($smtpConnect, 515);
 if(empty($smtpConnect))
 {
 $output = "Failed to connect: $smtpResponse";
 return $output;
 }
 else
 {
 $logArray['connection'] = "Connected: $smtpResponse";
 }

//Request Auth Login
 fputs($smtpConnect,"AUTH LOGIN" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['authrequest'] = "$smtpResponse";

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

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

//Say Hello to SMTP
 fputs($smtpConnect, "HELO $localhost" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['heloresponse'] = "$smtpResponse";

//Email From 
 fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['mailfromresponse'] = "$smtpResponse";
 
//Email To
 fputs($smtpConnect, "RCPT TO: $to" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['mailtoresponse'] = "$smtpResponse";

//The Email
 fputs($smtpConnect, "DATA" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['data1response'] = "$smtpResponse";

//Construct Headers
 $headers = "MIME-Version: 1.0" . $newLine;
 $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
 $headers .= "To: $to" . $newLine;
 $headers .= "From: $from" . $newLine;
 
 
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['data2response'] = "$smtpResponse";

// Say Bye to SMTP
 fputs($smtpConnect,"QUIT" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['quitresponse'] = "$smtpResponse";
 }
 ?>

Open in new window

0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209652
Can you elaborate on 'not working'
0
 

Author Comment

by:cycledude
ID: 40209665
sure


Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gmail.com:465 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in E:\domains\domain.com\wwwroot\includes\email.php on line 38

Warning: fgets(): supplied argument is not a valid stream resource in E:\domains\domain.com\wwwroot\includes\email.php on line 39
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209676
change the
$port = "465";

Open in new window

to
$port = "587";

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cycledude
ID: 40209699
Hi

I have tried both ports, same result

How can I set SSL, I think this may be the issue?
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209715
that's based on the port and initial negotiation:
change your line on the return to:
$output = "Failed to connect: $smtpResponse[$errstr]";

that way we can get the error string as well and post that please.
0
 

Author Comment

by:cycledude
ID: 40209728
Hi

I got the same response?

Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:587 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in E:\domains\domain.com\wwwroot\includes\email.php on line 38

Warning: fgets(): supplied argument is not a valid stream resource in E:\domains\domain.com\wwwroot\includes\email.php on line 39
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209734
strange, post your revised code please
0
 

Author Comment

by:cycledude
ID: 40209750
here you go

i dunno if this makes a difference but the client has moved their email hosting from the server that the site is hosted on... they moved email to gmail servers (so the email address is not a gmail native address)



function authSendEmail($from,  $to,  $subject, $message, $htmlFormat = 0)
 {
 //SMTP + SERVER DETAILS
 /* * * * CONFIGURATION START * * * */
 $smtpServer = "ssl://smtp.gmail.com";
 $port = "587";
 $timeout = "30";
 $username = "email@gmail.com";
 $password = "ggggg";
 $localhost = "mail.localhost.com";
 $newLine = "\r\n";
 /* * * * CONFIGURATION END * * * * */
 
//Connect to the host on the specified port
 $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
 $smtpResponse = fgets($smtpConnect, 515);
 if(empty($smtpConnect))
 {
 //$output = "Failed to connect: $smtpResponse";
 $output = "Failed to connect: $smtpResponse[$errstr]";
 return $output;
 }
 else
 {
 $logArray['connection'] = "Connected: $smtpResponse";
 }

//Request Auth Login
 fputs($smtpConnect,"AUTH LOGIN" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['authrequest'] = "$smtpResponse";

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

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

//Say Hello to SMTP
 fputs($smtpConnect, "HELO $localhost" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['heloresponse'] = "$smtpResponse";

//Email From 
 fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['mailfromresponse'] = "$smtpResponse";
 
//Email To
 fputs($smtpConnect, "RCPT TO: $to" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['mailtoresponse'] = "$smtpResponse";

//The Email
 fputs($smtpConnect, "DATA" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['data1response'] = "$smtpResponse";

//Construct Headers
 $headers = "MIME-Version: 1.0" . $newLine;
 $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
 $headers .= "To: $to" . $newLine;
 $headers .= "From: $from" . $newLine;
 
 
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['data2response'] = "$smtpResponse";

// Say Bye to SMTP
 fputs($smtpConnect,"QUIT" . $newLine);
 $smtpResponse = fgets($smtpConnect, 515);
 $logArray['quitresponse'] = "$smtpResponse";
 }
 ?>

Open in new window

0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209757
change "ssl://smtp.gmail.com" to "smtp.gmail.com"
0
 

Author Comment

by:cycledude
ID: 40209763
hi

I just spotted that myself and removed it, same result.
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40209764
also make sure you can connect from the machine this is hosted on by trying to telnet.
telnet smtp.gmail.com 587
this will make sure the firewall is not preventing communication to the site.
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40209903
The 'test_smtp_gmail_advanced.php' in PHPMailer_5.2.4 works fine to send thru Gmail.  You might take a look at it to see if there is anything that can help there.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with spreadsheets can be a daunting task, especially when having to deal with large amounts of data. All you see are rows and rows of numbers and soon your eyes begin to glaze over. Take advantage of the tools in Google Sheets to create prof…
This article discusses how to create an extensible mechanism for linked drop downs.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question