Solved

send email from php  through gmail server

Posted on 2014-07-21
13
339 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

Author Closing Comment

by:cycledude
ID: 40212566
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

You can provide a virtual interface for remote stakeholders in a SWOT analysis through a Google Drawing template. By making real time viewing and collaboration possible, your team can build a stronger product.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial  demonstrates whether your site uses one subdomain or multiple subdomains, how to create full URLs from Google Analytics content reports. This procedure is called concatenation and can also be done with the CONCATENTATE function.…
This Micro Tutorial will demonstrate how to use comments in Google Docs. You will learn how to create, edit, and delete a comment.

772 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