致命错误:未捕获的PHPMailer\PHPMailer\异常:SMTP错误:无法连接到SMTP主机,无法连接到服务器

lpwwtiir  于 2023-04-10  发布在  PHP
关注(0)|答案(1)|浏览(468)

我安装了PHPMailer来从我的PHP应用程序发送电子邮件。我使用Mailtrap来伪造服务器。
但是,当我提交表单以发送电子邮件时,我收到以下错误:

  • 致命错误:未捕获的PHPMailer\PHPMailer\异常:SMTP错误:无法连接到SMTP主机。无法连接到服务器在/home/raso 1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php:2233堆栈跟踪:#0 /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(2019):PHPMailer\PHPMailer\PHPMailer-〉smtpConnect(Array)#1 /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1678):PHPMailer\PHPMailer\PHPMailer-〉smtpSend('Date:星期六,8 Ap...','这是HTM...')#2 /home/raso 1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.php(1516):Copyright © 2018 - 2019 www.cnjs.com.cn All Rights Reserved.粤ICP备15048888号-1PHPMailer\PHPMailer\PHPMailer-〉send()#4 {main} thrown in /home/raso1970/cliquedigitale.com4muz.com/vendor/phpmailer/phpmailer/src/PHPMailer.phpon line 2233*

我用email.php写了这段代码:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/////// START PHPMAILER //////
 
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
 
?>
 
<!-- Configuration-->
<?php require_once("./classes/Config.php"); ?>
 
<?php
 
//Load Composer's autoloader
require 'vendor/autoload.php';
 
 
/**
 *
 *
 * Configure PHPMailer
 *
 * 
 */
 
 
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
 
    //Server settings
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                   //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = Config::SMTP_HOST;                      //Set the SMTP server to send through
    $mail->Username   = Config::SMTP_USER;                      //SMTP username
    $mail->Password   = Config::SMTP_PASSWORD;                  //SMTP password
    $mail->Port       = Config::SMTP_PORT;                      //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication                     
    $mail->SMTPSecure = 'tls';                                  //Enable implicit TLS encryption
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
 
 
    //Recipients
    $mail->setFrom("john@doe.com", "John Doe");
    $mail->addAddress("doe@john.com");

    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

////// END PHPMAILER /////
 
 
///// START SENDING EMAIL //////
 
if($mail->send()){
    echo "IT WAS SENT";
} else {
    echo "NOT SENT";
}
 
///// END SENDING EMAIL /////

?>

然后,我在contact.php中的action属性中使用这个文件:

<!-- Start contact form section -->
      <section class="contact__form--section section--padding pt-0">
        <div class="container">
          <div class="section__heading text-center mb-40">
            <h2 class="section__heading--maintitle mb-10">Une Question ?</h2>
            <p class="section__heading--desc">
              Merci de nous laisser un message en indiquant le sujet de votre
              consultation, nous vous répondrons dans les 24h ouvrées.
            </p>
          </div>
          <div class="main__contact--area">
            <div class="row">
              <div class="col-xl-5 col-lg-6 col-md-6">
                <div class="contact__form--thumbnail">
                  <img
                    class="contact__form--thumbnail__img"
                    src="assets/img/other/contact-thumb2.webp"
                    alt="contact-thumb"
                  />
                </div>
              </div>
              <div class="col-xl-7 col-lg-6 col-md-6">
                <div class="contact__form">
                  <form
                    class="contact__form--inner"
                    id="contactFormData"
                    action="email.php"
                    method="POST"
                  >
                    <div class="row">
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input1"
                            >Prénom
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="fname"
                            required="required"
                            pattern="[0-9A-Za-z\- ]+"
                            id="input1"
                            placeholder="Votre Prénom"
                            type="text"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input2"
                            >Nom
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="lname"
                            required="required"
                            pattern="[0-9A-Za-z\- ]+"
                            id="input2"
                            placeholder="Votre Nom"
                            type="text"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input3"
                            >Numéro de Téléphone
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            id="input3"
                            placeholder="Numéro de Téléphone"
                            type="number"
                            name="phone"
                            required="required"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input4"
                            >Email
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="email"
                            id="input4"
                            placeholder="Email"
                            type="email"
                            required="required"
                          />
                        </div>
                      </div>
                      <div class="col-12">
                        <div class="contact__form--list mb-10">
                          <label class="contact__form--label" for="input5"
                            >Votre Message
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <textarea
                            class="contact__form--textarea"
                            name="message"
                            id="input5"
                            placeholder="Votre Message"
                            required="required"
                          ></textarea>
                        </div>
                      </div>
                    </div>
                    <button
                      class="contact__form--btn primary__btn"
                      type="submit"
                      name="submit"
                    >
                      Envoyer
                    </button>
                    <p id="my-form-status"></p>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
      <!-- End contact section -->

