无法使用PhP发送电子邮件

时间:2014-06-10 04:45:13

标签: javascript php ajax html5

我正在尝试使用php从表单发送电子邮件,这是我的PHP代码(连同验证,不想遗漏任何内容:_)

<?php
/*
* Contact Form Class
*/


header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'sgg3590@rit.edu'; // Your Email
$message_min_length = 5; // Min Message Length


class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Contact from Your Website'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }


    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }


    private function validateFields(){

        if(!$this->name)
        {
            $this->response_html .= '<p>Please enter your name</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>Please enter an e-mail address</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>Please enter a valid e-mail address</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }

    }


    private function sendEmail(){

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

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = '<p>Thank You!</p>';
        }
        else
        {
        $this->response_status = 0;
            $this->response_html = '<p>Sending failed</p>';
        }

    }


    function sendRequest(){

        $this->validateFields();

        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

&GT;

这就是我如何称呼它

BRUSHED.contactForm = function(){
    $("#contact-submit").on('click',function() {
        $contact_form = $('#contact-form');

        var fields = $contact_form.serialize();

        $.ajax({
            type: "POST",
            url: "_include/php/contact.php",
            data: fields,
            dataType: 'json',
            success: function(response) {

                if(response.status){
                    $('#contact-form input').val('');
                    $('#contact-form textarea').val('');
                }

                $('#response').empty().html(response.html);
            }
        });
        return false;
    });
}

这是我的表格

<form id="contact-form" class="contact-form" action="#">
                                <p class="contact-name">
                                    <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
                                </p>
                                <p class="contact-email">
                                    <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
                                </p>
                                <p class="contact-message">
                                    <textarea id="contact_message" placeholder="Your Message" name="message" rows="5" cols="40"></textarea>
                                </p>
                                <p class="contact-submit">
                                    <a id="contact-submit" class="submit" href="#">Send Your Email</a>
                                </p>

                                <div id="response">

                                </div>
                            </form>

验证工作正常,因此它转到php文件,但我无法发送电子邮件,并且在我按下发送按钮后没有填写响应div(既不用谢谢也不发送失败) 我从一个网站上关注了这个代码,我真的不明白这里有什么错误.. :(

2 个答案:

答案 0 :(得分:1)

首先确保您已将计算机主机设置为与域名相同,因为有时邮件服务器会拒绝没有匹配域的邮件标头,例如Sender: test@test.orgFrom: localhost

然后,安装postfix,以便纠正电子邮件中的任何不正确/不正确的内容,最后,您丢失了电子邮件标题。以下是我的例子:

<?php
try {
    $to      = 'user@test.org';
    $subject = 'Mail Test';
    $message = <<<TEXT
Mail request received
TEXT;
    $message = str_replace("\n.", "\n..", $message);
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: Test Org. <webmaster@test.org>' . "\r\n" .
        'Reply-To: webmaster@test.org' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
    printf("Mail sent to " + $to);
} catch (Exception $e) {
    printf("Error occured: " + $e);
}
?>

如果仍然失败,您可以尝试从控制台echo "this is the body" | mail -s "test" "receipent@test.org"发送测试消息,看看它是否有效。如果两者都失败了,最好询问服务器供应商,因为他们可能已将外发邮件设置为禁用或其他内容。

答案 1 :(得分:0)

我正在使用WAMP的本地主机,并意识到wamp服务器没有提供功能。这段代码很好并且工作正常。

由于