简体   繁体   中英

PHP AJAX Mailer Issue

<!DOCTYPE html>
<html lang="en">
<head>

    <title>AJAX Contact Form Demo</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div id="page-wrapper">
      <h1>AJAX Contact Form Demo</h1>

      <div id="form-messages"></div>

        <form id="main-contact-form" method="post" action="mailer.php">
            <div class="field">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" name="name" required>
            </div>

            <div class="field">
                <label for="email">Email:</label>
                <input type="email" class="form-control" id="email" name="email" required>
            </div>

            <div class="field">
                <label for="message">Message:</label>
                <textarea id="message" class="form-control" name="message" required></textarea>
            </div>

            <div class="field">
                <button type="submit" name="submit">Send</button>
            </div>
        </form>
    </div>

    <script src="jquery-2.1.0.min.js"></script>
    <script type="text/javascript" src="pap.js"></script>
</body>
</html>

Here is my Ajax Code:

$(function() {

    // Get the form.
    var form = $('#main-contact-form');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })

        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');

        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occured and your message could not be sent.');
            }
        });

    });

});

Here is my Php Mailer Code:

<?php


if(isset($_POST['submit'])) 
{
        $name = strip_tags(trim($_POST["name"]));
        $name = str_replace(array("\r","\n"),array(" "," "),$name);
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $message = trim($_POST["message"]);


$mess=
'Full Name: $name<br />
Email:  $email<br />
Comments:  $message<br /> 
';

    require "phpmailer/class.phpmailer.php"; //include phpmailer class

    // Instantiate Class  
    $mail = new PHPMailer();  

    // Set up SMTP  
    $mail->IsSMTP();                // Sets up a SMTP connection  
    $mail->SMTPAuth = true;         // Connection with the SMTP does require authorization    
    $mail->SMTPSecure = "ssl";      // Connect using a TLS connection  
    $mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
    $mail->Port = 465;  //Gmail SMTP port
    //$mail->Encoding = '7bit';

    // Authentication  
    $mail->Username   = "Enter Your Email-ID For Testing"; // Your full Gmail address
    $mail->Password   = "Your Email Password"; // Your Gmail password

    // Compose
    $mail->SetFrom($_POST['email'], $_POST['name']);
    $mail->AddReplyTo($_POST['email'], $_POST['name']);
    $mail->Subject = "hello";      // Subject (which isn't required)  
    $mail->MsgHTML($mess);
    echo"hello";
    // Send To  
    $mail->AddAddress("aa@gmail.com", "Bla Bla"); // Where to send it - Recipient
    $result = $mail->Send();        // Send!  
    $mess = $result ? 'Successfully Sent!' : 'Sending Failed!';      
    unset($mail);
    echo 'Successfully Sent!';

}

 ?>

I think there is issue in $mess keyword but i want to concatenate these prefix with the details in the sending mail .

But if i correct that also after that also the mail is not going .

Can anybody help me out why this problem is coming .

Please use the phpmailer class for this as it is required to send the mail.

Thanks For the help.

If you're using a local server like XAMPP, WAMP, LAMP. Maybe you have to configure the SMTP on your server's php.ini

If you are deploying you app in a webserver, check if your gmail account have activated the permission to allow "Less secure apps" to connect through your account.

You may use the PHPMailer Debug to track the error on the log and bring us more information about the issue:

$mail->SMTPDebug = 2; // enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only

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