PHP Mailer,为什么不起作用...?

时间:2018-07-09 20:14:50

标签: php email gmail phpmailer

我正在尝试让php mailer使用此代码。如果我取出邮递区号,注册表格会生效,否则显示白屏。这是我单击提交时的代码。但是我没有收到电子邮件(用户名等变量位于config.php中)

            <?php
        use PHPMailer\PHPMailer\PHPMailer;
        use PHPMailer\PHPMailer\Exception;
         require_once('verbinding.php');
         include('PHPMailer/config.php');

         require 'PHPMailer/src/PHPMailer.php';
         require 'PHPMailer/src/Exception.php';
         require 'PHPMailer/src/SMTP.php';
         //print_r($_POST);
         if(isset($_POST) & !empty($_POST))
        {
           $username    = mysqli_real_escape_string($conn, $_POST['rusername']);
           $email           = mysqli_real_escape_string($conn, $_POST['remail']);
           $password    = md5($_POST['rpassword']);
           $passwordagain   = md5($_POST['rpassword_2']);
           if($password == $passwordagain)
        { //check double entries voor username
            $usernamesql = "SELECT * FROM `users` WHERE username='$username'";
            $usernameres = mysqli_query($conn, $usernamesql);
            $count = mysqli_num_rows($usernameres); //if count equals to 1 then we are not oging to insert this data  mysqlinumrows expects 1 argument
            if($count == 1){
              echo $fmsg = "Username exists in onze database, please kies een andere username";
        }
            $emailsql = "SELECT * FROM `users` WHERE email='$email'";
            $emailres = mysqli_query($conn, $emailsql);
            $emailcount = mysqli_num_rows($emailres); //if count equals to 1 then we are not oging to insert this data  mysqlinumrows expects 1 argument
            if($emailcount == 1){
              echo $fmsg = "Email exists in onze database, please reset je wachtwoord";
        }
               $sqltest     = "INSERT INTO `users` (username, email, password) VALUES ('$username', '$email', '$password')";
               $result      = mysqli_query($conn, $sqltest) or die(mysqli_error($conn)); //is used to put it in to database table ? die ik nu in een result $variabel stop
              if($result)
        {
              echo $smsg = "Registratie succes";
              //$id = mysqli_insert_id($conn);voor later om id mee te geven denk ik

              //require_once "vendor/autoload.php";

              $mail = new PHPMailer(true);
        try {
              //Enable SMTP debugging.
              $mail->SMTPDebug = 2;
              $mail->isSMTP();
              $mail->Host = $smtphost;
              $mail->SMTPAuth = true;
              $mail->Username = $smtpuser;
              $mail->Password = $smtppass;
              //If SMTP requires TLS encryption then set it
              $mail->SMTPSecure = "tls";
              //Set TCP port to connect to
              $mail->Port = 465; //587 al gerobeerd 465 is voor ssl

              //$mail->From = "ellycodegorilla@gmail.com";
              //$mail->FromName = "Full Name";

              $mail->setFrom('ellycodegorilla@gmail.com', 'Mailer');
              $mail->addAddress('ellydeklein@gmail.com', 'Joe User');     // Add a recipient


              $mail->isHTML(true);

              $mail->Subject = "Subject Text";
              $mail->Body = "<i>Mail body in HTML</i>";
              $mail->AltBody = "This is the plain text version of the email content";
              $mail->send();
              echo "Mail has been send";
          } catch (Exception $e) {
            echo "Mail dit not send";
          }

        }else
        {
                    echo $fmsg = "Fail to register";
        }
        }else
        {
                echo $fmsg = "Password dont match";
        }
        }
        ?>

奇怪的事情是我尝试过此快速方法here

的stackoverflow答案之一

我分别测试了一下,但是没有用。我试图遵循Github manual

但是,老实说,我对采取什么步骤并不十分清楚,我使用Google的captcha选项打开了电子邮件以供外部访问。我使用Kubuntu,对Kubuntu以及PHP和SQL还是陌生的。我正在按照自己支付的一门小课程学习:-S,但是没有反馈。而且提供的代码显然无法正常工作。

1 个答案:

答案 0 :(得分:0)

我问家教,他帮了我,有一些新文件。我相信PHP Mailer的初始设置对于菜鸟来说有点模糊。我似乎无法在任何地方找到自动装带器脚本!

我在SendGrid上设置了一个帐户,看来GMAIL非常安全。也许可以确保安全,但是这是以后的发现。我需要了解身份验证。

因此可以使用以下代码进行工作:

          <?php
      // 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\Exception;

      //include('config.php');

      require 'PHPMailer-master/src/Exception.php';
      require 'PHPMailer-master/src/PHPMailer.php';
      require 'PHPMailer-master/src/SMTP.php';

      $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
      try {
          //Server settings
          $mail->SMTPDebug = 2;                                 // Enable verbose debug output
          $mail->isSMTP();                                      // Set mailer to use SMTP
          $mail->Host = 'smtp.sendgrid.net';  // Specify main and backup SMTP servers
          $mail->SMTPAuth = true;                               // Enable SMTP authentication
          $mail->Username = 'apikey';                 // SMTP username
          $mail->Password = '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';                           // SMTP password
          $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
          $mail->Port = 587;                                    // TCP port to connect to. 25, 587  (for unencrypted/TLS connections)

          $mail->setFrom('e@gmail.com', 'Blog van ELly de Klein');
          $mail->addAddress('e@gmail.com', 'Gebruiker');

          //Content
          $mail->isHTML(true);                                  // Set email format to HTML
          $mail->Subject = 'Here is the subject';
          $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
          $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.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      }
      ?>

感谢您的帮助,还有2票赞成:P有一天,我将获得票赞成!