SMTP邮件不工作在服务器上工作正常在Localhost上

时间:2016-11-14 12:14:05

标签: php mysql

您好我正在使用smtp最新文件并检查所有可用于网络的解决方案,My SMTP php邮件在localhost上工作不在服务器上我的服务器的网络邮件工作正常。首先我将数据插入到我的网络中数据库然后那些我想发送管理员电子邮件ID的数据我的数据也插入数据库中,邮件发送工作正常在localhost上但是在服务器上给出了错误,它无法与服务器连接。我联系我的托管服务器支持论坛,但他们说这是你的代码问题 这是我的PHP代码

 <?php
   if(isset($_POST['Isubmit'])){
   $rname= $_POST['Iname'];
   $remail= $_POST['Iemail'];
   $rcontact= $_POST['Icontact'];
   $rmessage= $_POST['Icomment'];
   $rcourse= $_POST['Icourse'];
    $date=date("Y-m-d");
 $sql=mysqli_query($connect,"INSERT INTO enquiry(`NAME`,`EMAIL_ID`,`MOBILE_NO`,`COMMENT`,`SUBJECT`,`IS_DELETED`,`DATE`)VALUES('".$rname."','".$remail."','".$rcontact."','".$rmessage."','".$rcourse."','1','".$date."')");

//Mail Send Code


 $ToEmail = 'admin@gmail.com'; 
 $EmailSubject = 'Site contact form'; 
 $MESSAGE_BODY = "Dear Sir/Mam,"."\n" ."\n"."New Contact Details are - "."\n"."\n"; 
$MESSAGE_BODY .= "Full Name: ".$rname."\n"."\n"; 
$MESSAGE_BODY .= "Email: ".$remail."\n"."\n"; 
$MESSAGE_BODY .= "Phone: ".$rcontact."\n"."\n"; 
$MESSAGE_BODY .= "Message: ".$rmessage."\n"."\n";
$MESSAGE_BODY .= "Course: ".$rcourse."\n"."\n";
 //SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don'thave access to that date_default_timezone_set('Etc/UTC');
       require 'PHPMailer-master/PHPMailerAutoload.php';
      //Create a new PHPMailer instance
     $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP
    $mail->isSMTP();
   //Enable SMTP debugging // 0 = off (for production use)
    $mail->SMTPDebug = 0;
 //Ask for HTML-friendly debug output
  $mail->Debugoutput = 'html';
//Set the hostname of the mail server
  $mail->Host = 'smtp.gmail.com';
   //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
             $mail->Port = 465;
    //Set the encryption system to use - ssl (deprecated) or tls
       $mail->SMTPSecure = 'ssl';
   //Whether to use SMTP authentication
     $mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
      $mail->Username = "username@gmail.com";//"lino.cspace@gmail.com";
   //Password to use for SMTP authentication
    $mail->Password = "yourpassword";//"lino@123";
      //Set who the message is to be sent from
       $mail->setFrom('admin@gmail.com', 'Admin');
       $mail->addAddress($ToEmail);
      //Set the subject line
        $mail->Subject = 'Enquiry Mail';
       $mail->Body = $MESSAGE_BODY;//'this is test msg on 25-jun.';
       if(!$mail->Send()) {
         echo "Mailer Error: " . $mail->ErrorInfo;
        } else {
           echo "Message sent!<br>";
          }
       }

1 个答案:

答案 0 :(得分:0)

启用smtp调试

$mail->SMTPDebug = 1;

OR

 $mail->SMTPDebug = 2;

然后错误信息将为您提供有关错误的更多信息

if (!$mail->Send())
{
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    echo "Message sent!<br>";
}