【问题标题】:PHPMailer will not work with XAMPP. No Errors. No Emails. Not working?PHPMailer 不适用于 XAMPP。没有错误。没有电子邮件。不工作?
【发布时间】:2014-11-17 06:44:28
【问题描述】:

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

我根据 web/StackOverFlow/ 文档尝试了以下方法:

  • 从 php.ini 中删除了“extension=php_openssl.dll”行的分号
  • 重新启动 XAMMP
  • 更新的端口和主机值*见下文*
  • 来自 https://github.com/PHPMailer/PHPMailer 的 DLd PHPMailer
  • 在 Youtube 上观看 PHPAcademy 的“Build A PHP Contact Form”视频
  • 实际上他 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');

【问题讨论】:

    标签: gmail xampp phpmailer html-entities contact-form


    【解决方案1】:

    您可以尝试一些事情:

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

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

    【讨论】:

    • 好的。我遵循了你的指导方针,但仍然没有。设法回显ErrorInfo。它说“SMTP 连接()失败”。另外,我之前应该在试图找出问题时提到过,我收到了一封来自 Google 的电子邮件,内容是:“我们最近阻止了对您的 Google 帐户的登录尝试”。只是认为这可能是相关的。
    • 好吧,一条错误信息总比没有好!现在你需要read this。设置 $mail-&gt;SMTPDebug = 3; 以显示 SMTP 对话。
    • 好吧,请阅读与此问题有关的许多问题。他们中的大多数实际上是由你回答的。所以即使我使用 XAMPP 作为本地 Web 服务器。这个问题可能来自我的 ISP?文档指出我可以在我的服务器上运行一些命令。我的印象是 XAMPP 不允许这样做。
    • XAMPP 只是一个安装在服务器上的软件包。没有什么可以阻止你运行其他东西——你只需要进入命令行。关键是你需要在有问题的服务器上运行诊断测试——在本地主机上运行它们什么也不会告诉你。因为(如您所见),这些问题很常见,所以我一直在考虑编写一个基于 PHP 的诊断脚本来执行与 PHPMailer 捆绑的相同操作,但还没有完成。
    • 找不到安装 dnsutils 的明确方法。所以决定用telnet。 (顺便说一句,目前在 Windows 7 上)。运行命令 telnet smtp.gmail.com 587 前三行无论我运行多少次都很快。至于第四行,我得到了这个 220 mx.google.com ESMTP something diff here - gsmtp
    【解决方案2】:

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

     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();
      }
    

    【讨论】:

    • 当您没有启用异常时,没有必要将其包装在 try 块中。
    • php 5 默认支持异常处理。不用担心异常:)
    • 没有。除非您将true 传递给构造函数,否则PHPMailer 不会生成异常。
    • 我认为它会产生异常,如果有的话。如果没有,那么你可以在代码“$mail->SMTPDebug = 2;”中添加额外的行;)
    • 没有。错误的。除非您使用$mail = new PHPMailer(true); 创建 PHPMailer 实例,否则它不会生成任何外部异常。调高调试输出不会有什么不同。我知道这一点是因为我写了 PHPMailer 的异常系统。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2014-01-14
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多