我期待在Mailtrap中的收件箱中收到一封电子邮件。然而,我得到了上面的错误。

**编辑:**现在我使用o2 switch SMTP主机,因为我想把这个项目上线。目前我可以连接到SMTP主机。但是我得到了一个错误。

2023-04-08 19:32:24服务器-〉客户端:220-onglet.o2switch.netESMTP Exim 4.95 #2 Sat,08 Apr 2023 21:32:24 +0200 220-We do not authorize the use of this system to transport unsolicited,220 and/or bulk e-mail. 2023-04-08 19:32:24 CLIENT -〉SERVER:EHLOcliquedigitale.com4muz.com2023-04-08 19:32:24服务器-〉客户端:250-onglet.o2switch.net好cliquedigitale.com4muz.com109.234.164.157]250-SIZE 52428800250- 8BITMIME 250-PIPELINING 250-PIPE_CONNECT250-AUTH PLAIN LOGIN 250 HELP 2023-04-08 19:32:24客户端-〉服务器:验证登录2023-04-08 19:32:24服务器-〉客户端:334 VXNlcm 5 hbWU 6 2023-04-08 19:32:24客户端-〉服务器:[credentials hidden] 2023-04-08 19:32:24 SERVER -〉CLIENT:334 UGFzc 3dvcmQ 6 2023-04-08 19:32:24客户端-〉服务器:[credentials hidden] 2023-04-08 19:32:24 SERVER -〉CLIENT:235验证成功2023-04-08 19:32:24客户端-〉服务器:邮箱:FROM:soso@gmail.com2023-04-08 19:32:24服务器-〉客户端:250 OK 2023-04-08 19:32:24客户端-〉服务器:RCPTTO:soso@gmail.com2023-04-08 19:32:24服务器-〉客户端:250已接受2023-04-08 19:32:24客户端-〉服务器:数据2023-04-08 19:32:24服务器-〉客户端:354输入消息,以“.”结尾,单独一行2023-04-08 19:32:24 CLIENT -〉SERVER:日期:2023年4月8日星期六21:32:24 +0200 2023-04-08 19:32:24客户端-〉服务器:发送至:soso@gmail.com 2023-04-08 19:32:24客户端-〉服务器:来自:Sofiane soso@gmail.com 2023-04-08 19:32:24客户端-〉服务器:主题:$title 2023-04-08 19:32:24客户端-〉服务器:消息ID:OradUdYR5sVTJuSvr9I7kFmeGDeJelsezuQ2lvCaE8@cliquedigitale.com4muz.com2023-04-08 19:32:24客户端-〉服务器:X-Mailer:PHPMailer 6.7.1(https://github.com/PHPMailer/PHPMailer)2023-04-08 19:32:24 CLIENT -〉SERVER:MIME版本:1.0 2023-04-08 19:32:24客户端-〉服务器:内容类型:text/html; charset=UTF-8 2023-04-08 19:32:24客户端-〉服务器:2023-04-08 19:32:24客户端-〉服务器:$comment 2023-04-08 19:32:24客户端-〉服务器:2023-04-08 19:32:24客户端-〉服务器:. 2023-04-08 19:32:24服务器-〉客户端:250 OK id= 1 plEIR-0001 hK-Mh 2023-04-08 19:32:24客户端-〉服务器:退出2023-04-08 19:32:24服务器-〉客户端:221onglet.o2switch.net关闭连接SOSO,我们收到您的意见。
我不知道现在的问题是什么?
下面是email.php的代码:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/////// START PHPMAILER //////
 
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
 
?>
 
<!-- Configuration-->
<?php require_once("./classes/Config.php"); ?>
 
<?php
 
