PHPMAILER无法发送邮件且没有报错信息。

21
我试图让用户填写联系表格,然后发送到我的电子邮件中。但是由于某些原因它没有起作用。我只收到一个空白页面,没有错误信息或任何文本,电子邮件也没有被发送。
if (isset($_POST['submit']))
{
    include_once('class.phpmailer.php');

    $name = strip_tags($_POST['full_name']);
    $email = strip_tags ($_POST['email']);
    $msg = strip_tags ($_POST['description']);

    $subject = "Contact Form from DigitDevs Website";

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';

    $mail->Host       = "mail.example.com"; // SMTP server example
    //$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "info@example.com"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('info@example.com', 'Information'); 
    $mail->AddReplyTo($email, 'Wale');

    $mail->IsHTML(true);

    $mail->Subject = $subject;

    $mail->Body    =  $msg;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->Send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
}
echo 'Message has been sent';

2
检查服务器日志并找出问题所在。 - Amal Murali
你使用的是哪个操作系统?如果是 Ubuntu,如果邮件发送失败,则会在您的网站根目录中创建一个名为 dead.letter 的文件,请检查一下。 - Mahesh.D
尝试在“exit;”后插入“}”。 - bbldzr
@AmalMurali 这是我在日志中得到的内容:[28-Jun-2013 11:57:02 UTC] PHP致命错误:require_once() [<a href='function.require'>function.require</a>]:在/home/digitdev/public_html/class.phpmailer.php的第1004行打开'class.smtp.php'失败(include_path='.:/usr/lib/php:/usr/local/lib/php')。 - Wale
@Wale:你有按照 INSTALL.txt 文件中的指示进行操作吗,包括下载和安装 PHPMailer 包? - Amal Murali
显示剩余2条评论
9个回答

17

你需要调用:

$mail = new PHPMailer(true); // with true in the parenthesis

从文档中得知:

true 参数表示会在发生错误时抛出异常,我们需要捕获。


6
您,先生,是一个真正的英雄!我一直在与一个虫子作斗争,但一直找不到它,直到现在! - Benjamin Karlog

15

现在它已经可以工作了,我没有包含'class.smtp.php'文件。下面是可用的代码:

 if (isset($_POST['submit']))
{
 include_once('class.phpmailer.php');

 require_once('class.smtp.php');

$name = strip_tags($_POST['full_name']);
$email = strip_tags ($_POST['email']);
$msg = strip_tags ($_POST['description']);

$subject = "Contact Form from DigitDevs Website";

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
//$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 26;                    // set the SMTP port for the GMAIL server
$mail->Username   = "info@example.com"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

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

$mail->AddAddress('info@example.com', 'Information'); 
$mail->AddReplyTo($email, 'Wale');

$mail->IsHTML(true);

$mail->Subject = $subject;

$mail->Body    =  $msg;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
 echo 'Message has been sent';

2
这是正确的。我遇到了同样的问题,在添加了class.smtp.php之后,它就正常工作了。 - Chamath Jeevan
同时,对我来说,也很重要的是需要引入 SMTP.php - Velojet

9

我有同样的问题,即使启用SMTPDebug也没有错误消息。在搜寻有效例子后,我注意到我没有包含SMTP Secure值。尝试添加以下行:

$mail->SMTPSecure = 'ssl'; //secure transfer enabled

现在工作得很出色。

5

我曾经遇到过类似的问题。参考 @Syclone 的回答,我一开始使用的是默认值 "tls"。

$mail->SMTPSecure = 'tls';

后来我将其更改为 $mail->SMTPSecure = 'ssl'; 就成功了!我的邮件服务器只接受 SSL 连接。


2

对我有用的是将From设置为用户名,将FromName设置为$ _POST ['email']

希望这可以帮助到你


1

PHPMailer使用异常。

尝试这个

try {

    include_once('class.phpmailer.php');

    $name = strip_tags($_POST['full_name']);
    $email = strip_tags ($_POST['email']);
    $msg = strip_tags ($_POST['description']);

    $subject = "Contact Form from DigitDevs Website";

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet = 'UTF-8';

    $mail->Host       = "mail.example.com"; // SMTP server example
    //$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "info@example.com"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('info@example.com', 'Information'); 
    $mail->AddReplyTo($email, 'Wale');

    $mail->IsHTML(true);

    $mail->Subject = $subject;

    $mail->Body    =  $msg;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->Send();

    exit;

} catch (phpmailerException $e) {
  echo $e->errorMessage(); //error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage();
}

Dreamweaver会将“catch”代码标记为红色,这意味着语法不正确... - Wale
1
我使用 PHP 标记 "<?php // your code ?>" 测试了代码。它没有解析错误。 - Bastien D
1
刚刚又尝试了一遍,没有代码错误,但它仍然不起作用。我的 error_log 会有帮助吗? - Wale
你的电子邮件是否未发送或未接收?如果没有错误,那么你的电子邮件可能没有被接收。 - Bastien D

1
我试图加载一个HTML文件以发送,但它不属于我的Ubuntu服务器上的www-data组。
chown -R www-data * 
chgrp -R www-data *

问题已解决!


0
我正在考虑是编写自己的处理程序还是将PHPMailer嵌入到我的现有类结构中。最终由于spl_autoload_register函数的通用性,这变得非常容易,因为该函数在PHPMailer系统以及我的现有类结构中都有使用。
我只需在现有的类结构中创建一个基本的“Email”类,如下所示:
<?php

   /**
     * Provides link to PHPMailer
     *
     * @author Mike Bruce
     */
   class Email {
      public $_mailer; // Define additional class variables as required by your application
      public function __construct()
      {
         require_once "PHPMail/PHPMailerAutoload.php" ;
         $this->_mailer = new PHPMailer() ;
         $this->_mailer->isHTML(true);
         return $this;
      }
   }
?>

从调用 Object 类的代码将会是:

$email = new Email;
$email->_mailer->functionCalls();

// continue with more function calls as required

非常好用,而且省去了我重新发明轮子的时间。


0

尝试使用以下 SSL 设置:

$mail->SMTPSecure  = 'tls'; //tls or ssl
$mail->SMTPOptions = array('ssl' => array('verify_peer'       => false,
                                          'verify_peer_name'  => false,
                                          'allow_self_signed' => true));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接