【问题标题】:PHPMAILER Not sending and not giving errorPHPMAILER 不发送也不给出错误
【发布时间】:2013-06-26 04:54:15
【问题描述】:

我正在尝试让用户填写联系表格,然后将其发送到我的电子邮件中。但由于某种原因它不起作用。我只是得到一个空白页,没有错误消息或任何文本和电子邮件也没有发送。

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';

【问题讨论】:

  • 检查服务器日志,找出问题所在。
  • 您使用的是哪个操作系统?如果是Ubuntu,如果邮件发送失败,那么在您的Web 根目录中将创建一个名为dead.letter 的文件,一旦检查。
  • 尝试在'exit;'之后插入'}'
  • 或在最后一个'echo'之后?
  • @AmalMurali 这是我在日志中得到的:[28-Jun-2013 11:57:02 UTC] PHP 致命错误:require_once() [ function.require]:在 /home/digitdev/public_html/ 中打开所需的 'class.smtp.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') 失败第 1004 行的 class.phpmailer.php

标签: php smtp phpmailer


【解决方案1】:

您需要致电:

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

来自文档:

true 参数意味着它会在错误时抛出异常,这是我们需要的 赶上。

【讨论】:

  • 先生,您是真正的英雄!我一直在与一个错误作斗争,但直到现在才找到它!
【解决方案2】:

它现在可以工作了,我没有包含“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';

【讨论】:

  • 这个正确。我有同样的问题,添加 class.smtp.php 后它工作正常。
  • 还需要SMTP.php 对我来说也很重要。
【解决方案3】:

即使启用了 SMTPDebug,我也遇到了同样的问题,没有错误消息。在搜索了工作示例后,我注意到我没有包含 SMTP Secure 值。尝试添加这一行:

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

现在像魅力一样工作。

【讨论】:

    【解决方案4】:

    我遇到了类似的问题。参考@Syclone 的回答。我使用的是默认的“tls”。

    $mail->SMTPSecure = 'tls';

    我改成之后 $mail->SMTPSecure = 'ssl'; 有效 !我的邮件服务器只接受 SSL 连接。

    【讨论】:

      【解决方案5】:

      对我有用的是将 From 设置为 Username 并将 FromName 设置为 $_POST['email']

      希望对你有帮助

      【讨论】:

        【解决方案6】:

        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”中的代码高亮为红色,表示语法不正确...
        • 我用 php balise "" 测试了代码。它没有解析错误。
        • 刚刚又试了一次,没有代码错误,但还是不行。我的 error_log 会有帮助吗?
        • 您的电子邮件未发送或未收到?没有错误,您的电子邮件可能没有收到
        【解决方案7】:

        我正在尝试加载要发送的 HTML 文件,该文件不属于我的 Ubuntu 服务器上的 www-data 组。

        chown -R www-data * 
        chgrp -R www-data *
        

        问题解决了!

        【讨论】:

          【解决方案8】:

          我正在争论是否将我自己的处理程序或撬棒 PHPMailer 编写到我现有的类结构中。由于在 PHPMailer 系统中使用的 spl_autoload_register 函数的多功能性以及我现有的类结构,这非常容易。

          我只是在现有的类结构中创建了一个基本类电子邮件,如下所示

          <?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
          

          工作很愉快,让我不必重新发明轮子。

          【讨论】:

            【解决方案9】:

            试试这个 ssl 设置:

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

            【讨论】:

              猜你喜欢
              • 2019-10-31
              • 2014-07-21
              • 2016-08-23
              • 1970-01-01
              • 2016-06-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多