//Load Composer's autoloader
require 'vendor/autoload.php';

        // Your email address
        $to = "contact@com4muz.com";
        
        // Get first name value
        $fname = $_POST['fname'];
        
        // Get Last name value
        $lname = $_POST['lname'];
        
        // Get phone number
        $phone = $_POST['phone'];
 
        // Get email value
        $email = $_POST['email'];
        
        // Get comment text
        $comment = $_POST['message'];
        
        // Email tile
        // if client name is Robert, title will be "Robert send you an email"
        $title = "$fname $lname send you an email"; 
        
        // Email parameters
        // $mailText .= "Name: $fname $lname \n";
        // $mailText .= "Phone: $phone \n";
        // $mailText .= "Email: $email \n";
        // $mailText .= "Message: $comment \n";
        
        // Set "From Header parameter"
        // $headers = 'From:' . $email;
        
        // Send email
    //     $sendMail = mail($to, $title, $mailText, $headers);

/**
 *
 *
 * Configure PHPMailer
 *
 * 
 */
 
 
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
 
 
// try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->isSMTP();
    $mail->Host       = Config::SMTP_HOST;
    $mail->Username   = Config::SMTP_USER;
    $mail->Password   = Config::SMTP_PASSWORD;
    $mail->Port       = Config::SMTP_PORT;                     
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = 'ssl';
    $mail->isHTML(true);
    $mail->CharSet = 'UTF-8';
 
 
    //Recipients
    $mail->setFrom("$email", "$fname");
    $mail->addAddress("$email");                               //Add a recipient
    // $mail->addAddress('ellen@example.com');               //Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');
 
    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
 
    //Content
    // $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = '$title';
    $mail->Body    = '$comment';
    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
 
//     $mail->send();
//     echo 'Message has been sent';
// } catch (Exception $e) {
//     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
 
////// END PHPMAILER /////
 
 
///// START SENDING EMAIL //////
 
if ($_POST) {
    
    // if all informations are filled
    if (($_POST['fname']) && ($_POST['lname']) && ($_POST['phone']) && ($_POST['email']) && ($_POST['message'])) {
        
        // If email is sucessful send
        if($mail->send()){

            // Write sucessful message
            echo "<strong>$lname</strong>, we received your comment.";
            exit;

              // If email is NOT sucessful send
        } else {
            
            // Write  message
            echo "<strong>Error</strong>, please try again.";
            exit;
        }
        
        // If we dont have POST method
    } else {
        
        // Write message
        echo "<strong>Error</strong>, please try again.";
        exit;
    }
}

///// END SENDING EMAIL /////

?>

下面是我的contact.php代码:

<div class="col-xl-7 col-lg-6 col-md-6">
                <div class="contact__form">
                  <form
                    class="contact__form--inner"
                    id="contactFormData"
                    action="email.php"
                    method="POST"
                  >
                    <div class="row">
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input1"
                            >Prénom
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="fname"
                            required="required"
                            pattern="[0-9A-Za-z\- ]+"
                            id="input1"
                            placeholder="Votre Prénom"
                            type="text"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input2"
                            >Nom
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="lname"
                            required="required"
                            pattern="[0-9A-Za-z\- ]+"
                            id="input2"
                            placeholder="Votre Nom"
                            type="text"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input3"
                            >Numéro de Téléphone
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            id="input3"
                            placeholder="Numéro de Téléphone"
                            type="number"
                            name="phone"
                            required="required"
                          />
                        </div>
                      </div>
                      <div class="col-lg-6 col-md-6 col-sm-6">
                        <div class="contact__form--list mb-20">
                          <label class="contact__form--label" for="input4"
                            >Email
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <input
                            class="contact__form--input"
                            name="email"
                            id="input4"
                            placeholder="Email"
                            type="email"
                            required="required"
                          />
                        </div>
                      </div>
                      <div class="col-12">
                        <div class="contact__form--list mb-10">
                          <label class="contact__form--label" for="input5"
                            >Votre Message
                            <span class="contact__form--label__star"
                              >*</span
                            ></label
                          >
                          <textarea
                            class="contact__form--textarea"
                            name="message"
                            id="input5"
                            placeholder="Votre Message"
                            required="required"
                          ></textarea>
                        </div>
                      </div>
                    </div>
                    <button
                      class="contact__form--btn primary__btn"
                      type="submit"
                      name="submit"
                    >
                      Envoyer
                    </button>
                    <p id="my-form-status"></p>
                  </form>
                </div>
              </div>
e5nqia27

e5nqia271#

我使用的电子邮件的FROM无效。我无法使用外部FROM,来自我不在主机上管理的地址,例如doe@john.com。FROM必须与我在主机上创建的地址相对应。

相关问题