【问题标题】:smtp php email script not sending emailsmtp php电子邮件脚本不发送电子邮件
【发布时间】:2015-01-13 07:03:04
【问题描述】:

由于某种原因,根本没有发送电子邮件。我没有收到任何控制台错误,输入字段甚至像他们应该的那样重置。它正在 Windows 服务器上进行处理,因此需要 gmail smtp。有什么想法吗?

<?php

require 'PHPMailerAutoload.php';
require_once('class.phpmailer.php');
include("class.smtp.php");

$emailaddress = 'levyandrew44@gmail.com';
$message=
'Name:  '.$_POST['name'].'<br />
Email:  '.$_POST['email'].'<br />
Phone:  '.$_POST['phone'].'<br />
Comments:   '.$_POST['comments'].'<br />
'.nl2br($_POST['message']).'
';

$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->SMTPDebug  = 2;                     // 1 = errors and messages,2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Host       = "smtp.gmail.com"; // sets the SMTP server
$mail->Port       = 465;                    // set the SMTP port for the GMAIL server
$mail->Username   = "info@newpointdigital.com"; // SMTP account username (the email account your created)
$mail->Password   = "newpoint!@#$";        // SMTP account password (the password for the above email account)
$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted
$mail->CharSet  = 'UTF-8';  // so it interprets foreign characters
$mail->SetFrom($_POST['email']);
$mail->AddReplyTo($_POST['email']);
$mail->Subject    = "Contact form from ".$_POST['name']." ";
$mail->MsgHTML($message);
$mail->AddAddress($emailaddress);                    
if(isset($_POST['submit'])) {
    if(!$mail->send()) {
        echo '<p class="contact-message">Message could not be sent.</p>';
        echo '<p class="contact-message">Mailer Error: ' . $mail->ErrorInfo . '</p>';
    } else {
        echo '<p class="contact-message">Your message has been sent. We will be in touch.';
    }       
}
?>
  <form role="form" method="post" id="contact-form" name="myemailform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#contact-form">

   <div class="row">
    <div class="col-sm-12 col-md-12 col-lg-12 "><input type="text" class="form-control" name="name" placeholder="Name*"></div>

   <div class="col-sm-12 col-md-12 col-lg-12"><input type="text" class="form-control" name="email" placeholder="E-mail*"></div>

   <div class="col-sm-12 col-md-12 col-lg-12"><input type="text" class="form-control" name="phone" placeholder="Telephone Number"></div>

   <div class="col-sm-12 col-md-12 col-lg-12"><textarea name="comments" class="form-control commentBox" placeholder="Comments"></textarea></div>

   <div class="col-sm-12 col-md-12 col-lg-12"><input type="submit" value="Send Form" class="form-control submitButton"></div>
       </div>
  </form>

来自 MAMP PHP_ERROR.LOG 的错误

[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 22
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 23
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: phone in /Users/andrewlevy/Documents/deepseadiving/index.php on line 24
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: comments in /Users/andrewlevy/Documents/deepseadiving/index.php on line 25
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: message in /Users/andrewlevy/Documents/deepseadiving/index.php on line 26
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 39
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 40
[12-Jan-2015 17:27:06 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 41
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 22
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 23
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: phone in /Users/andrewlevy/Documents/deepseadiving/index.php on line 24
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: comments in /Users/andrewlevy/Documents/deepseadiving/index.php on line 25
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: message in /Users/andrewlevy/Documents/deepseadiving/index.php on line 26
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 39
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 40
[12-Jan-2015 17:28:15 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 41
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 22
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 23
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: phone in /Users/andrewlevy/Documents/deepseadiving/index.php on line 24
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: comments in /Users/andrewlevy/Documents/deepseadiving/index.php on line 25
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: message in /Users/andrewlevy/Documents/deepseadiving/index.php on line 26
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 39
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: email in /Users/andrewlevy/Documents/deepseadiving/index.php on line 40
[12-Jan-2015 17:28:17 America/New_York] PHP Notice:  Undefined index: name in /Users/andrewlevy/Documents/deepseadiving/index.php on line 41
[12-Jan-2015 17:28:56 America/New_York] PHP Notice:  Undefined index: message in /Users/andrewlevy/Documents/deepseadiving/index.php on line 26
[12-Jan-2015 17:29:04 America/New_York] PHP Notice:  Undefined index: message in /Users/andrewlevy/Documents/deepseadiving/index.php on line 26

【问题讨论】:

  • 您是否检查过是否安装了 SMTP,正如我在此处阅读的那样:The SMTP server is not installed by default. SMTP can be added through the Features Summary area of the Server Manager tool in Windows Server
  • @KoenHoeijmakers hes uding gmail 的服务器
  • 你收到已发送的消息还是未发送的消息?
  • 但是:它正在 Windows 服务器上进行处理
  • 但他没有使用本地邮件服务器,他使用的是 gmail

标签: php email smtp gmail


【解决方案1】:

将您的 class.smtp.php gmail SMTP 类包含到自动加载器文件中。如果您自己的 smtp 类是标准的 PHPMailer 类,那么只需删除该包含并让自动加载器对其进行排序。

在页面顶部也可以用来做类似的事情:

error_reporting(E_ALL);
ini_set('display errors',1);

它应该给你 PHP 警告,而不仅仅是致命错误。

【讨论】:

  • 也取消注释您的 SMTPDebug 行
  • 我习惯于将错误输出到文件,但第一行 - Error_reporting(E_ALL) 会显示警告和错误,无论它们显示在哪里。这可能会为它失败的原因提供线索。
  • 您的 PHP 错误通常出现在哪里?这些将显示在那里。
  • 您正在使用浏览器控制台...不不不,PHP 会将其错误转储到文件中 - 我将在单独的答案中向您展示如何查找错误。找到后,它们会显示您需要修复的内容!
【解决方案2】:

PHP 错误报告:

我有一个印象 - 快速阅读你的原始问题 - 你没有错误,但是通过 cmets 的对话我现在看到你没有看到任何错误,但这不是因为没有错误,而是因为你正在寻找因为他们在错误的地方。

在 PHP 中查找错误:

在页面的最顶部添加这些行:

///set log errors to TRUE
    ini_set("log_errors", 1);
/// show all ERRORS, WARNINGS and NOTICES
    error_reporting(E_ALL);
///set where these errors are recorded.
    ini_set("error_log", "/ *whateveryourwebfolderis* /php-errors.log");

现在运行您的脚本,然后 (S)FTP 到您的服务器空间并找到您拥有的 web 文件夹,其中应该是一个名为“php-errors.log”的文件,下载并使用代码程序打开它或一个记事本,它会从 PHP 中输​​出非常严重的错误。

并向我们提供您从此文件中获得的反馈。

【讨论】:

  • 我在 MAMP 中尝试我的代码并在 php_error.log 中得到这些错误
  • 看起来您的 POST 变量尚未设置
猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2011-07-30
  • 2016-04-17
  • 2011-04-28
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多