Gmail PHPMailer不能与XAMPP一起工作。没有错误。没有电子邮件。不工作?

Gmail PHPMailer不能与XAMPP一起工作。没有错误。没有电子邮件。不工作?,gmail,xampp,phpmailer,html-entities,contact-form,Gmail,Xampp,Phpmailer,Html Entities,Contact Form,我目前正在使用XAMPP和PHPMailer在我的测试网站上创建一个简单的“联系我”电子邮件表单。问题如下。我似乎无法发送测试电子邮件以确保它是否正常工作。我花了周末的大部分时间试图找出这个问题的根源,但我似乎仍然找不到答案。它甚至没有告诉我是否出了问题 根据web/StackOverFlow/Documentation,我尝试了以下方法: 从php.ini中删除了“extension=php\u openssl.dll”行的分号 重新启动XAMMP 更新的端口和主机值*见下文* 来自的DLd

我目前正在使用XAMPP和PHPMailer在我的测试网站上创建一个简单的“联系我”电子邮件表单。问题如下。我似乎无法发送测试电子邮件以确保它是否正常工作。我花了周末的大部分时间试图找出这个问题的根源,但我似乎仍然找不到答案。它甚至没有告诉我是否出了问题

根据web/StackOverFlow/Documentation,我尝试了以下方法:
  • 从php.ini中删除了“extension=php\u openssl.dll”行的分号
  • 重新启动XAMMP
  • 更新的端口和主机值*见下文*
  • 来自的DLd PHP编译器https://github.com/PHPMailer/PHPMailer
  • 在Youtube上观看了PHPAcademy的“”视频
  • 事实上,他95%的代码都是他的…只是对我不起作用
  • 在他的视频上留下了评论。目前还没有回应
我浏览了这个网站上的大部分问题,没有找到任何可以解决我问题的方法。到目前为止,我正在阅读一些文档。我似乎在做正确的事情。因此,我很困惑,这种简单的形式不适合我

contact.php

<?php
  session_start();
  require_once 'helpers/security.php';
  $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
  $fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];

?>

<!DOCTYPE html>
<html lang="eng">
  <head>
    <meta charset="UTF-8">
    <title>Contact Form</title>
    <link rel="stylesheet" href="indexContactStyle.css">

  </head>
<body>
  <div class="contact">
  <?php if( !empty($errors)) :  ?>
    <div class="panel">  
      <ul>  <li>  <?php echo implode('</li><li>', $errors ); ?>  </li>  </ul>
    </div>
  <?php endif ?>

  <form action="contact.php" method="post">
    <label>
      Your Name * <input type="text" name="inputName" autocomplete="off" <?php echo isset($fields['name']) ? ' value= " '. e($fields['name']) .' " ' : '' ?> >
    </label>
    <label>
      Your Email * <input type="text" name="inputEmail" autocomplete="off" <?php echo isset($fields['email']) ? ' value= " '. e($fields['email']) .' " ' : '' ?> >
    </label> 
    <label>
      Your Message * <textarea name="message" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea>
    </label>       
    <input type="submit" value="Send">
    <p class="muted"> * means a required field</p>
    <button class="muted" type="button" onClick="goBack()">Back</button>
      </form>
    </div>
  </body>
</html>

<?php
  unset($_SESSION['errors']);
  unset($_SESSION['fields']);
?>

联系方式
    • 您的名字*> 你的留言*

      *表示必填字段

      返回
index.php

<?php

session_start();

require_once 'libs/phpMailer/PHPMailerAutoload.php';


$errors = []; 

print_r($_POST);
if (isset($_POST['inputName'], $_POST['inputEmail'], $_POST['message'])) {


$fields = [
    'name' => htmlentities( $_POST['inputName'] ),
    'email' => $_POST['inputEmail'],
    'message'=> $_POST['message']

];



foreach ($fields as $field => $data) {

    if( empty($data) ) {
        $errors[] = 'The ' . $field . 'field is required.';
    }

} 

if(empty($errors)) {
    $m = new PHPMailer;

    $m->isSMTP();
    $m->SMTPauth = true;
    $m->SMTDebug = true;
    $m->Host ='smtp.gmail.com';
    $m->Username = 'myEmail@gmail.com';
    $m->Password = 'myPass';
    $m->SMTPSecure = 'ssl';
    $m->Port = 465;

    $m->isHTML(true);

    $m->Subject = 'Contact form submitted';

    $m->Body ='From: ' .$fields['name'] .  ' ( ' . $fields['email'] . ' ) <p> ' .  $fields['message'] . ' </p> ' ;

    $m->FromName = 'Contact';
    // Not entirely sure what is supposed to go here. Some ppl put their own email others the email of the sender. which one is it?
    $m->AddAddress('myEmail@gmail.com', 'myName');

    if ( $m->send() ) {
        header('Location: thanks.php');
        die();
    } else {
        $errors[] = 'Sorry could not send email. Try again later ';
    }

}


} else {
$errors[] = 'Something went HORRIBLY WRONG!!';
}


