Sending Email with PHPMailer giving an SMTP error?

Discussion in 'PHP' started by elearnindia, Jul 23, 2010.

  1. #1
    I am trying to send email using PHPmailer with google SMTP server, it is giving an SMTP error(SMTP Error:could not connect to

    SMTP host
    ),I am showing the code below:-

    <?
    if(isset($_POST['email']))
    {
        $first_name = $_POST['name']; // required
        $email_from = $_POST['email']; // required
        $phone = $_POST['phone']; // not required
        $comments = $_POST['comments']; // required
                                                            
                                                            
        $email_message .= "First Name: ".$first_name."\n\n";
        $email_message .= "Email: ".$email_from."\n\n";
        $email_message .= "Telephone: ".$phone."\n\n";
        $email_message .= "Comments: ".$comments."\n";
                                                            
        require_once('PHPMailer_v5.1/class.phpmailer.php');
        $mail = new PHPMailer();
        $mail->IsSMTP();  // telling the class to use SMTP
        $mail->SMTPAuth = true; // enable SMTP authentication
        $mail->Username = "username"; // SMTP username
        $mail->Password = "password"; // SMTP password
                                                            
        $mail->SMTPSecure = "ssl"; // sets the prefix to the server
        $mail->Host     = "smtp.gmail.com"; // sets GMail as the SMTP  server
        $mail->Port = 465;  // set the SMTP port for the GMAIL server
                                                             
        $mail->From = $email_from;
        $mail->FromName = $first_name;
        $mail->SetFrom($email_from, $first_name);
        $mail->AddReplyTo($email_from, $first_name);
        $mail->AddAddress("sureshbaburavilla@gmail.com",$first_name);
        $mail->AddCC("sridhar.425@gmail.com",$first_name);
        $mail->AddBCC("srinivasvar@gmail.com",$first_name);                                                     
        $mail->Subject  = "Sree Vizag Marketing-Tiles Enquiry";
        $mail->Body     = $email_message;
        $mail->WordWrap = 200;
                                                             
        if(!$mail->Send()) 
            {
               echo "<script>alert('Appears to be Server Problem! Please contact again!');window.location='contact.html';</script>";
            }
        else
            {
            echo "<script>alert('Email sent,get back to you soon!');window.location='index.html';</script>";
            }
                    
    }
                                                    
    ?>
    PHP:
    I have also used:-

    <?
    $mail->SMTPSecure = "tls"; // sets the prefix to the server
    $mail->Port = 587;  // set the SMTP port for the GMAIL server
    ?>
    
    PHP:
    It is also giving the same SMTP error

    Any solution to this will be gladly appreciated.
     
    elearnindia, Jul 23, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Code looks ok. Running on local server or a web host?
     
    Deacalion, Jul 23, 2010 IP
  3. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello Deacalion,Code running on local server sending email perfectly, but on a web host it is giving SMTP error.
     
    elearnindia, Jul 23, 2010 IP
  4. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Does your hosting has OpenSSL enabled?
     
    imperialDirectory, Jul 23, 2010 IP
  5. elearnindia

    elearnindia Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    OpenSSL is not enabled on web host.I written to the web host to enable OpenSSL
     
    elearnindia, Jul 24, 2010 IP
  6. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Google's mail servers require you to use SSL, they won't accept plaintext connections. If your web host doesn't have open SSL available to you, I'd consider switching unless they add it.
    If you do indeed have it installed, it would show in phpinfo()
     
    Deacalion, Jul 24, 2010 IP