PHP邮件程序连接错误

时间:2013-11-14 10:26:24

标签: php

我收到以下错误: SMTP - >错误:无法连接到服务器:连接超时(110) 以下发件人地址失败:test@gmail.com:未连接称为Mail()

我的代码或任何其他问题有什么问题?

我的openssl也启用了

我正在做以下编码:

<?php
      include("securimage/securimage.php");
      $img = new Securimage();
      $valid = $img->check($_POST['code']);

      if($valid == true) {

        require_once('class.phpmailer.php'); 

        $mail = new PHPMailer(); 
        $body="Name : ".$_POST['name']."<br>";
        $body .="Subject : ".$_POST['subject']."<br>";
        $body .="Phone : ".$_POST['phone']."<br>";
        $body .="Email : ".$_POST['email']."<br>";
        $body .=$_POST['comment']."<br>";
        $mail->IsSMTP(); 
        $mail->SMTPDebug  = 1;                 
        $mail->SMTPAuth   = true;                
        $mail->SMTPSecure = "ssl";                 
        $mail->Host= "smtp.gmail.com";     
        $mail->Port       = 465;                   
        $mail->Username   = "contactus@gmail.com";  
        $mail->Password   = "contactus"; 
        $mail->From        = $_POST['email'];          
        $mail->FromName    = $_POST['name'];

        $mail->AddReplyTo($_POST['email']);
        $mail->Subject    = "Contact Us";
        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; 
        $mail->MsgHTML($body);
        $mail->AddAddress('niyati@ngplweb.com', 'Niyati'); 

        if(!$mail->Send()) 
        {
              setcookie("msg","Error.",time()+5);
              header("location:contactus.php");
        } 
        else 
        {
               setcookie("msg","Thank you.We will get back to you soon......",time()+5);
               header("location:contactus.php");
         }
       } 
       else 
       {
               setcookie("msg","Incorrect Captcha.",time()+5);
               header("location:contactus.php");
        } 
        ?>

2 个答案:

答案 0 :(得分:2)

您的密码分配不正确。这可能是您的连接不起作用的原因。

 mail->Password = "contactus"; 
^---

应更改为:

$mail->Password = "contactus"; 

答案 1 :(得分:1)

“SMTP - &gt;错误:无法连接到服务器:连接超时(110)以下发件人地址失败:test@gmail.com:调用Mail()而未连接”

这是因为

    $mail->Username   = "contactus@gmail.com";  
    $mail->Password   = "contactus"; 

不是有效的。尝试使用一些有效的用户名和密码,我想一切都会好的。 有效,我的意思是真实的“gmail id”的真实用户名和密码。

相关问题