Solved

send email from php  through gmail server

Posted on 2014-07-21
13
337 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Google Drive" not show in "My Files" on Samsung Galaxy Tab A 12 86
PHP Undefined Index in HTML Form? 2 32
<? versus <?php 5 37
javascript date picker in php while loop 5 27
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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 Micro Tutorial will demonstrate without sampling how to find out top organic landing pages. The hack gets around the standard way to find the pages in Google Analytics results in sampling for larger sites.
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.

895 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

17 Experts available now in Live!

Get 1:1 Help Now