【问题标题】:PHPMailer not sending e-mail through Gmail, returns blank pagePHPMailer 不通过 Gmail 发送电子邮件,返回空白页
【发布时间】:2015-09-08 07:37:07
【问题描述】:

我正在尝试在我的页面底部设置一个联系表。我正在使用 PHPMailer 并尝试在我的 gmail 帐户中接收电子邮件。每次我提交表单时,我的 url 都会更改为 url/email.php 并且我得到一个空白页面。我究竟做错了什么?谁能看到我明显遗漏的明显错误?

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 = 'me@gmail.com';       
$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");
}
}
?>

【问题讨论】:

  • 您正在使用isset(),这将屏蔽有关submitsend 表单字段不匹配的任何通知。
  • 尝试使用 try catch 看看它是否告诉你任何事情
  • 那么,如果我取出 isset() 我应该在它的位置使用一些东西吗?我也尝试了一次尝试,但没有找到任何东西。有一段时间网络抛出错误 500,但我需要更改端口来解决这个问题,并且已经这样做了。仍然得到一个空白页,没有电子邮件。

标签: php email gmail phpmailer send


【解决方案1】:

这里有一些非常混乱的代码。您应该从最新的示例开始,而不是旧示例,并确保您使用的是最新的 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 检查拒绝。把你自己的地址放在这里,让reply-to携带提交者的地址,就像你已经在做的那样。

Submit 可能不会包含在提交中,如果表单不是通过该控件提交的,例如如果它是通过在文本输入中按回车提交的。检查您知道将始终在提交中的字段:

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

添加 try/catch 不会产生任何效果 - PHPMailer 不会抛出异常,除非你要求它,而你不是。

您在正文上应用strip_tags,然后调用$mail-&gt;isHTML(true); - 您是否想要HTML?

综上所述,这些都不会导致空白页。您很可能遇到了最近流行的 gmail auth 问题,并且看到您需要启用调试输出:

$mail->SMTPDebug = 2;

然后read the troubleshooting guide 你看到了什么。

【讨论】:

  • 我把这一切都搞定了!谢谢您的帮助!确实是 Gmail 身份验证问题!
猜你喜欢
  • 2013-11-04
  • 1970-01-01
  • 2015-09-23
  • 2014-10-19
  • 2021-03-23
  • 1970-01-01
  • 2012-01-07
  • 2023-03-19
相关资源
最近更新 更多