【问题标题】:phpmailer can not send emailphpmailer无法发送电子邮件
【发布时间】:2019-01-19 18:03:50
【问题描述】:

其他一切正常,但由于某种原因我无法发送电子邮件:

<?php    
$msg="";

use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";

if(isset($_POST['submit'])) {
    $subject=$_POST['subject'];
    $email=$_POST['email'];
    $message=$_POST['message'];


    $mail= new PHPMailer();

     $mail->AddAddress('nkhlpatil647@gmail.com', 'First Name');
     $mail->SetFrom('nkhlpatil647@gmail.com','admin');


    $mail->Subject = $subject; 
   $mail-> isHTML(true); 
   $mail->Body=$message;

    if($mail->send())
        $msg="Your rmail msg has been send";
     else
       $msg="mail msg has not been send";
}
?>

$mail-&gt;send() 函数总是转到else 部分。我做错了什么?

【问题讨论】:

    标签: php email phpmailer send


    【解决方案1】:

    您没有声明发送邮件的内容,这可能是原因之一。 PHPMailer 实际上并不发送电子邮件,它旨在连接到您的 Web 服务器上可以发送电子邮件的东西,例如:sendmail、postfix、与邮件中继服务的 SMTP 连接等,因此您可能需要声明在您的设置中。

    例如,如果您使用的是网络服务器内置的 sendmail,请在

    之后添加
    $mail = new PHPMailer;
    // declare what mail function you are using
    $mail->isSendmail();
    

    PHPMailer 还支持其他几个选项,例如 SMTP 和 gmail。请参阅这组示例以最适合您的方案:https://github.com/PHPMailer/PHPMailer/tree/master/examples

    另外,这是我的设置方式,不确定 require 或 include_once 是否最佳,但我的安装效果很好。另外,我添加了 SMTP 模块,以便通过 sendmail 使用它。

    // require php mailer classes
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    // require php mailer scripts
    require 'src/Exception.php';
    require 'src/PHPMailer.php';
    require 'src/SMTP.php';
    

    这就是我个人安装的 PHPMailer 在实践中的工作方式,通过 PHP 实例化,而不是通过 Composer 安装。我在 SO 上的另一篇文章中使用了这个答案 - How to use PHPMailer without composer?

    【讨论】:

    • 检查我刚刚添加的代码,这就是我的 phpmail 在顶部设置的方式。注意,如果您使用的是 SMTP 之类的扩展,您也需要添加它。
    • 我重新访问了 PHPMailer 文档,并且可以使用 send() 函数,而无需像您所引用的那样指定 isSendMail。除非指定,否则默认为 php mail()
    • isSendmail 选项本质上与使用 isMail 相同(两者最终都通过两种不同的机制调用本地 sendmail 二进制文件),您只是对它有更多的控制权,但两者都是通常比使用 isSMTP 到 localhost 更差、更不安全和更慢。
    • 虽然 SMTP 更胜一筹,但它可能不适用于共享虚拟主机。 Gmail 选项或像 sendgrid 这样的 STMP 中继总是一个不错的选择。我发现在实践中,文档关于指定邮件功能是错误的,也许它在与 composer 一起安装时以这种方式工作,但不是由 PHP 实例化。
    【解决方案2】:

    我相信一直使用花括号是一种很好的编码习惯。这是参考您的 if/else 语句。

    除此之外,我在您的代码中没有看到任何直接跳出并说明问题区域的内容。

    请确保您的所有 $_POST 变量都回显出它们的预期值。

    回应您的消息,以确保它输出您的预期值。

    您不希望这些参数中的任何一个为空。

    PHPMailer 类具有错误处理功能。我建议您使用 try/catch 块来显示任何可能存在的错误并从那里排除故障。

    您也可以使用$mail-&gt;ErrorInfo;。这将显示在$mail-&gt;send() 函数调用之后生成的任何错误。我的答案中包含了这两个概念。

    像这样:

    $msg="";
    
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception; //<---Add this.
    
    //Switch your includes to requires.
    require "PHPMailer/PHPMailer.php";
    require "PHPMailer/Exception.php";
    //require "PHPMailer/SMTP.php";  //If you are using SMTP make sure you have this line.
    
    if(isset($_POST['submit'])) {
    
      try{
    
        $subject =$_POST['subject'];
        $email =$_POST['email'];
        $message =$_POST['message'];
    
    
        //$mail = new PHPMailer();
        $mail = new PHPMailer(true); //Set to true. Will allow exceptions to be passed.
    
        $mail->AddAddress('nkhlpatil647@gmail.com', 'First Name');
        $mail->SetFrom('nkhlpatil647@gmail.com','admin');
    
    
        $mail->Subject = $subject; 
        $mail->isHTML(true); 
        $mail->Body = $message;
    
        if($mail->send()){
    
          $msg="Your email msg has been send";
    
    
        }else{
    
           $msg="mail msg has not been send"; 
           echo 'Mailer Error: ' . $mail->ErrorInfo;
         }
    
       }catch(phpmailerException $e){
    
          echo $e->errorMessage();
    
       }
    
    } 
    

    如果您使用 SMTP,您可以尝试使用$mail-&gt;SMTPDebug 设置。可能会为您提供一些额外的信息。检查 PHPMailer 文档以获取值及其属性。

    【讨论】:

    • 先生,我尝试了您的代码,但仍然收到相同的味精邮件味精尚未发送
    • 致命错误:未捕获的 PHPMailer\PHPMailer\Exception:无法实例化邮件函数。
    • 检查已更新。 PHPMailer 说你需要指定use PHPMailer\PHPMailer\Exception; 确保你的PHPMailer 代码在正确的路径中。
    • 您是否能够使用 PHP 的 mail() 函数发送电子邮件。
    猜你喜欢
    • 2023-03-20
    • 2015-09-06
    • 2020-11-25
    • 2017-07-19
    • 2017-10-05
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多