簡體   English   中英

html聯系人表格+ php無法發送電子郵件

[英]html contact form + php not sends email

我只是嘗試使用html表單和php發送電子郵件,但無法將電子郵件發送到文本框中提供的相應郵件ID作為輸入。

我搜索並獲得了此示例代碼,任何人都可以建議我在哪里出錯。

html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Contact Form</title>
        <script  src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
        <link rel="stylesheet" type="text/css" href="./styles.css" />

        <script type="text/javascript">                                         
        /* <![CDATA[ */
            $(document).ready(function(){ // sends the data filled in the contact form to the php file and shows a message
                $("#contact-form").submit(function(){
                    var str = $(this).serialize();
                    $.ajax({
                       type: "POST",
                       url: "send.php",
                       data: str,
                       success: function(msg)
                       {
                            $("#formstatus").ajaxComplete(function(event, request, settings){
                                if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
                                    result = '<div class="formstatusok">Your message has been sent. Thank you!</div>';
                                    $("#fields").hide();
                                }
                                else{
                                    result = msg;
                                }
                                $(this).html(result);
                            });
                        }

                     });
                    return false;
                });
            });
        /* ]]> */   
        </script>  
    </head>

    <body>
        <div id="wrap">
            <h1>Drop me a Message!</h1>
            <div id='form_wrap'>
                <form id="contact-form" action="javascript:alert('success!');">

            <p id="formstatus"></p>
                    <p>Hello Joe,</p>
                    <label for="email">Your Message : </label>
                    <textarea  name="message" value="Your Message" id="message" ></textarea>
                    <p>Best,</p>    
                    <label for="name">Name: </label>
                    <input type="text" name="name" value="" id="name" />
                    <label for="email">Email: </label>
                    <input type="text" name="email" value="" id="email" />
                    <input type="submit" name ="submit" value="OK I want to send it now, thanks!" />
                </form>
            </div>
        </div>
    </body>
    </html>

send.php

    <?php
        define("WEBMASTER_EMAIL", 'myid@company.com');

        error_reporting (E_ALL ^ E_NOTICE);

        function ValidateEmail($email)
        {
            $regex = '/([a-z0-9_.-]+)'. # name
            '@'. # at
            '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
            '.'. # period
            '([a-z]+){2,10}/i'; # domain extension 

            if($email == '') 
                return false;
            else
                $eregi = preg_replace($regex, '', $email);
            return empty($eregi) ? true : false;
        }

    //////////////////////////////////////////////////////

        $post = (!empty($_POST)) ? true : false;

        if($post)
        {
            $name    = stripslashes($_POST['name']);
            $email   = trim($_POST['email']);
            $subject = stripslashes($_POST['subject']);
            $message = stripslashes($_POST['message']);

            $error = '';

            // Check name
            if(!$name || $name == "Name*")
                $error .= 'Please enter your name.<br />';

            // Check email
            if(!$email || $email == "Email*")
                $error .= 'Please enter an e-mail address.<br />';

            if($email && !ValidateEmail($email))
                $error .= 'Please enter a valid e-mail address.<br />';

            // Check message
            if(!$message)
                $error .= "Please enter your message. <br />";

            if(!$error)
            {
                $mail = mail(WEBMASTER_EMAIL, $subject, $message,
                     "From: ".$name." <".$email.">\r\n"
                    ."Reply-To: ".$email."\r\n"
                    ."X-Mailer: PHP/" . phpversion());

                if($mail)
                    echo 'OK';
            }
            else
                echo '<div class="formstatuserror">'.$error.'</div>';
        }

    ?>

有任何想法嗎 ?

嘗試在您的php.ini中配置它

SMTP = smtp.exmaple.com -> smtp host
smtp_port = 25 -> port usually 25
username = usename@user.com
password = password
sendmail_from = sourceofmail@example.com

首先,由於垃圾郵件引起的問題,許多服務器都限制了郵件的發送,除非經過某些授權機構(例如Gmail,Yahoo等)的驗證。

其次,您應該檢查您的php版本,看看是否使用了正確的格式,而不是復制粘貼示例代碼。

最后,如果您是在本地計算機上執行此操作,則您的ISP可能已經實施了一些發送電子郵件的規定。

如果您從ajax收到“確定”消息,並且顯示了已發送郵件的消息,但仍然沒有收到電子郵件,那么您需要檢查php.ini的smtp服務器設置。

打開您的php.ini文件,然后查找以下設置:

SMTP = smtp.gmail.com 
smtp_port = 25
username = youremail@gmail.com
password = PASSWORD
sendmail_from = webmaster@yourdomain.com

如果您不確定從何處獲取這些郵件,可以使用gmail的smtp服務器設置發送電子郵件進行測試。
檢查一下: https : //www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server

我在服務器上嘗試了您的代碼,並且發現了第一件事:“ ReferenceError:未定義$”

因此,鏈接的jquery無法正常工作,並且不會執行ajax請求。

將其更改為googleapis上的最新版本(googlecode將在2016年1月25日關閉)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

(假設您的php.ini配置為發送郵件:請參閱其他答案)

暫無
暫無

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

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