简体   繁体   中英

PHP Mailer doesn't send the mail yet returns no error

I m trying to use PHP mailer using one of my Gmail accounts to send emails, but for some reason, it has no errors in console, returns success back but doesn't send mail as well.

FYI, I have checked other related questions in the stack that are similar to me, but still feel I need expert advice on it.

Index:

<!DOCTYPE html>
<html>
<head>
    <title>Send an Email</title>
</head>
<body>

    <center>
        <h4 class="sent-notification"></h4>

        <form id="myForm">
            <h2>Send an Email</h2>

            <label>Name</label>
            <input id="name" type="text" placeholder="Enter Name">
            <br><br>

            <label>Email</label>
            <input id="email" type="text" placeholder="Enter Email">
            <br><br>

            <label>Subject</label>
            <input id="subject" type="text" placeholder=" Enter Subject"> 
            <br><br>

            <p>Message</p>
            <textarea id="body" rows="5" placeholder="Type Message"></textarea>
            <br><br>

            <button type="button" onclick="sendEmail()" value="Send An Email">Submit</button> 
        </form>
    </center>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        function sendEmail() {
            var name = $("#name");
            var email = $("#email");
            var subject = $("#subject");
            var body = $("#body");

            if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) {
                $.ajax({
                   url: 'sendEmail.php',
                   method: 'POST',
                   dataType: 'json',
                   data: {
                       name: name.val(),
                       email: email.val(),
                       subject: subject.val(),
                       body: body.val()
                   }, success: function (response) {
                        $('#myForm')[0].reset();
                        $('.sent-notification').text("Message Sent Successfully.");
                   }
                });
            }
        }

        function isNotEmpty(caller) {
            if (caller.val() == "") {
                caller.css('border', '1px solid red');
                return false;
            } else
                caller.css('border', '');

            return true;
        }
    </script>

</body>
</html>

sendEmail.php

<?php
    use PHPMailer\PHPMailer\PHPMailer;

    if (isset($_POST['name']) && isset($_POST['email'])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $subject = $_POST['subject'];
        $body = $_POST['body'];

        require_once "PHPMailer/PHPMailer.php";
        require_once "PHPMailer/SMTP.php";
        require_once "PHPMailer/Exception.php";

        $mail = new PHPMailer();

        //SMTP Settings
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->Username = "harishkolliparat@gmail.com"; //enter you email address
        $mail->Password = '*****'; //enter you email password
        $mail->Port = 465;
        $mail->SMTPSecure = "ssl";
        

        //Email Settings
        $mail->isHTML(true);
        $mail->setFrom($email, $name);
        $mail->addAddress("harishkolliparat@gmail.com"); //enter you email address
        $mail->Subject = ("$email ($subject)");
        $mail->Body = $body;

        if ($mail->send()) {
            $status = "success";
            $response = "Email is sent!";
        } else {
            $status = "failed";
            $response = "Something is wrong: <br><br>" . $mail->ErrorInfo;
        }

        exit(json_encode(array("status" => $status, "response" => $response)));
    }
?>

Any help is greatly appreciated.

You have to enable debug for it to spit out the details of the sent email. Note that once debug mode is turned on, it still outputs data, a whole lot of email data about the sending process whether it fails or succeeds.

$mail->SMTPDebug = 0; disables debug

$mail->SMTPDebug = 1; enables client related debug

$mail->SMTPDebug = 2; enables server related debug

Your debug statement should come before any of the other $mail object statements, that is before your $mail->isSMTP() call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM