如何解决服务器filezilla上的联系人表单(phpmailer)错误?

如何解决服务器filezilla上的联系人表单(phpmailer)错误?,php,phpmailer,filezilla,Php,Phpmailer,Filezilla,我使用PHPmailer作为我的联系人表单,当我用XAMPP打开我的网站时,它可以工作,但是当我将网站上传到在线服务器filezilla时,联系人表单显示一个错误 use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; $error = ''; $name = ''; $email = ''; $subject = ''; $message =

我使用PHPmailer作为我的联系人表单,当我用XAMPP打开我的网站时,它可以工作,但是当我将网站上传到在线服务器filezilla时,联系人表单显示一个错误

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';


$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';

function clean_text($string)
{
 $string = trim($string);
 $string = stripslashes($string);
 $string = htmlspecialchars($string);
 return $string;
}

if(isset($_POST["submit"]))
{
 if(empty($_POST["name"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Nome</label></p>';
 }
 else
 {
  $name = clean_text($_POST["name"]);
  if(!preg_match("/^[a-zA-Z ]*$/",$name))
  {
   $error .= '<p><label class="text-danger">Apenas letras e espaços em branco sao permitidos</label></p>';
  }
 }
 if(empty($_POST["email"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Email</label></p>';
 }
 else
 {
  $email = clean_text($_POST["email"]);
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
   $error .= '<p><label class="text-danger">Formato de email inválido</label></p>';
  }
 }
 if(empty($_POST["subject"]))
 {
  $error .= '<p><label class="text-danger">*Por Favor Insira o seu Assunto</label></p>';
 }
 else
 {
  $subject = clean_text($_POST["subject"]);
 }
 if(empty($_POST["message"]))
 {
  $error .= '<p><label class="text-danger">Por Favor Insira a sua Mensagem</label></p>';
 }
 else
 {
  $message = clean_text($_POST["message"]);
 }
 if($error == '')
 {


$mail = new PHPMailer(true);
  $mail->CharSet = "UTF-8";

  $mail->IsSMTP();        

  $mail->Host = 'smtp.sapo.pt';  
  $mail->Port = 25;      
  $mail->SMTPAuth = true;      
  $mail->Username = '...@sapo.pt';

  $mail->Password = '*****';  

  $mail->SMTPSecure = 'tls';     
  $mail->From = $_POST["email"];    
  $mail->FromName = $_POST["name"];   
  $mail->addAddress('....@sapo.pt ', 'namecompany');

  $mail->WordWrap = 50;       
  $mail->IsHTML(true);          
  $mail->Subject = $_POST["subject"];   



  if($mail->Send())        
  {
   $error = '<label class="text-success">Obrigado por entrar em contacto connosco!</label>';
  }
  else
  {
   $error = '<label class="text-danger">There is an Error</label>';
  }
  $name = '';
  $email = '';
  $subject = '';
  $message = '';
 }
}
显示错误:

致命错误:未捕获PHPMailer\PHPMailer\Exception:SMTP连接 失败。在里面 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/phpmailer.php:1775 堆栈跟踪:0 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/phpmailer.php1516: PHPMailer\PHPMailer\PHPMailer->smtpSend'日期:星期四,12秒, “\r\n\r\n…”1 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/phpmailer.php1352: PHPMailer\PHPMailer\PHPMailer->postSend 2 /home1/carvalho/public_html/contact2.php119: PHPMailer\PHPMailer\PHPMailer->send 3{main}抛出 /home1/carvalho/public_html/vendor/phpmailer/phpmailer/src/phpmailer.php 在线1775


您的SMTP凭据在生产服务器上不同。您只需获取有效凭据

如果您确定凭据正确,请检查端口和SMTPsecure,因为: 端口25不需要ssl/tls支持 用于ssl的端口465 tls的端口587

按如下方式修复代码:

$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";

$mail->IsSMTP();        

$mail->Host = 'smtp.sapo.pt';  
$mail->Port = 587; // this for "tls" connection  
$mail->SMTPAuth = true;      
$mail->Username = '...@sapo.pt';

$mail->Password = '*****';  

$mail->SMTPSecure = 'tls'; // this require 587    
$mail->From = $_POST["email"];    
$mail->FromName = $_POST["name"];   
$mail->addAddress('....@sapo.pt ', 'namecompany');

$mail->WordWrap = 50;       
$mail->IsHTML(true);          
$mail->Subject = $_POST["subject"];

如果托管公司允许您直接从其他SMTP服务器发送电子邮件,请与他们联系。有一些大型托管公司不允许这样做,所以您必须通过他们的SMTP中继发送。实际上,您的托管提供商很可能会阻止出站SMTP。这里的许多问题都介绍了如何诊断此问题,错误消息链接的故障排除指南中也有详细介绍。与您描述的问题不同,不要这样做:$mail->from=$\u POST[email];;这是伪造的,将导致您的邮件被反弹或垃圾邮件过滤。以代码为基础以避免此类问题。@Synchro PHP编译器被认为不安全?因为我注意到这个问题是因为www. SAPO.PT阻止我的电子邮件,因为我认为我有可疑的行为。这就是为什么它不起作用的原因……PHPMailer很好,但它肯定可以以不安全的方式使用。允许提交者伪造任意的发件人地址将被视为可疑,并且是阻止的理由,但这是您的代码,而不是PHPMailer,这就是为什么我在联系人表单示例中指出您,该示例没有犯此错误。该错误看起来更像是连接问题,而不是凭据问题。如果他们使用第三方邮件服务/smtp,我不明白为什么凭据需要不同?显然,有效的smtp主机也是连接凭据的一部分。但是,为什么要将开发环境连接到生产电子邮件服务器?如果你问我的话,这有点疯狂。现在甚至不在XAMPP上工作,就在昨天还在工作。你能ping服务器吗?如果可以,你能在正确的端口上远程登录吗?我已经尝试了所有方法…端口465、587、26、25、21以及ssl和TLSFfirst。PHPMailer不是一个好主意,因为它不再是支持者。Recente更新导致一些问题。为了获得最佳性能,我使用支持SMTP的Zend mail进行了切换。第二,尝试删除smtp连接,并检查是否发送电子邮件。之后,控制主机是否有防火墙或类似的活动。对不起?我仍然非常支持PHPMailer。如果您有问题,请在bug tracker上创建一个问题。@Synchro您看到解决方案了吗?是的-通过阅读,这将告诉您如何准确地诊断它。