php PHPMAILER 不发送也不报错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17364323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:45:01  来源:igfitidea点击:

PHPMAILER Not sending and not giving error

phpsmtpphpmailer

提问by Wale

I am trying to let users fill out a contact form, which will then be sent to my email. But its not working for some reason. I just get a blank page with no error message or any text and email is also not sent.

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

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   = "[email protected]"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('[email protected]', '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';

回答by user3670588

You need to call:

您需要致电:

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

From the documentation:

从文档:

The trueparam means it will throw exceptions on errors, which we need to catch.

truePARAM意味着将抛出错误异常,这就需要我们抓住。

回答by Wale

Its working now, i didnt include the 'class.smtp.php' file. The working code is below:

它现在可以工作了,我没有包含“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   = "[email protected]"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

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

$mail->AddAddress('[email protected]', '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';

回答by Syclone

I had the same problem with no error message even with SMTPDebug enabled. After searching around for working examples I noticed that I didn't include the SMTP Securevalue. Try adding this line:

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

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

Work like a charm now.

现在像魅力一样工作。

回答by phil

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

我有一个类似的问题。参考@ Syclone 的回答。我使用的是默认的“tls”。

$mail->SMTPSecure = 'tls';

$mail->SMTPSecure = 'tls';

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

在我将其更改为 $mail->SMTPSecure = 'ssl';它工作后!我的邮件服务器只接受 SSL 连接。

回答by Raf

What worked for me was setting From as Username and FromName as $_POST['email']

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

Hope this helps

希望这可以帮助

回答by Andy

I was trying to load an HTML file to send, which did not belong to the www-data group on my Ubuntu server.

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

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

Problem solved!

问题解决了!

回答by Bastien D

PHPMailer use exception.

PHPMailer 使用异常。

Try this

尝试这个

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   = "[email protected]"; // SMTP account username example
    $mail->Password   = "password";        // SMTP account password example

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

    $mail->AddAddress('[email protected]', '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();
}

回答by Mike M0ITI

I was debating whether to write my own handler or crow-bar PHPMailer into my existing class structure. In the event it was very easy because of the versatility of the spl_autoload_register function which is used within the PHPMailer system as well as my existing class structure.

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

I simply created a basic class Email in my existing class structure as follows

我只是在我现有的类结构中创建了一个基本类 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;
      }
   }
?>

From a calling Object class the code would be:

从调用对象类的代码将是:

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

// continue with more function calls as required

Works a treat and has saved me re-inventing the wheel.

很好用,让我免于重新发明轮子。