2015-06-22 116 views
0

我试图在页面底部设置联系表单。我正在使用PHPMailer并尝试通过我的Gmail帐户接收电子邮件。每次我提交表单时,我的网址都会变为url/email.php,并且我会得到一个空白页面。我究竟做错了什么?任何人都可以看到我明显缺少的明显错误?PHPMailer不通过Gmail发送电子邮件,返回空白页

HTML:

<div class="container-fluid"> 
     <form action="email.php" id="contact-me" method="post" name="contact-me"> 
      <div class="row-fluid"> 
      <div class="form-group"> 
       <div class="col-md-3 col-md-offset-3"> 
       <input class="input" name="name" id="name" placeholder="Name" type="text"> 
       </div> 
       <div class="col-md-3"> 
       <input class="input" id="email" name="email" 
       placeholder="Email" type="text"> 
       </div> 
      </div> 
      </div> 
      <div class="form-group"> 
       <textarea class="input input-textarea" id="message" name="message" placeholder="Type your message here" rows= 
       "5"></textarea> 
      </div> 
      <input name="send" class="send" type="submit" value="Get In Touch"> 
     </form> 
    </div> 
</section> 

PHP:

<?php 
if (isset($_POST['submit'])) 
{ 
require('PHPMailer-master/PHPMailerAutoload.php'); 
require_once('PHPMailer-master/class.smtp.php'); 
include_once('PHPMailer-master/class.phpmail.php'); 

$name = strip_tags($_Post['name']); 
$email = strip_tags($_POST['email']); 
$msg = strip_tags($_POST['message']); 
$subject = "Message from Portfolio"; 

$mail = new PHPMailer(); 
$mail->isSMTP(); 
$mail->CharSet = 'UTF-8'; 
$mail->Host = 'smtp.gmail.com'; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'tls';        
$mail->Port = 587;  
$mail->Username = '[email protected]';  
$mail->Password = '***********'; 

$mail->From = $email; 
$mail->FromName = $name; 

$mail->AddAddress("me.com", "Katia Sittmann"); 
$mail->AddReplyTo($email, $name); 
$mail->isHTML(true); 

$mail->WordWrap = 50; 
$mail->Subject =$subject; 
$mail->Body = $msg; 
$mail->AltBody ="No message entered"; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 
}else{ 
    header("Location: thank-you.html"); 
} 
} 
?> 
+0

您正在使用'isset()',它会掩盖任何有关'submit'与'send'表单字段不匹配的通知。 – mario

+0

尝试使用try catch,看看它是否告诉你anthing –

+0

因此,如果我取出isset()应该使用它的位置?我也尝试了一次尝试,并没有发现任何东西。有一段时间,网络抛出了错误500,但我需要更改端口来解决这个问题,并且已经这样做了。仍然得到一个空白页面,没有电子邮件。 – user2832346

回答

0

你有一些非常混乱代码在这里。你应该从最新的例子开始,而不是老的例子,并确保你使用最新的PHPMailer(至少5.2.10)。

不要这样做:

require('PHPMailer-master/PHPMailerAutoload.php'); 
require_once('PHPMailer-master/class.smtp.php'); 
include_once('PHPMailer-master/class.phpmail.php'); 

所有你需要的是这样的,如果和在需要时将自动加载其他类:

require 'PHPMailer-master/PHPMailerAutoload.php'; 

这将无法正常工作:

$name = strip_tags($_Post['name']); 

PHP变量是大小写敏感的,所以这样做:

$name = strip_tags($_POST['name']); 

这将导致问题:

$mail->From = $email; 

当你这样做,你伪造发件人地址,它将通过SPF检查遭到拒绝。把你自己的地址放在这里,然后让回复 - 携带提交者的地址,就像你已经在做的那样。

Submit如果未通过该控件提交表单,则可能不会包含在提交中。如果它是通过在文本输入中返回来提交的。检查你知道永远是在提交场:

if (array_key_exists('email', $_POST)) {... 

添加一个try/catch起不到任何东西 - 除非你问它的PHPMailer不抛出异常,而你不是。

你正在申请strip_tags身上,但然后打电话$mail->isHTML(true); - 你想HTML或不?

这一切都说,这些东西都不会造成空白页。这是相当有可能你正在运行到这是最近流行的Gmail的权威性问题,一看就知道你需要启用调试输出:

$mail->SMTPDebug = 2; 
上你所看到的

,然后read the troubleshooting guide

+0

我能够把它全部整理出来!感谢您的帮助!这确实是Gmail身份验证问题的一个问题! – user2832346

相关问题