2015-01-12 315 views
0

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

<?php 

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

$emailaddress = '[email protected]'; 
$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 = "[email protected]"; // SMTP account username (the email account your created) 
$mail->Password = "[email protected]#$";  // 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> 

错误FROM 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 
+0

您是否检查过SMTP是否已安装,因为我在这里阅读:'SMTP服务器默认情况下未安装。可以通过Windows服务器上的服务器管理器工具的功能摘要区域添加SMTP – Neat

+0

@KoenHoeijmakers hes uding gmail的服务器 –

+0

您是否收到发送的消息或未发送的消息? –

回答

0

包括你的Gmail class.smtp.php SMTP类到自动加载文件。如果你自己的smtp类是标准的PHPMailer类,那么只需删除那些包含,然后让自动加载器对它进行分类。

在页面的顶部做这样的事情也有用:

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

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

+0

也取消您的SMTPDebug行注释 – Martin

+0

错误会显示我控制台? – aelevy44

+0

我习惯将错误输出到文件中,但第一行 - Error_reporting(E_ALL)将显示警告以及错误,无论它们在何处显示。这可能会给出它为什么失败的线索。 – Martin

0

PHP错误报告:

我是一个印象 - 速读你原来的问题 - 那你有没有错误,但随着意见的谈话,我现在看到你看到没有错误,但这不是因为有没有错误,但是因为你在错误的地方寻找他们。

要查找的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到您的服务器空间,并在网上找到文件夹和它应该是一个名为“php-errors.log”的文件,下载它并用代码程序或记事本打开它,它将输出来自PHP的严重错误。

并给我们你从这个文件得到的反馈。

+0

我正在尝试我的代码在MAMP中,并在php_error.log中得到这些错误 – aelevy44

+0

我把错误放回我原来的帖子 – aelevy44

+0

看起来像你的POST变量还没有设置 – Martin