skip to Main Content

I’m trying to send mail to my domain mail server:
I used a try{} catch(){} to detect if there is any error, but surprisingly, there isn’t any error.

<?php 
function Redirect_to($New_Location){
    header("Location:" . $New_Location);
    exit;
}
if(isset($_POST['Submitenq'])){
    require('phpmailer/PHPMailerAutoload.php');

    define ('GUSER','[email protected]');
    define ('GPWD','mymailpass');

    $recever1 = '[email protected]';

    $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>
              <tbody>
                <tr>
                    <td colspan="2">'. '<p>Dear Sir / Madam, I have some enqueries as follows :</p></br>' .'</td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>'. $enq_email .'</td>
                </tr>
                <tr>
                    <td>Phone No:</td>
                    <td>'. $enq_phone .'</td>
                </tr>
                <tr>
                    <td>City:</td>
                    <td>'. $sendercity .'</td>
                </tr>
                <tr>
                    <td>Message:</td>
                    <td>'. $enq_message .'</td>
                </tr>
                <tr>
                    <td>Date of Enquery:</td>
                    <td>'. $DateTime .'</td>
                </tr>
              </tbody>
              </table>';
              
              $mail->send();
              $success_msg =  "Your Message Sent Successfully: ";
        }catch(phpmailerException $e){
            echo $e->errorMessage();
        }catch(Exception $e){
            echo $e->getMessage();
        }

    }

}

?>
<?php include 'header.php'; ?>

<div style="height:50vh;margin-top: 268px; background:#ddd;" class="d-flex justify-content-center align-items-center">
    <?php 
        if(isset($success_msg)){
    ?>
    <div class="alert alert-success" role="alert">

        <?php
            echo $success_msg . $enq_name;
        ?>
    </div>
    <?php
        }else{
    ?>
    <div class="alert alert-danger" role="alert">
<?php
    echo "Something went wrong";

?>
</div>
    <?php
        }
    ?>
</div>

<?php include 'footer.php'; ?>

It’s showing me that the email sent successfully, but I’m not getting
any mail.

I’ve contacted my server helpline, they’re saying that certain IPs were blocked. But now I’ve allowed these IPs from the CPanel. But still, the mails are not sent

I really need help on this 🙂

Thanks for taking the time to read.

3

Answers


  1. Chosen as BEST ANSWER

    My final Working Code is:

    function Redirect_to($New_Location){
        header("Location:" . $New_Location);
        exit;
    }
    if(isset($_POST['Submitenq'])){
        require('phpmailer/PHPMailerAutoload.php');
    
        define ('GUSER','[email protected]');
        define ('GPWD','mailpass');
    
        $recever1 = '[email protected]';
    
        $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(true);
            try{
                $mail->IsSMTP();
                $mail->Mailer = "smtp";
                    // $mail->SMTPDebug  = 2; 
                $mail->SMTPAuth   = TRUE;
                $mail->SMTPSecure = "none";
                $mail->Port       = 25;
                $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->From = GUSER; 
                $mail->FromName = $enq_name;
    
                $mail->addAddress($recever1);
            
                  $mail->Subject = 'Enquiry Mail from - '. $enq_name;
                  $mail->Body = '<table class="table" cellspacing="0">
                  <thead>
                    <tr>
                        <th colspan="2">Enquiry Mail from - '. $enq_name .'</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                        <td colspan="2">'. '<p>Dear Sir / Madam, I have some enquiries. My Details are as follows :</p></br>' .'</td>
                    </tr>
                    <tr>
                        <td>Email:</td>
                        <td>'. $enq_email .'</td>
                    </tr>
                    <tr>
                        <td>Phone No:</td>
                        <td>'. $enq_phone .'</td>
                    </tr>
                    <tr>
                        <td>Message:</td>
                        <td>'. $enq_message .'</td>
                    </tr>
                    <tr>
                        <td>Date and TIME of Enquery:</td>
                        <td>'. $DateTime .'</td>
                    </tr>
                  </tbody>
                  </table>';
                  
                  $mail->send();
                  $success_msg =  "Your Message Sent Successfully: ";
            }catch(phpmailerException $e){
                echo $e->errorMessage();
            }catch(Exception $e){
                echo $e->getMessage();
            }
    

    I decided to send mail from my own server instead of Gmail server:

    So I changed:

    $mail->SMTPSecure = "ssl";
    

    to

    $mail->SMTPSecure = "none";
    

    Most Important thing to remember that:

    $mail->Username   = GUSER;
    

    And

    $mail->From = GUSER;
    

    MUST BE SAME

    AND

    Also, We have to pass true to the constructor:

    $mail = new PHPMailer(true)
    

    // Argument true in constructor enables exceptions


  2. PHPMailer doesn’t throw exceptions by default – you have to ask for them by passing true to the constructor, as in $mail = new PHPMailer(true);. Without that you have to check the return values from methods like send() to find out if they worked.

    Errors are stored in the ErrorInfo property – see any of the code examples provided with PHPMailer to see how to handle errors correctly.

    You can also set $mail->SMTPDebug = 2; to see what your mail server is saying. Beyond that, read the PHPMailer troubleshooting guide.

    Login or Signup to reply.
  3. Go to your Gmail Account -> Security

    Find "Access less secure app". Turn it ON and Run your PHP file again.

    The Mail should be sent.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search