使用phpmailer无法收到电子邮件

时间:2018-06-01 08:54:39

标签: php phpmailer

我正在尝试通过提交按钮邮寄查询表格。我为此创建了流程表单,并在警报时成功发送了消息。但我无法接收收件箱或垃圾邮件。在我提供我的代码之后,请帮我解决这个错误。

<?php
    session_start();        
    include 'dbconfig.php';     
    include 'phpmailer/PHPMailerAutoload.php';

        $checkin = $_SESSION['checkin'];
        $checkout = $_SESSION['checkout'];
        $room = $_SESSION['room'];
        $guest = $_SESSION['guest'];

        $name = $_POST['name'];
        $email = $_POST['email'];
        $mob = $_POST['mob'];

        $full_message='<table width="650" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">    
        <tr>
            <td width="584" bgcolor="#FFFFFF"><span class="style3">Room Boking Enquiry </span></td>
        </tr>
        <tr>
            <td height="45" valign="middle" bgcolor="#FFFFFF">
                <table border="0" cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                        <td width="59%">
                            <p class="style3"><strong>Name :</strong>'.$name.'</p>
                            <p class="style3"><strong>Email ID: </strong>  '.$email.'</p>                   
                            <p class="style3"><strong>Mobile No.:</strong> '.$mob.'</p>
                            <p class="style3"><strong>Check In:</strong> '.$checkin.'</p>
                            <p class="style3"><strong>Check Out:</strong> '.$checkout.'</p>
                            <p class="style3"><strong>Room:</strong> '.$room.'</p>
                            <p class="style3"><strong>Guest:</strong> '.$guest.'</p>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>       
        </table>';

        if(empty($name)){           
            echo "Please Enter Name.";
        }
        elseif(empty($email)){          
            echo "Please Enter Email Id.";
        }       
        elseif(empty($mob)){
            echo "Please Enter Email Id.";
        }       
        else{
            $mail = new PHPMailer;

            $mail->setFrom('xxxx@xxx.com','xxxx');
            $mail->addAddress($email);     // Add a recipient

            $mail->isHTML(true);           // Set email format to HTML

            $mail->Subject = 'Room Enquiry Form';
            $mail->Body = $full_message;

            if ($mail->send()){
                unset($_SESSION['checkin']);
                unset($_SESSION['checkout']);
                unset($_SESSION['room']);
                unset($_SESSION['guest']);
                session_destroy();                  
                echo 1;
            }
        }
?>

我的代码出了什么问题。我收到了成功的消息,但无法收到邮件。

2 个答案:

答案 0 :(得分:0)

请查看此代码。在新的PHPMailer v6.0 +中,您现在需要将PHPMailer类导入到PHP脚本最顶层的全局命名空间中。

  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;

  require 'path/to/PHPMailer/src/Exception.php';
  require 'path/to/PHPMailer/src/PHPMailer.php';
  require 'path/to/PHPMailer/src/SMTP.php';

  $sentMail = new PHPMailer();
  $sentMail->IsSMTP();
  $sentMail->Host = "mail.example.com";

  $sentMail->SetFrom("$from", "$from");
  $sentMail->AddAddress("$to");

  $sentMail->Subject = "$subject";
  $sentMail->Body = "$message";
  $sentMail->Send();

答案 1 :(得分:0)

以下是我如何设置,只需填写每个字段的信息。如果您有网络托管,您可以在cPanel - &gt;中找到您的信息。 Webmail - &gt;配置邮件客户端。

如果您没有网络托管,则可以通过Gmail的服务器发送邮件。如何使用它的一个例子是在GitHub上:https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = "xxxxxxx.com";

//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Send HTML or Plain Text Email
$mail->isHTML(true);

//Username to use for SMTP authentication
$mail->Username = "xxx@xxxxx.com";

//Password to use for SMTP authentication
$mail->Password = "mypassword";

//Set who the message is to be sent from
$mail->setFrom('xxx@xxxxx.com', $name);

//Set an alternative reply-to address
$mail->addReplyTo($email, $name);

//Set who the message is to be sent to
$mail->addAddress('xxx@xxxxx.com', 'JayCuellar');

//Set the subject line
$mail->Subject = 'Subject Line';