PHPMAILER不发送错误并且不发送错误

时间:2013-06-28 12:02:20

标签: php smtp phpmailer

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

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

9 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:13)

您需要致电:

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

来自文档:

  

true param意味着它会在我们需要的错误上抛出异常   赶上。

答案 2 :(得分:6)

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

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

现在像魅力一样工作。

答案 3 :(得分:4)

I had a similar problem. In reference to @Syclone's answer. I was using the default "tls".

$mail->SMTPSecure = 'tls';

After I changed it to $mail->SMTPSecure = 'ssl'; It worked ! My mailserver was only accepting connections over SSL.

答案 4 :(得分:2)

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();
}

答案 5 :(得分:2)

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

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

问题解决了!

答案 6 :(得分:2)

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

希望这有帮助

答案 7 :(得分: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

工作一种享受,让我重新发明了轮子。

答案 8 :(得分:0)

试试这个 ssl 设置:

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