phpmailer无法发送电子邮件

3
一切都好,但出于某种原因,我无法发送电子邮件。
<?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->send()函数总是进入else部分。我做错了什么?

如果您在美国西部(俄勒冈州)以外的地区使用Amazon SES,请将email-smtp.us-west-2.amazonaws.com替换为相应地区的Amazon SES SMTP终端节点。 $mail->Host = ... - Yevgeniy Afanasyev
这个回答解决了你的问题吗?PHP邮件函数无法完全发送电子邮件 - DarkBee
2个回答

1

我认为在编码时始终使用花括号是一个很好的实践。这是针对你的if/else语句。

除此之外,我没有看到你的代码中有任何明显的问题区域。

请确保所有的$_POST变量都输出了预期的值。

同时也将你的消息输出来确保它输出了你预期的值。

你不希望这些参数中有任何一个为空。

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

你还可以使用$mail->ErrorInfo;。这将显示在$mail->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->SMTPDebug的设置。这可能会提供一些额外的信息。请查阅PHPMailer文档以了解各个值及其属性。

先生,我尝试了您的代码,但仍然收到相同的消息 邮件消息未发送 - Nikhil Patil
致命错误:未捕获的PHPMailer\PHPMailer\Exception:无法实例化邮件函数。 - Nikhil Patil
检查更新。PHPMailer 表示您需要指定 use PHPMailer\PHPMailer\Exception; 确保您的 PHPMailer 代码位于正确的路径中。 - Joseph_J
你能否使用PHP的mail()函数发送电子邮件? - Joseph_J

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上另一篇帖子的答案 - 如何在没有Composer的情况下使用PHPMailer?

请检查我刚刚添加的代码,这是我的phpmail设置在顶部..请注意,如果您正在使用SMTP等扩展程序,则也需要添加它。 - tremor
我重新查看了PHPMailer文档,send()函数可以在不指定isSendMail的情况下使用,就像你提到的那样。它将默认使用php的mail()函数,除非另有指定。 - Joseph_J
“isSendmail”选项本质上与使用“isMail”相同(两者最终都通过两种不同的机制调用本地sendmail二进制文件),只是您可以稍微更多地控制它,但两者通常都比使用“isSMTP”到localhost更差、不安全和慢。 - Synchro
1
虽然SMTP更好,但在共享Web托管上可能不可用。 Gmail选项或类似sendgrid的STMP中继始终是一个很好的选择。我发现在实践中,文档关于指定邮件功能的内容是错误的,也许在使用composer安装时可以这样工作,但在PHP实例化时不行。 - tremor

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