PHP Gmail SMTP Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tutu saint
    New Member
    • Jun 2011
    • 21

    PHP Gmail SMTP Error

    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.


    Code:
    <?php
    
    require_once('Connections/dbcon.php');
    
    if (isset($_POST['formsubmitted'])) {
        $error = array();//Declare An Array to store any error message  
        if (empty($_POST['name'])) {//if no name has been supplied 
            $error[] = 'Please Enter a name ';//add to array "error"
        } else {
            $name = $_POST['name'];//else assign it a variable
        }
    	
        if (empty($_POST['lname'])) {//if no name has been supplied 
            $error[] = 'Please Enter a name ';//add to array "error"
        } else {
            $lname = $_POST['lname'];//else assign it a variable
        }
    	
    	
        if (empty($_POST['EmailAd'])) {
            $error[] = 'Please Enter your Email ';
        } else {
    
    
            if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['EmailAd'])) {
               //regular expression for email validation
                $EmailAd = $_POST['EmailAd'];
            } else {
                 $error[] = 'Your EMail Address is invalid  ';
            }
        }
        if (empty($_POST['Password'])) {
            $error[] = 'Please Enter Your Password ';
        } else {
            $Password = $_POST['Password'];
        }
    
    	
    require_once("phpmailer/class.phpmailer.php");
    
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = 'ssl://smtp.gmail.com:465';
    $mailer->SMTPAuth = TRUE;
    $mailer->Username = 'mymail@gmail.com';  // Change this to your gmail adress
    $mailer->Password = 'mypassword';  // Change this to your gmail password
    $mailer->From = 'mymail@gmail.com';  // This HAVE TO be your gmail adress
    $mailer->FromName = 'Registration Success!'; // This is the from name in the email, you can put anything you like here
    $mailer->Body = 'Your username: '.$EmailAd.'  | Your password is: "'.$Password.'';
    $mailer->Subject = 'JobSearch Nigeria';
    $mailer->AddAddress($_POST['EmailAd']); 
    
    if(!$mailer->Send())
    {
       echo "<script>alert('Registration failed.')</script>";
    
    }
    else
    {
    echo "<script>alert('Registration Success! Please logon to more fetures.')</script>";
    
      mysql_query("INSERT INTO users(name, lname, EmailAd, Password)VALUES('$name', '$lname','$EmailAd', '$Password')");
    }
    
    
    }
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.
    Code:
    $mailer->Host = 'smtp.gmail.com:465';
    $mailer->SMTPSecure = 'ssl';
    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.

    Comment

    • waqaspuri
      New Member
      • Jun 2013
      • 4

      #3
      Email SPF records may interrupt

      Comment

      Working...