473,473 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PHP Gmail SMTP Error

21 New Member
I have a registration script that sends email through Gmail SMTP with the below script.

i received errors (SMTP Error: Could not connect to SMTP host. )


Please what is wrong with this code.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. require_once('Connections/dbcon.php');
  4.  
  5. if (isset($_POST['formsubmitted'])) {
  6.     $error = array();//Declare An Array to store any error message  
  7.     if (empty($_POST['name'])) {//if no name has been supplied 
  8.         $error[] = 'Please Enter a name ';//add to array "error"
  9.     } else {
  10.         $name = $_POST['name'];//else assign it a variable
  11.     }
  12.  
  13.     if (empty($_POST['lname'])) {//if no name has been supplied 
  14.         $error[] = 'Please Enter a name ';//add to array "error"
  15.     } else {
  16.         $lname = $_POST['lname'];//else assign it a variable
  17.     }
  18.  
  19.  
  20.     if (empty($_POST['EmailAd'])) {
  21.         $error[] = 'Please Enter your Email ';
  22.     } else {
  23.  
  24.  
  25.         if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['EmailAd'])) {
  26.            //regular expression for email validation
  27.             $EmailAd = $_POST['EmailAd'];
  28.         } else {
  29.              $error[] = 'Your EMail Address is invalid  ';
  30.         }
  31.     }
  32.     if (empty($_POST['Password'])) {
  33.         $error[] = 'Please Enter Your Password ';
  34.     } else {
  35.         $Password = $_POST['Password'];
  36.     }
  37.  
  38.  
  39. require_once("phpmailer/class.phpmailer.php");
  40.  
  41. $mailer = new PHPMailer();
  42. $mailer->IsSMTP();
  43. $mailer->Host = 'ssl://smtp.gmail.com:465';
  44. $mailer->SMTPAuth = TRUE;
  45. $mailer->Username = 'mymail@gmail.com';  // Change this to your gmail adress
  46. $mailer->Password = 'mypassword';  // Change this to your gmail password
  47. $mailer->From = 'mymail@gmail.com';  // This HAVE TO be your gmail adress
  48. $mailer->FromName = 'Registration Success!'; // This is the from name in the email, you can put anything you like here
  49. $mailer->Body = 'Your username: '.$EmailAd.'  | Your password is: "'.$Password.'';
  50. $mailer->Subject = 'JobSearch Nigeria';
  51. $mailer->AddAddress($_POST['EmailAd']); 
  52.  
  53. if(!$mailer->Send())
  54. {
  55.    echo "<script>alert('Registration failed.')</script>";
  56.  
  57. }
  58. else
  59. {
  60. echo "<script>alert('Registration Success! Please logon to more fetures.')</script>";
  61.  
  62.   mysql_query("INSERT INTO users(name, lname, EmailAd, Password)VALUES('$name', '$lname','$EmailAd', '$Password')");
  63. }
  64.  
  65.  
  66. }
  67.  
  68.  
Jun 19 '13 #1
2 2036
Atli
5,058 Recognized Expert Expert
Which version of PHPMailer are you using? I tried your script with 5.1 and it worked fine.

Generally speaking, though, PHPMailer has a SMTPSecure property that should be used to set the connection encryption method, rather than specifying it in the host string.
Expand|Select|Wrap|Line Numbers
  1. $mailer->Host = 'smtp.gmail.com:465';
  2. $mailer->SMTPSecure = 'ssl';
  3.  
It shouldn't really matter these days, but I'm unsure how earlier versions of PHPMailer handled the host string. If the "ssl" is set like in the above script, PHPMailer will add the "ssl://" stream type to the host name when it opens the connection.


Of course, it's possible that Google is outright blocking your connection. The error states that it can't connect to the SMTP host, which could mean that Google's SMTP servers have some sort of IP/hostname block going that is simply throwing out connections from your host. That may well be the case on some shared hosts, if you are sharing them with known spammers. - I very much doubt Google is going to serve email for any kind of bulk mailing applications or automated scripts for long. That's definitely not why they set these SMTP servers up.
Jun 21 '13 #2
waqaspuri
4 New Member
Email SPF records may interrupt
Jun 21 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Robbie | last post by:
Hi, I have a strange error occuring - I don't think its a code issue - but maybe someone here has run into this before. I have some code to send an Email using the System.Web.Mail message...
2
by: Latha Rajeev | last post by:
Hi, I have an application which sends out mails(using CDO), the SMTP Server being a machine which is one the same network. Most times, the mails are sent out successfully but every once in a...
3
by: Charlie | last post by:
Hi: Using the sMTP class and my ISP as smtp server, I sometimes get the following message: "The message could not be sent to the SMTP server. The transport error code was 0x800ccc60. The...
1
by: Hennie7863 | last post by:
Hi all, At this moment I'm trying to get database mail working. According to some people it should be easy. Well...that is not the case for me. I'm having the following error: The mail could...
6
by: sx216 | last post by:
hi i have the error when using phpmailer to send mail.. "error in sending mail: SMTP Error: The following recipients failed: me@domain.com" if anybody knows the solution..please help...
0
by: Homer | last post by:
Hi, I got "InnerException: Unable to connect to remote server"..."No connection could be made because the target machine actively refused it" when I attempted to send an email through my...
3
by: Alexander Vasilevsky | last post by:
Code below after some pause raise exception "waiting times operations has expired". Why? public static void Send(string from, string pwd, string to, string subject, string body) { ...
8
by: aradhanathawait | last post by:
I tried a lot to send email from my gmail id using the smtp server of gmail.com but I am getting request time out error. I increased the execution time in web.config then also getting the same error....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.