Solved

send email from php  through gmail server

Posted on 2014-07-21
13
336 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 82

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Learn about cloud computing and its benefits for small business owners.
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 will demonstrate the easy use of Gmail embedding images in your email so the recipient of your email can view them in context.
This Micro Tutorial will demonstrate how marketers can use the Mobile Emulation Tool in Chrome Developer Tool. This will let you preview your site on any mobile device.

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now