PHPMailer不会通过本地主机发送电子邮件

时间:2020-10-15 16:01:18

标签: php phpmailer

我正在创建一个简单的联系表单,只要有人注册,该表单就会向我发送电子邮件。我的phpmailer函数不会发送任何电子邮件,并且不会在控制台日志中显示任何错误。我已经在互联网上搜索了几天,似乎我的代码与其他人的代码匹配,但是我不确定为什么它不起作用。我正在使用的平台是xampp本地主机。我对php也很陌生。请帮忙:)

编辑:我解决了这个问题。我将ajax用于post方法,并意识到我的isset Submit语句是无用的。删除即可解决问题!

这是我的代码:

<?php
    use PHPMailer\PHPMailer\PHPMailer;
  require_once('phpMailer/Exception.php');
   require_once('phpMailer/PHPMailer.php');
    require_once('phpMailer/SMTP.php');

  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phoneNum'];
  $agent = $_POST['xxx'];
  $day = $_POST['appointment-day'];
  $time = $_POST['time'];


if(isset($_POST['submit'])) {
  if(isset($_POST['email']) && $_POST['email'] != '') {
   if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

     $mail = new PHPMailer();
     $mail->SMTPDebug = 1;
     $mail->Host='smtp.gmail.com';
     $mail->Port=587;
    $mail->SMTPSecure='tls';
    $mail->SMTPAuth=true;
    $mail->Username='xxx@gmail.com';
    $mail->Password='xxx';

     $mail->setFrom('xxxx@gmail.com');
     $mail->addAddress('myemail@gmail.com');
     $mail->addReplyTo($_POST['email'],$_POST['name']);

     $mail->isHTML(true);
     $mail->Subject = "Registration Form";
     $mail->Body ="Registered User's Name: ".$name."\r\n";
     $mail->Body ="Email: ".$email."\r\n";
     $mail->Body ="phone Number: ".$phone."\r\n";
     $mail->Body ="xxx? ".$agent."\r\n";
     $mail->Body ="Appointment Day and Time: ".$day." ".$time."\r\n";

   if(!$mail->Send()){
      echo "message failed";
    } else {
      echo "message sent!";
    }



   }
 }

}

?>

0 个答案:

没有答案