簡體   English   中英

PHP AJAX Mailer問題

[英]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>

這是我的Ajax代碼:

$(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.');
            }
        });

    });

});

這是我的Php Mailer代碼:

<?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!';

}

 ?>

我認為$ mess關鍵字存在問題,但我想將這些前綴與發送郵件中的詳細信息連接起來。

但是,如果我在那之后也進行了糾正,那么郵件也就不會發送了。

誰能幫我解決這個問題的原因。

請為此使用phpmailer類,因為它是發送郵件所必需的。

謝謝您的幫助。

如果您使用的是XAMPP,WAMP,LAMP等本地服務器。 也許您必須在服務器的php.ini上配置SMTP

如果要在網絡服務器中部署應用程序,請檢查您的gmail帳戶是否已激活允許“較少安全的應用程序”通過您的帳戶進行連接的權限。

您可以使用PHPMailer調試來跟蹤日志中的錯誤,並為我們提供有關該問題的更多信息:

$ mail-> SMTPDebug = 2; //啟用SMTP調試信息(用於測試)// 1 =錯誤和消息// 2 =僅消息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM