为什么 phpmailer 不向我的域邮件服务器发送邮件

我正在尝试将邮件发送到我的域邮件服务器:我使用了 try{} catch(){} 来检测是否存在任何错误,但令人惊讶的是,没有任何错误。


<?php 

function Redirect_to($New_Location){

    header("Location:" . $New_Location);

    exit;

}

if(isset($_POST['Submitenq'])){

    require('phpmailer/PHPMailerAutoload.php');


    define ('GUSER','courses@cadcentreju.org');

    define ('GPWD','mymailpass');


    $recever1 = 'courses@cadcentreju.org';


    $enq_name = $_POST["enq_name"];

    $enq_email = $_POST["enq_email"];

    $enq_phone = $_POST["enq_phone"];

    $enq_message = $_POST["enq_message"];


    date_default_timezone_set("Asia/Kolkata");

    $CurrentTime = time();

    $DateTime = strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);

    

    if(empty($enq_name) || empty($enq_email) || empty($enq_phone) || empty($enq_message)){

        Redirect_to("index.php?error=1");

    }else{

        $mail = new PHPMailer();

        try{

            $mail->IsSMTP();

            $mail->Mailer = "smtp";

                // $mail->SMTPDebug  = 2; 

            $mail->SMTPAuth   = TRUE;

            $mail->SMTPSecure = "ssl";

            $mail->Port       = 465;

            $mail->Host       = "mail.supremecluster.com";

            // $mail->CharSet   = "UTF-8";

            $mail->Username   = GUSER;

            $mail->Password   = GPWD;

            $mail->isHTML(true); 

                $mail->setFrom($enq_email,$enq_name);

                $mail->addAddress($recever1);

        

              $mail->Subject = 'Enquery Mail from - '. $enq_name;

              $mail->Body = '<table class="table" cellspacing="0">

              <thead>

                <tr>

                    <th colspan="2">Enquery Mail from - '. $enq_name .'</th>

                </tr>

              </thead>

它显示电子邮件已成功发送,但我没有收到任何邮件。


我联系了我的服务器帮助热线,他们说某些 IP 被阻止了。但现在我已经从 CPanel 允许了这些 IP。但邮件仍然没有发送


我真的需要这方面的帮助:)


感谢您抽出时间阅读。


撒科打诨
浏览 112回答 3
3回答

潇潇雨雨

PHPMailer 默认情况下不会抛出异常 - 您必须通过传递给构造函数来请求异常true,如$mail = new PHPMailer(true);. 如果没有它,您必须检查方法的返回值,例如send()确定它们是否有效。错误存储在ErrorInfo属性中。您还可以设置$mail->SMTPDebug = 2;查看邮件服务器的内容。

侃侃无极

转到您的 Gmail 帐户 -> 安全找到“访问不太安全的应用程序”。打开它并再次运行您的 PHP 文件。邮件应该已发送。

POPMUISE

我的最终工作代码是:function Redirect_to($New_Location){&nbsp; &nbsp; header("Location:" . $New_Location);&nbsp; &nbsp; exit;}if(isset($_POST['Submitenq'])){&nbsp; &nbsp; require('phpmailer/PHPMailerAutoload.php');&nbsp; &nbsp; define ('GUSER','courses@cadcentreju.org');&nbsp; &nbsp; define ('GPWD','mailpass');&nbsp; &nbsp; $recever1 = 'courses@cadcentreju.org';&nbsp; &nbsp; $enq_name = $_POST["enq_name"];&nbsp; &nbsp; $enq_email = $_POST["enq_email"];&nbsp; &nbsp; $enq_phone = $_POST["enq_phone"];&nbsp; &nbsp; $enq_message = $_POST["enq_message"];&nbsp; &nbsp; date_default_timezone_set("Asia/Kolkata");&nbsp; &nbsp; $CurrentTime = time();&nbsp; &nbsp; $DateTime = strftime("%B-%d-%Y %H:%M:%S",$CurrentTime);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(empty($enq_name) || empty($enq_email) || empty($enq_phone) || empty($enq_message)){&nbsp; &nbsp; &nbsp; &nbsp; Redirect_to("index.php?error=1");&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $mail = new PHPMailer(true);&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->IsSMTP();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Mailer = "smtp";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // $mail->SMTPDebug&nbsp; = 2;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPAuth&nbsp; &nbsp;= TRUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPSecure = "none";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Port&nbsp; &nbsp; &nbsp; &nbsp;= 25;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Host&nbsp; &nbsp; &nbsp; &nbsp;= "mail.supremecluster.com";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // $mail->CharSet&nbsp; &nbsp;= "UTF-8";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Username&nbsp; &nbsp;= GUSER;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Password&nbsp; &nbsp;= GPWD;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->isHTML(true);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->setFrom($enq_email,$enq_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->From = GUSER;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->FromName = $enq_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->addAddress($recever1);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Subject = 'Enquiry Mail from - '. $enq_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->Body = '<table class="table" cellspacing="0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th colspan="2">Enquiry Mail from - '. $enq_name .'</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="2">'. '<p>Dear Sir / Madam, I have some enquiries. My Details are as follows :</p></br>' .'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Email:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'. $enq_email .'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Phone No:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'. $enq_phone .'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Message:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'. $enq_message .'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Date and TIME of Enquery:</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'. $DateTime .'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mail->send();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $success_msg =&nbsp; "Your Message Sent Successfully: ";&nbsp; &nbsp; &nbsp; &nbsp; }catch(phpmailerException $e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $e->errorMessage();&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception $e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $e->getMessage();&nbsp; &nbsp; &nbsp; &nbsp; }我决定从我自己的服务器而不是 Gmail 服务器发送邮件:所以我改变了:$mail->SMTPSecure = "ssl";到$mail->SMTPSecure = "none";最重要的是要记住:$mail->Username&nbsp; &nbsp;= GUSER;和$mail->From = GUSER;必须相同和另外,我们必须将 true 传递给构造函数:$mail = new PHPMailer(true)// 构造函数中的参数 true 启用异常
打开App,查看更多内容
随时随地看视频慕课网APP