?
Solved

send email from php  through gmail server

Posted on 2014-07-21
13
Medium Priority
?
343 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 

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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This Micro Tutorial demonstrates the importance of annotations in Google Analytics and how they should be used to document changes made to a site, Google updates (Ex: Panda & Penguin), marketing campaigns, and any other events that might have contri…
In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
Suggested Courses

770 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