$_SESSION['errors'] = $errors;

$_SESSION['fields'] = $fields;

header('Location: contact.php');

以下代码对我来说很好。试试这个:

 try 
 {
    $mail = new PHPMailer(); 

    $mail->IsSMTP();
    $mail->Mailer = 'smtp';
    $mail->SMTPAuth = true;
    $mail->Host = 'smtp.gmail.com'; 
    $mail->Port = 465;
    $mail->SMTPSecure = 'ssl';


    $m->Username = 'myEmail@gmail.com';
    $m->Password = 'myPass';

    $mail->IsHTML(true); // if you are going to send HTML formatted emails
    $mail->SingleTo = true; 
    $mail->From = "test@abc.com";
    $mail->FromName = "Mailer";
    $mail->AddAddress("ankit@yopmail.com", "Ankit Chugh");   //mail to to
    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";
    $mail->AltBody    = "To view the message, please use an HTML compatible email vie  wer!"; 
    $mail->MsgHTML("Your html message");

    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
  }
  catch (phpmailerException $e) {
      echo $e->errorMessage(); //error messages from PHPMailer
  }
  catch (Exception $e) {
       echo $e->getMessage();
  }

您可以尝试以下几点:

  • PHP属性和变量区分大小写,因此
    SMTPauth
    应该 be
    SMTPAuth
  • SMTPDebug
    接受整数,而不是布尔值,因此将调试输出设置为1或更高,将其设置为0将其关闭
  • 对于gmail,将
    SMTPSecure
    设置为“tls”,并将
    Port
    设置为587
  • addAddress
    用于添加普通的“收件人”-您可以为希望邮件发送到的每个人(可以有多个)调用此功能
  • 成功发送后不要调用
    die()
    -如果不调用,脚本将正常终止。-如果您正在生成任何调试输出,则重定向到“谢谢”页面将不起作用(您将收到“headers ready sent”(标题已发送)错误),因此,在调试时,您可能希望只在此处回显某些内容,以便知道它已起作用

大部分代码看起来都像是编写课程的人使用的是旧版本的PHPMailer。

如果您没有启用异常,则将其包装在try块中没有意义。默认情况下,php 5支持异常处理。不要担心异常:)否。PHPMailer不会生成异常,除非您将
true
传递给构造函数。我认为如果有异常,它将生成异常。如果没有异常,那么您可以在代码“$mail->SMTPDebug=2;”中添加额外的行)不,错了。除非使用
$mail=new PHPMailer(true)创建PHPMailer实例,否则它不会生成任何外部异常。打开调试输出不会有任何区别。我之所以知道这一点,是因为我编写了PHPMailer的异常系统。我遵循了你的指导方针,但仍然一无所获。设法回显错误信息。上面写着“SMTP connect()失败”。我之前也应该提到,在试图找出问题的时候,我收到了一封来自谷歌的电子邮件,上面说:“我们最近阻止了登录您的谷歌帐户的尝试。”。只是觉得这可能有关系。好吧,一条错误消息总比没有好得多!现在你需要。设置
$mail->SMTPDebug=3以显示SMTP对话。我们只需阅读大量与此相关的问题。事实上,大多数问题的答案都是你。所以即使我使用XAMPP作为本地web服务器。此问题可能来自我的ISP?文档说明我可以在服务器上运行一些命令。我的印象是XAMPP不允许这样做。XAMPP只是一个安装在服务器上的包。没有什么可以阻止您运行其他东西-您只需要进入命令行。关键是您需要在有问题的服务器上运行诊断测试—在localhost上运行它们不会告诉您什么。因为(如您所见),这些问题很常见,所以我一直在考虑编写一个基于PHP的诊断脚本,以完成与PHPMailer捆绑的相同任务,但尚未完成。无法找到安装dnsutils的明确方法。所以决定使用telnet。(顺便说一句,目前在Windows7上)。运行命令telnet smtp.gmail.com 587无论我运行了多少次,前三行都过得很快。至于第四行,我得到了这个220 mx.google.com ESMTP的东西在这里不同-gsmtp