【问题标题】:phpmailer not working after migration to php 7迁移到 php 7 后 phpmailer 无法正常工作
【发布时间】:2018-07-25 06:11:02
【问题描述】:

下面是我的php代码

以前服务器使用 php 5.9,我们最近将其更新到 php 7.0

发送邮件时出现以下错误,提示 SMTP 错误,但凭据正确

无法发送消息。 邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

<?php
$cname = $_REQUEST[ 'cname' ];
//echo $cname;
$email = $_REQUEST[ 'email' ];
//echo $email.',';
$phone = $_REQUEST[ 'phone' ];
//echo $phone.',';
$datetime = $_REQUEST[ 'datetime' ];
//echo $datetime.',';
$package = $_REQUEST[ 'packages' ];
//echo $package.',';


$message = '<table style="height: 231px; width: 490px; float: left;">
<tbody>
<tr style="height: 21.5px;">
<td style="width: 235.433px; height: 21.5px;"><strong>Name:-</strong></td>
<td style="width: 253.567px; height: 21.5px;">' . $cname . '</td>
</tr>
<tr style="height: 21px;">
<td style="width: 235.433px; height: 21px;"><strong>Email:-</strong></td>
<td style="width: 253.567px; height: 21px;">' . $email . '</td>
</tr>
<tr style="height: 21px;">
<td style="width: 235.433px; height: 21px;"><strong>Phone No:-</strong></td>
<td style="width: 253.567px; height: 21px;">' . $phone . '</td>
</tr>
<tr style="height: 21px;">
<td style="width: 235.433px; height: 21px;"><strong>Date & Time:-</strong></td>
<td style="width: 253.567px; height: 21px;">' . $datetime . '</td>
</tr>
<tr style="height: 21.5px;">
<td style="width: 235.433px; height: 21.5px;"><strong>Packages:-</strong></td>
<td style="width: 253.567px; height: 21.5px;">' . $package . '</td>
</tr>';

require "PHPMailer/PHPMailerAutoload.php";
// require_once "PHPMailer/class.phpmailer.php"; //include phpmailer class

// Instantiate Class  
$mail = new PHPMailer();

// Set up SMTP  
$mail->IsSMTP(); // Sets up a SMTP connection  
//$mail->SMTPDebug  = 2;       // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization    
$mail->SMTPSecure = "ssl"; //"ssl";      // Connect using a TLS connection 
//$mail->SMTPSecure = "tls";

//$mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
$mail->Host = "mail.test.com";
$mail->Port = '465'; //'465';  //Gmail SMTP port
$mail->Encoding = '8bit';

// Authentication  

$mail->Username = "testuser@test.com";
$mail->Password = "password";


// Compose
$mail->SetFrom( 'testuser@test.com', 'Test' );

$mail->Subject = "Appointment for $package has been received"; // Subject (which isn't required)  
$mail->MsgHTML( $message );

// Send To  
//$mail->AddAddress("", "Recipient Name"); // Where to send it - Recipient
//$mail->AddAddress("testuser@test.com", "Recipient Name");



// $result = $mail->Send();     // Send!  
//$message = $result ? 'Successfully Sent!' : 'Sending Failed!'; 
//$message = $result ? header("Location:thank_you.php"); : 'Sending Failed!';      
//unset($mail);

if ( !$mail->send() ) {
    echo 'Message could not be sent.';
    echo '<br>Mailer Error: ' . $mail->ErrorInfo;
} else {}


?>
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Favicon -->
    <link rel="icon" href="favicon.png">
        <link href="css/main.css" rel="stylesheet" type="text/css">
    <style>
    .text-xs-center {
    text-align: center !important;
}
.jumbotron {
    padding: 4rem 2rem;
}
.jumbotron {
    background-color: #eceeef;
    border-radius: 0.3rem;
    margin-bottom: 0rem;
    padding: 5rem 1rem;
}
        .display-3 {
    font-size: 4.5rem;
    font-family: Poiret One; font-weight: bold;
}
        .lead {
    font-size: 1.25rem;
    font-weight: 300;
    font-family: Poiret One; font-weight: bold;
}
p {
    margin-bottom: 1rem;
    margin-top: 0;
    font-family: Poiret One; font-weight: bold;
}
    .thankyou-check {
    color: #15639a;
    font-size: 100px;
    text-align: center;
    margin: 6px;    
}
    </style>

    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/bootstrap.min.js" type="text/javascript"></script>
</head>

<body>
    <?php echo  file_get_contents('header.php'); ?>
    <div class="jumbotron text-xs-center">
    <i class="fa fa-check-circle thankyou-check"></i>
  <h1 class="display-3">Thank You!</h1>
  <p class="lead">for your appointment and We will get back to you soon.</p>
</div>  
    <?php echo  file_get_contents('footer.php'); ?>
</body>

    enter code here

</html>

【问题讨论】:

  • 启用 SMTP 调试:$mail->SMTPDebug = 2;并检查它说什么
  • 尝试使用端口587,因为465 是gmail 的端口,并且您已更新主机。
  • SMTP 错误:密码命令失败:535 5.7.8 @klian
  • @LovepreetSingh 更改端口没有帮助
  • 错误 535 是身份验证问题。您必须检查凭据是否正确:SMTP 主机是否正确。用户名正确。密码正确。端口正确。认证方式正确。

标签: php php-7.0


【解决方案1】:

您可以更改第 459 行并将 split 函数替换为 explode 函数。

-  $toArr = split(',', $to);
+  $toArr = explode(',', $to);

【讨论】:

  • 这与问题完全无关。 OP的问题是身份验证问题。
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
相关资源
最近更新 更多